Messing around with UUID's and ObjectId's.

This commit is contained in:
Olof Larsson 2013-04-12 11:39:47 +02:00
parent 6e15d22eda
commit 21dac166eb
7 changed files with 143 additions and 11 deletions

View File

@ -4,6 +4,7 @@ import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -14,7 +15,9 @@ import org.bukkit.inventory.PlayerInventory;
import com.massivecraft.mcore.adapter.InventoryAdapter;
import com.massivecraft.mcore.adapter.ItemStackAdapter;
import com.massivecraft.mcore.adapter.MongoURIAdapter;
import com.massivecraft.mcore.adapter.ObjectIdAdapter;
import com.massivecraft.mcore.adapter.PlayerInventoryAdapter;
import com.massivecraft.mcore.adapter.UUIDAdapter;
import com.massivecraft.mcore.cmd.CmdMcore;
import com.massivecraft.mcore.integration.protocollib.ProtocolLibFeatures;
import com.massivecraft.mcore.mixin.ScheduledTeleportEngine;
@ -32,6 +35,7 @@ import com.massivecraft.mcore.util.FirstTeleportUtil;
import com.massivecraft.mcore.util.PlayerUtil;
import com.massivecraft.mcore.util.TimeDiffUtil;
import com.massivecraft.mcore.util.TimeUnit;
import com.massivecraft.mcore.xlib.bson.types.ObjectId;
import com.massivecraft.mcore.xlib.gson.Gson;
import com.massivecraft.mcore.xlib.gson.GsonBuilder;
import com.massivecraft.mcore.xlib.mongodb.MongoURI;
@ -67,6 +71,8 @@ public class MCore extends MPlugin
.disableHtmlEscaping()
.excludeFieldsWithModifiers(Modifier.TRANSIENT)
.registerTypeAdapter(MongoURI.class, MongoURIAdapter.get())
.registerTypeAdapter(ObjectId.class, ObjectIdAdapter.get())
.registerTypeAdapter(UUID.class, UUIDAdapter.get())
.registerTypeAdapter(ItemStack.class, ItemStackAdapter.get())
.registerTypeAdapter(Inventory.class, InventoryAdapter.get())
.registerTypeAdapter(PlayerInventory.class, PlayerInventoryAdapter.get())

View File

@ -0,0 +1,63 @@
package com.massivecraft.mcore.adapter;
import java.lang.reflect.Type;
import com.massivecraft.mcore.xlib.bson.types.ObjectId;
import com.massivecraft.mcore.xlib.gson.JsonDeserializationContext;
import com.massivecraft.mcore.xlib.gson.JsonDeserializer;
import com.massivecraft.mcore.xlib.gson.JsonElement;
import com.massivecraft.mcore.xlib.gson.JsonParseException;
import com.massivecraft.mcore.xlib.gson.JsonPrimitive;
import com.massivecraft.mcore.xlib.gson.JsonSerializationContext;
import com.massivecraft.mcore.xlib.gson.JsonSerializer;
public class ObjectIdAdapter implements JsonDeserializer<ObjectId>, JsonSerializer<ObjectId>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ObjectIdAdapter i = new ObjectIdAdapter();
public static ObjectIdAdapter get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public JsonElement serialize(ObjectId src, Type typeOfSrc, JsonSerializationContext context)
{
return convertObjectIdToJsonPrimitive(src);
}
@Override
public ObjectId deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
return convertJsonElementToObjectId(json);
}
// -------------------------------------------- //
// STATIC LOGIC
// -------------------------------------------- //
public static String convertObjectIdToString(ObjectId objectId)
{
return objectId.toString();
}
public static JsonPrimitive convertObjectIdToJsonPrimitive(ObjectId objectId)
{
return new JsonPrimitive(convertObjectIdToString(objectId));
}
// Can return null
public static ObjectId convertStringToObjectId(String string)
{
return ObjectId.massageToObjectId(string);
}
public static ObjectId convertJsonElementToObjectId(JsonElement jsonElement)
{
return convertStringToObjectId(jsonElement.getAsString());
}
}

View File

@ -21,7 +21,7 @@ public class PlayerInventoryAdapter implements JsonDeserializer<PlayerInventory>
public static PlayerInventoryAdapter get() { return i; }
// -------------------------------------------- //
// IMPLEMENTATION
// OVERRIDE
// -------------------------------------------- //
@Override

View File

@ -0,0 +1,62 @@
package com.massivecraft.mcore.adapter;
import java.lang.reflect.Type;
import java.util.UUID;
import com.massivecraft.mcore.xlib.gson.JsonDeserializationContext;
import com.massivecraft.mcore.xlib.gson.JsonDeserializer;
import com.massivecraft.mcore.xlib.gson.JsonElement;
import com.massivecraft.mcore.xlib.gson.JsonParseException;
import com.massivecraft.mcore.xlib.gson.JsonPrimitive;
import com.massivecraft.mcore.xlib.gson.JsonSerializationContext;
import com.massivecraft.mcore.xlib.gson.JsonSerializer;
public class UUIDAdapter implements JsonDeserializer<UUID>, JsonSerializer<UUID>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static UUIDAdapter i = new UUIDAdapter();
public static UUIDAdapter get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public JsonElement serialize(UUID src, Type typeOfSrc, JsonSerializationContext context)
{
return convertUUIDToJsonPrimitive(src);
}
@Override
public UUID deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
return convertJsonElementToUUID(json);
}
// -------------------------------------------- //
// STATIC LOGIC
// -------------------------------------------- //
public static String convertUUIDToString(UUID objectId)
{
return objectId.toString();
}
public static JsonPrimitive convertUUIDToJsonPrimitive(UUID objectId)
{
return new JsonPrimitive(convertUUIDToString(objectId));
}
public static UUID convertStringToUUID(String string)
{
return UUID.fromString(string);
}
public static UUID convertJsonElementToUUID(JsonElement jsonElement)
{
return convertStringToUUID(jsonElement.getAsString());
}
}

View File

@ -13,7 +13,7 @@ import java.util.Map.Entry;
import com.massivecraft.mcore.store.idstrategy.IdStrategyAiGson;
import com.massivecraft.mcore.store.idstrategy.IdStrategyOidGson;
import com.massivecraft.mcore.store.idstrategy.IdStrategyUuidMongoAndGson;
import com.massivecraft.mcore.store.idstrategy.IdStrategyUuid;
import com.massivecraft.mcore.store.storeadapter.StoreAdapter;
import com.massivecraft.mcore.store.storeadapter.StoreAdapterGson;
import com.massivecraft.mcore.util.DiscUtil;
@ -202,7 +202,7 @@ public class DriverGson extends DriverAbstract<JsonElement>
instance = new DriverGson();
instance.registerIdStrategy(IdStrategyAiGson.get());
instance.registerIdStrategy(IdStrategyOidGson.get());
instance.registerIdStrategy(IdStrategyUuidMongoAndGson.get());
instance.registerIdStrategy(IdStrategyUuid.get());
}
}

View File

@ -11,7 +11,7 @@ import java.util.Map.Entry;
import com.massivecraft.mcore.store.idstrategy.IdStrategyAiMongo;
import com.massivecraft.mcore.store.idstrategy.IdStrategyOidMongo;
import com.massivecraft.mcore.store.idstrategy.IdStrategyUuidMongoAndGson;
import com.massivecraft.mcore.store.idstrategy.IdStrategyUuid;
import com.massivecraft.mcore.store.storeadapter.StoreAdapter;
import com.massivecraft.mcore.store.storeadapter.StoreAdapterMongo;
import com.massivecraft.mcore.xlib.mongodb.BasicDBObject;
@ -235,6 +235,6 @@ public class DriverMongo extends DriverAbstract<BasicDBObject>
instance = new DriverMongo();
instance.registerIdStrategy(IdStrategyAiMongo.get());
instance.registerIdStrategy(IdStrategyOidMongo.get());
instance.registerIdStrategy(IdStrategyUuidMongoAndGson.get());
instance.registerIdStrategy(IdStrategyUuid.get());
}
}

View File

@ -2,24 +2,25 @@ package com.massivecraft.mcore.store.idstrategy;
import java.util.UUID;
import com.massivecraft.mcore.adapter.UUIDAdapter;
import com.massivecraft.mcore.store.CollInterface;
public class IdStrategyUuidMongoAndGson extends IdStrategyAbstract<UUID, String>
public class IdStrategyUuid extends IdStrategyAbstract<UUID, String>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static IdStrategyUuidMongoAndGson i = new IdStrategyUuidMongoAndGson();
public static IdStrategyUuidMongoAndGson get() { return i; }
private IdStrategyUuidMongoAndGson() { super("uuid", UUID.class, String.class); }
private static IdStrategyUuid i = new IdStrategyUuid();
public static IdStrategyUuid get() { return i; }
private IdStrategyUuid() { super("uuid", UUID.class, String.class); }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override public String localToRemote(Object local) { return ((UUID)local).toString(); }
@Override public UUID remoteToLocal(Object remote) { return UUID.fromString((String)remote); }
@Override public String localToRemote(Object local) { return UUIDAdapter.convertUUIDToString((UUID)local); }
@Override public UUID remoteToLocal(Object remote) { return UUIDAdapter.convertStringToUUID((String)remote); }
@Override
public UUID generateAttempt(CollInterface<?, UUID> coll)