mirror of
https://github.com/dmulloy2/ProtocolLib.git
synced 2024-11-24 03:25:29 +01:00
Add a generic enum converter and a set converter
This commit is contained in:
parent
1b4a8ec0aa
commit
fbc596d57b
@ -24,9 +24,11 @@ import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bukkit.Material;
|
||||
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.
|
||||
* @param <T> Type
|
||||
|
@ -489,6 +489,15 @@ public abstract class EnumWrappers {
|
||||
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
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
private static class EnumConverter<T extends Enum<T>> implements EquivalentConverter<T> {
|
||||
|
Loading…
Reference in New Issue
Block a user