A polymorph adapter and some text utils.
This commit is contained in:
parent
ef49abde15
commit
303f91430a
76
src/com/massivecraft/mcore5/adapter/PolymorphicAdapter.java
Normal file
76
src/com/massivecraft/mcore5/adapter/PolymorphicAdapter.java
Normal file
@ -0,0 +1,76 @@
|
||||
package com.massivecraft.mcore5.adapter;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
|
||||
|
||||
import com.massivecraft.mcore5.util.ClassResolveUtil;
|
||||
import com.massivecraft.mcore5.xlib.gson.JsonDeserializationContext;
|
||||
import com.massivecraft.mcore5.xlib.gson.JsonDeserializer;
|
||||
import com.massivecraft.mcore5.xlib.gson.JsonElement;
|
||||
import com.massivecraft.mcore5.xlib.gson.JsonNull;
|
||||
import com.massivecraft.mcore5.xlib.gson.JsonObject;
|
||||
import com.massivecraft.mcore5.xlib.gson.JsonParseException;
|
||||
import com.massivecraft.mcore5.xlib.gson.JsonPrimitive;
|
||||
import com.massivecraft.mcore5.xlib.gson.JsonSerializationContext;
|
||||
import com.massivecraft.mcore5.xlib.gson.JsonSerializer;
|
||||
|
||||
public class PolymorphicAdapter<T> implements JsonDeserializer<T>, JsonSerializer<T>
|
||||
{
|
||||
public static final String TYPE = "type";
|
||||
public static final String VALUE = "value";
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context)
|
||||
{
|
||||
if (src == null)
|
||||
{
|
||||
return JsonNull.INSTANCE;
|
||||
}
|
||||
|
||||
JsonObject ret = new JsonObject();
|
||||
|
||||
String type = src.getClass().getCanonicalName();
|
||||
ret.addProperty(TYPE, type);
|
||||
|
||||
JsonElement value = context.serialize(src);
|
||||
ret.add(VALUE, value);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
if (!json.isJsonObject())
|
||||
{
|
||||
throw new JsonParseException("A polymorph must be an object.");
|
||||
}
|
||||
|
||||
JsonObject jsonObject = json.getAsJsonObject();
|
||||
|
||||
if (!jsonObject.has(TYPE))
|
||||
{
|
||||
throw new JsonParseException("A polymorph must be have a \""+TYPE+"\" field.");
|
||||
}
|
||||
|
||||
if (!jsonObject.has(VALUE))
|
||||
{
|
||||
throw new JsonParseException("A polymorph must be have a \"+VALUE+\" field.");
|
||||
}
|
||||
|
||||
String type = ((JsonPrimitive)jsonObject.get(TYPE)).getAsString();
|
||||
|
||||
Class<?> typeClass = null;
|
||||
try
|
||||
{
|
||||
typeClass = ClassResolveUtil.resolve(type);
|
||||
}
|
||||
catch (ClassNotFoundException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
throw new JsonParseException(e.getMessage());
|
||||
}
|
||||
return context.deserialize(jsonObject.get(VALUE), typeClass);
|
||||
}
|
||||
}
|
57
src/com/massivecraft/mcore5/util/ClassResolveUtil.java
Normal file
57
src/com/massivecraft/mcore5/util/ClassResolveUtil.java
Normal file
@ -0,0 +1,57 @@
|
||||
package com.massivecraft.mcore5.util;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.SimplePluginManager;
|
||||
import org.bukkit.plugin.java.JavaPluginLoader;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class ClassResolveUtil
|
||||
{
|
||||
protected static Collection<JavaPluginLoader> javaPluginLoaders;
|
||||
|
||||
static
|
||||
{
|
||||
try
|
||||
{
|
||||
SimplePluginManager pluginManager = (SimplePluginManager)Bukkit.getPluginManager();
|
||||
Field field = SimplePluginManager.class.getDeclaredField("fileAssociations");
|
||||
field.setAccessible(true);
|
||||
Map<Pattern, JavaPluginLoader> fileAssociations = (Map<Pattern, JavaPluginLoader>)field.get(pluginManager);
|
||||
javaPluginLoaders = fileAssociations.values(); // Does this one autoupdate?
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static Class<?> resolve(String className) throws ClassNotFoundException
|
||||
{
|
||||
//System.out.println("resolve..."+this.javaPluginLoaders.toString());
|
||||
Class<?> ret = null;
|
||||
for (JavaPluginLoader javaPluginLoader : javaPluginLoaders)
|
||||
{
|
||||
ret = javaPluginLoader.getClassByName(className);
|
||||
if (ret != null)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
|
||||
try
|
||||
{
|
||||
return classLoader.loadClass(className);
|
||||
}
|
||||
catch (ClassNotFoundException e)
|
||||
{
|
||||
return Class.forName(className, false, classLoader);
|
||||
}
|
||||
}
|
||||
}
|
@ -26,6 +26,11 @@ public class Txt
|
||||
public static final long millisPerMonth = 31 * millisPerDay;
|
||||
public static final long millisPerYear = 365 * millisPerDay;
|
||||
|
||||
public static final Set<String> vowel = MUtil.set(
|
||||
"A", "E", "I", "O", "U", "Y", "Å", "Ä", "Ö",
|
||||
"a", "e", "i", "o", "u", "y", "å", "ä", "ö"
|
||||
);
|
||||
|
||||
public static Map<String, Long> unitMillis;
|
||||
|
||||
static
|
||||
@ -328,20 +333,41 @@ public class Txt
|
||||
return new SimpleEntry<String, String>(first, second);
|
||||
}
|
||||
|
||||
public static boolean isVowel(String str)
|
||||
{
|
||||
if (str == null || str.length() == 0) return false;
|
||||
return vowel.contains(str.substring(0, 1));
|
||||
}
|
||||
|
||||
public static String aan(String noun)
|
||||
{
|
||||
return isVowel(noun) ? "an" : "a";
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// Material name tools
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static String getMaterialName(Material material)
|
||||
public static String getNicedEnumString(String str)
|
||||
{
|
||||
List<String> parts = new ArrayList<String>();
|
||||
for (String part : material.toString().toLowerCase().split("_"))
|
||||
for (String part : str.toLowerCase().split("_"))
|
||||
{
|
||||
parts.add(upperCaseFirst(part));
|
||||
}
|
||||
return implode(parts, " ");
|
||||
}
|
||||
|
||||
public static String getNicedEnum(Object enumObject)
|
||||
{
|
||||
return getNicedEnumString(enumObject.toString());
|
||||
}
|
||||
|
||||
public static String getMaterialName(Material material)
|
||||
{
|
||||
return getNicedEnum(material);
|
||||
}
|
||||
|
||||
public static String getMaterialName(int materialId)
|
||||
{
|
||||
return getMaterialName(Material.getMaterial(materialId));
|
||||
|
Loading…
Reference in New Issue
Block a user