Rework the BackstringSet solution

This commit is contained in:
Olof Larsson 2017-03-22 21:37:33 +01:00
parent 39473ca5b4
commit d96fee007a
8 changed files with 154 additions and 232 deletions

View File

@ -13,7 +13,7 @@ import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import com.massivecraft.massivecore.adapter.AdapterBackstringEnumSet;
import com.massivecraft.massivecore.adapter.AdapterBackstringSet;
import com.massivecraft.massivecore.adapter.AdapterBannerPatterns;
import com.massivecraft.massivecore.adapter.AdapterEntry;
import com.massivecraft.massivecore.adapter.AdapterInventory;
@ -30,7 +30,7 @@ import com.massivecraft.massivecore.adapter.AdapterMsonEvent;
import com.massivecraft.massivecore.adapter.AdapterPlayerInventory;
import com.massivecraft.massivecore.adapter.AdapterSound;
import com.massivecraft.massivecore.adapter.AdapterUUID;
import com.massivecraft.massivecore.collections.BackstringEnumSet;
import com.massivecraft.massivecore.collections.BackstringSet;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.collections.MassiveListDef;
import com.massivecraft.massivecore.collections.MassiveMap;
@ -195,7 +195,7 @@ public class MassiveCore extends MassivePlugin
ret.registerTypeAdapter(Entry.class, AdapterEntry.get());
// Assorted Custom
ret.registerTypeAdapter(BackstringEnumSet.class, AdapterBackstringEnumSet.get());
ret.registerTypeAdapter(BackstringSet.class, AdapterBackstringSet.get());
ret.registerTypeAdapter(PS.class, PSAdapter.get());
ret.registerTypeAdapter(Sound.class, AdapterSound.get());
ret.registerTypeAdapter(UUID.class, AdapterUUID.get());

View File

@ -4,7 +4,8 @@ import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Set;
import com.massivecraft.massivecore.collections.BackstringEnumSet;
import com.massivecraft.massivecore.collections.BackstringSet;
import com.massivecraft.massivecore.command.type.RegistryType;
import com.massivecraft.massivecore.xlib.gson.JsonDeserializationContext;
import com.massivecraft.massivecore.xlib.gson.JsonDeserializer;
import com.massivecraft.massivecore.xlib.gson.JsonElement;
@ -13,7 +14,7 @@ import com.massivecraft.massivecore.xlib.gson.JsonSerializationContext;
import com.massivecraft.massivecore.xlib.gson.JsonSerializer;
import com.massivecraft.massivecore.xlib.gson.reflect.TypeToken;
public class AdapterBackstringEnumSet implements JsonDeserializer<BackstringEnumSet<?>>, JsonSerializer<BackstringEnumSet<?>>
public class AdapterBackstringSet implements JsonDeserializer<BackstringSet<?>>, JsonSerializer<BackstringSet<?>>
{
// -------------------------------------------- //
// CONSTANTS
@ -25,30 +26,28 @@ public class AdapterBackstringEnumSet implements JsonDeserializer<BackstringEnum
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final AdapterBackstringEnumSet i = new AdapterBackstringEnumSet();
public static AdapterBackstringEnumSet get() { return i; }
private static final AdapterBackstringSet i = new AdapterBackstringSet();
public static AdapterBackstringSet get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public JsonElement serialize(BackstringEnumSet<?> src, Type type, JsonSerializationContext context)
public JsonElement serialize(BackstringSet<?> src, Type type, JsonSerializationContext context)
{
return context.serialize(src.getStringSet(), stringSetType);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public BackstringEnumSet<?> deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException
public BackstringSet<?> deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException
{
Set<String> stringSet = context.deserialize(json, stringSetType);
ParameterizedType ptype = (ParameterizedType) type;
Type[] args = ptype.getActualTypeArguments();
Class<?> clazz = (Class<?>) args[0];
return new BackstringEnumSet(clazz, stringSet);
return new BackstringSet(RegistryType.getType(clazz), stringSet);
}
}

View File

@ -1,77 +0,0 @@
package com.massivecraft.massivecore.collections;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
public class BackstringEnumSet<T extends Enum<?>> extends BackstringSet<T>
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private Class<T> clazz;
private Map<String, T> name2enum = new LinkedHashMap<String, T>();
private void init(Class<T> clazz)
{
this.clazz = clazz;
for (T t : this.clazz.getEnumConstants())
{
name2enum.put(t.name(), t);
}
}
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public BackstringEnumSet(Class<T> clazz)
{
super();
init(clazz);
}
public BackstringEnumSet(Class<T> clazz, Collection<?> c)
{
super(c);
init(clazz);
}
public BackstringEnumSet(Class<T> clazz, Object... objects)
{
super(objects);
init(clazz);
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public T convertFromString(String string)
{
return this.name2enum.get(string);
}
@Override
public String convertToString(Object t)
{
if (t == null) return null;
if (t instanceof Enum<?>)
{
Enum<?> e = (Enum<?>)t;
return e.name();
}
return t.toString();
}
@Override
public Map<String, T> getStringToTypeMap()
{
return this.name2enum;
}
}

View File

@ -1,79 +0,0 @@
package com.massivecraft.massivecore.collections;
import java.util.Iterator;
@Deprecated
public class BackstringIterator<E> implements Iterator<E>
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private final Iterator<String> iterator;
private final BackstringSet<E> set;
private String next = null;
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public BackstringIterator(Iterator<String> iterator, BackstringSet<E> set)
{
this.iterator = iterator;
this.set = set;
this.prepareNext();
}
// -------------------------------------------- //
// INTERNAL
// -------------------------------------------- //
private void prepareNext()
{
String perhaps = null;
while (this.iterator.hasNext())
{
perhaps = this.iterator.next();
if (this.set.convertFromString(perhaps) != null)
{
this.next = perhaps;
break;
}
}
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public boolean hasNext()
{
return (this.next != null);
}
@Override
public E next()
{
String current = this.next;
this.prepareNext();
if (current == null) return null;
return this.set.convertFromString(current);
}
@Override
public void remove()
{
throw new UnsupportedOperationException();
}
// -------------------------------------------- //
// EXTRAS
// -------------------------------------------- //
public String peek()
{
return next;
}
}

View File

@ -1,56 +1,98 @@
package com.massivecraft.massivecore.collections;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.command.type.RegistryType;
import com.massivecraft.massivecore.command.type.Type;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public abstract class BackstringSet<T> extends AbstractSet<T>
public class BackstringSet<T> extends AbstractSet<T>
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private Set<String> stringSet = new LinkedHashSet<String>();
private final Type<T> type;
private final Set<String> stringSet;
public Set<String> getStringSet() { return this.stringSet; }
// -------------------------------------------- //
// ABSTRACT CONVERSION
// CONVTERT
// -------------------------------------------- //
public abstract T convertFromString(String string);
public abstract String convertToString(Object t);
private T convertFromString(String string) throws MassiveException
{
if (string == null) return null;
return this.type.read(string);
}
public abstract Map<String, T> getStringToTypeMap();
@SuppressWarnings("unchecked")
private String convertToString(Object object)
{
if (object == null) return null;
if (object instanceof String)
{
return (String)object;
}
return this.type.getId((T) object);
}
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public BackstringSet()
public BackstringSet(Type<T> type)
{
this.type = type;
this.stringSet = new MassiveSet<>();
}
public BackstringSet(Collection<?> c)
public BackstringSet(Type<T> type, Collection<?> collection)
{
if (c != null)
this(type);
if (collection != null)
{
for (Object o : c)
for (Object object : collection)
{
this.stringSet.add(this.convertToString(o));
String string = this.convertToString(object);
this.stringSet.add(string);
}
}
}
public BackstringSet(Object... objects)
public BackstringSet(Type<T> type, Object... objects)
{
this(Arrays.asList(objects));
this(type, Arrays.asList(objects));
}
// -------------------------------------------- //
// MOAR CONSTRUCT
// -------------------------------------------- //
@SuppressWarnings("unchecked")
public BackstringSet(Class<T> clazz)
{
this((Type<T>) RegistryType.getType(clazz));
}
@SuppressWarnings("unchecked")
public BackstringSet(Class<T> clazz, Collection<?> collection)
{
this((Type<T>) RegistryType.getType(clazz), collection);
}
@SuppressWarnings("unchecked")
public BackstringSet(Class<T> clazz, Object... objects)
{
this((Type<T>) RegistryType.getType(clazz), objects);
}
// -------------------------------------------- //
@ -61,20 +103,45 @@ public abstract class BackstringSet<T> extends AbstractSet<T>
public Iterator<T> iterator()
{
// Create
List<T> ret = new ArrayList<>();
// Get our conversion map
Map<String, T> typeMap = this.getStringToTypeMap();
List<T> temporaryList = new MassiveList<>();
// Fill
for (String key: this.getStringSet())
for (String string : this.getStringSet())
{
T value = typeMap.get(key);
if (value != null) ret.add(value);
try
{
T value = this.convertFromString(string);
if (value != null) temporaryList.add(value);
}
catch (MassiveException ignored)
{
// ignored
}
}
// Return
return ret.iterator();
final Iterator<T> temporaryIterator = temporaryList.iterator();
return new Iterator<T>()
{
@Override
public boolean hasNext()
{
return temporaryIterator.hasNext();
}
@Override
public T next()
{
return temporaryIterator.next();
}
@Override
public void remove()
{
String message = String.format("%s iterator does not support removal.", BackstringSet.class.getSimpleName());
throw new UnsupportedOperationException(message);
}
};
}
@Override
@ -84,42 +151,34 @@ public abstract class BackstringSet<T> extends AbstractSet<T>
}
@Override
public boolean contains(Object o)
public boolean contains(Object object)
{
if (o == null)
{
return this.stringSet.contains(null);
}
else
{
return this.stringSet.contains(this.convertToString(o));
}
String string = this.convertToString(object);
return this.stringSet.contains(string);
}
@Override
public boolean add(T e)
public boolean add(T object)
{
if (e == null)
{
return this.stringSet.add(null);
}
else
{
return this.stringSet.add(this.convertToString(e));
}
return this.addObject(object);
}
public boolean addString(String string)
{
return this.addObject(string);
}
private boolean addObject(Object object)
{
String string = this.convertToString(object);
return this.stringSet.add(string);
}
@Override
public boolean remove(Object o)
public boolean remove(Object object)
{
if (o == null)
{
return this.stringSet.remove(null);
}
else
{
return this.stringSet.remove(this.convertToString(o));
}
String string = this.convertToString(object);
return this.stringSet.remove(string);
}
@Override

View File

@ -6,7 +6,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import com.massivecraft.massivecore.collections.BackstringEnumSet;
import com.massivecraft.massivecore.collections.BackstringSet;
import com.massivecraft.massivecore.collections.ExceptionSet;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.collections.MassiveMap;
@ -18,7 +18,7 @@ import com.massivecraft.massivecore.command.type.combined.TypeDataPotionEffect;
import com.massivecraft.massivecore.command.type.combined.TypeEntry;
import com.massivecraft.massivecore.command.type.combined.TypePotionEffectWrap;
import com.massivecraft.massivecore.command.type.combined.TypeSoundEffect;
import com.massivecraft.massivecore.command.type.container.TypeBackStringEnumSet;
import com.massivecraft.massivecore.command.type.container.TypeBackstringSet;
import com.massivecraft.massivecore.command.type.container.TypeExceptionSet;
import com.massivecraft.massivecore.command.type.container.TypeList;
import com.massivecraft.massivecore.command.type.container.TypeMap;
@ -43,6 +43,7 @@ import com.massivecraft.massivecore.command.type.enumeration.TypeParticleEffect;
import com.massivecraft.massivecore.command.type.enumeration.TypeRabbitType;
import com.massivecraft.massivecore.command.type.enumeration.TypeSkeletonType;
import com.massivecraft.massivecore.command.type.enumeration.TypeSound;
import com.massivecraft.massivecore.command.type.enumeration.TypeSpawnReason;
import com.massivecraft.massivecore.command.type.enumeration.TypeVillagerProfession;
import com.massivecraft.massivecore.command.type.enumeration.TypeWorldType;
import com.massivecraft.massivecore.command.type.primitive.TypeBooleanTrue;
@ -131,10 +132,10 @@ public class RegistryType
Class<?> fieldClass = field == null ? null : field.getType();
List<Type<?>> innerTypes;
if (ReflectionUtil.isRawTypeAssignableFromAny(BackstringEnumSet.class, fieldType, fieldClass))
if (ReflectionUtil.isRawTypeAssignableFromAny(BackstringSet.class, fieldType, fieldClass))
{
innerTypes = getInnerTypes(field, fieldType, 1);
return TypeBackStringEnumSet.get((Type<? extends Enum>)innerTypes.get(0));
return TypeBackstringSet.get((Type<? extends Enum>)innerTypes.get(0));
}
if (ReflectionUtil.isRawTypeAssignableFromAny(List.class, fieldType, fieldClass))
@ -316,6 +317,7 @@ public class RegistryType
register(TypeMaterial.get());
register(TypeOcelotType.get());
register(TypeParticleEffect.get());
register(TypeSpawnReason.get());
// 1.7 Compat
try

View File

@ -1,11 +1,11 @@
package com.massivecraft.massivecore.command.type.container;
import com.massivecraft.massivecore.collections.BackstringEnumSet;
import com.massivecraft.massivecore.collections.BackstringSet;
import com.massivecraft.massivecore.command.type.Type;
import java.util.Set;
public class TypeBackStringEnumSet<E extends Enum<E>> extends TypeContainer<Set<E>, E>
public class TypeBackstringSet<E extends Enum<E>> extends TypeContainer<Set<E>, E>
{
// -------------------------------------------- //
// FIELDS
@ -17,14 +17,14 @@ public class TypeBackStringEnumSet<E extends Enum<E>> extends TypeContainer<Set<
// CONSTRUCT
// -------------------------------------------- //
public static <E extends Enum<E>> TypeBackStringEnumSet<E> get(Type<E> innerType)
public static <E extends Enum<E>> TypeBackstringSet<E> get(Type<E> innerType)
{
return new TypeBackStringEnumSet<E>(innerType);
return new TypeBackstringSet<E>(innerType);
}
public TypeBackStringEnumSet(Type<E> innerType)
public TypeBackstringSet(Type<E> innerType)
{
super(BackstringEnumSet.class, innerType);
super(BackstringSet.class, innerType);
this.innerTypeClass = innerType.getClazz();
}
@ -33,9 +33,9 @@ public class TypeBackStringEnumSet<E extends Enum<E>> extends TypeContainer<Set<
// -------------------------------------------- //
@Override
public BackstringEnumSet<E> createNewInstance()
public BackstringSet<E> createNewInstance()
{
return new BackstringEnumSet<E>((Class<E>) innerTypeClass);
return new BackstringSet<E>((Class<E>) innerTypeClass);
}
}

View File

@ -0,0 +1,18 @@
package com.massivecraft.massivecore.command.type.enumeration;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
public class TypeSpawnReason extends TypeEnum<SpawnReason>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static TypeSpawnReason i = new TypeSpawnReason();
public static TypeSpawnReason get() { return i; }
public TypeSpawnReason()
{
super(SpawnReason.class);
}
}