Archiviert
13
0

Add a generic enum converter and a set converter

Dieser Commit ist enthalten in:
Dan Mulloy 2015-06-23 12:59:13 -04:00
Ursprung 1b4a8ec0aa
Commit fbc596d57b
2 geänderte Dateien mit 70 neuen und 1 gelöschten Zeilen

Datei anzeigen

@ -24,9 +24,11 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
@ -321,6 +323,64 @@ public class BukkitConverters {
}; };
} }
/**
* Retrieve an equivalent converter for a set of generic items.
* @param <T> Type
* @param genericItemType - the generic item type.
* @param itemConverter - an equivalent converter for the generic type.
* @return An equivalent converter.
*/
@SuppressWarnings("unchecked")
public static <T> EquivalentConverter<Set<T>> getSetConverter(final Class<?> genericItemType, final EquivalentConverter<T> itemConverter) {
// Convert to and from the wrapper
return new IgnoreNullConverter<Set<T>>() {
@Override
protected Set<T> getSpecificValue(Object generic) {
if (generic instanceof Collection) {
Set<T> items = new HashSet<T>();
// Copy everything to a new list
for (Object item : (Collection<Object>) generic) {
T result = itemConverter.getSpecific(item);
if (item != null)
items.add(result);
}
return items;
}
// Not valid
return null;
}
@Override
protected Object getGenericValue(Class<?> genericType, Set<T> specific) {
Collection<Object> newContainer = (Collection<Object>) DefaultInstances.DEFAULT.getDefault(genericType);
// Convert each object
for (T position : specific) {
Object converted = itemConverter.getGeneric(genericItemType, position);
if (position == null)
newContainer.add(null);
else if (converted != null)
newContainer.add(converted);
}
return newContainer;
}
@Override
public Class<Set<T>> getSpecificType() {
// Damn you Java
Class<?> dummy = Set.class;
return (Class<Set<T>>) dummy;
}
};
}
/** /**
* Retrieve an equivalent converter for an array of generic items. * Retrieve an equivalent converter for an array of generic items.
* @param <T> Type * @param <T> Type

Datei anzeigen

@ -489,6 +489,15 @@ public abstract class EnumWrappers {
return new EnumConverter<Particle>(Particle.class); return new EnumConverter<Particle>(Particle.class);
} }
/**
* Retrieve a generic enum converter for use with StructureModifiers.
* @param enumClass - Enum class
* @return A generic enum converter
*/
public static <T extends Enum<T>> EquivalentConverter<T> getGenericConverter(Class<T> enumClass) {
return new EnumConverter<T>(enumClass);
}
// The common enum converter // The common enum converter
@SuppressWarnings({ "rawtypes", "unchecked" }) @SuppressWarnings({ "rawtypes", "unchecked" })
private static class EnumConverter<T extends Enum<T>> implements EquivalentConverter<T> { private static class EnumConverter<T extends Enum<T>> implements EquivalentConverter<T> {