Search in sources :

Example 1 with ImmutableMultiset

use of com.google.common.collect.ImmutableMultiset in project Terasology by MovingBlocks.

the class ConstructorLibrary method getCollectionCopyConstructor.

public static <E> CollectionCopyConstructor<? extends Collection<E>, E> getCollectionCopyConstructor(TypeInfo<? extends Collection<E>> typeInfo) {
    Class<? extends Collection<E>> rawType = typeInfo.getRawType();
    Type type = typeInfo.getType();
    if (Multiset.class.isAssignableFrom(rawType)) {
        if (ImmutableMultiset.class.isAssignableFrom(rawType)) {
            return ImmutableMultiset::copyOf;
        }
        return HashMultiset::create;
    }
    if (SortedSet.class.isAssignableFrom(rawType)) {
        if (ImmutableSortedSet.class.isAssignableFrom(rawType)) {
            return ImmutableSortedSet::copyOf;
        }
        return TreeSet::new;
    }
    if (EnumSet.class.isAssignableFrom(rawType)) {
        return (items) -> {
            if (!(type instanceof ParameterizedType)) {
                throw new IllegalArgumentException("Invalid EnumSet type: " + type.toString());
            }
            Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0];
            if (!(elementType instanceof Class)) {
                throw new IllegalArgumentException("Invalid EnumSet type: " + type.toString());
            }
            return EnumSet.copyOf((Collection) items);
        };
    }
    if (Set.class.isAssignableFrom(rawType)) {
        if (ImmutableSet.class.isAssignableFrom(rawType)) {
            return ImmutableSet::copyOf;
        }
        return LinkedHashSet::new;
    }
    if (Queue.class.isAssignableFrom(rawType)) {
        return ArrayDeque::new;
    }
    if (ImmutableList.class.isAssignableFrom(rawType)) {
        return ImmutableList::copyOf;
    }
    return ArrayList::new;
}
Also used : UnsafeAllocator(org.terasology.reflection.reflect.internal.UnsafeAllocator) Arrays(java.util.Arrays) Array(java.lang.reflect.Array) SortedSet(java.util.SortedSet) Multiset(com.google.common.collect.Multiset) TypeInfo(org.terasology.reflection.TypeInfo) HashMap(java.util.HashMap) Constructor(java.lang.reflect.Constructor) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) ImmutableList(com.google.common.collect.ImmutableList) HashMultiset(com.google.common.collect.HashMultiset) ImmutableMultiset(com.google.common.collect.ImmutableMultiset) Map(java.util.Map) InstanceCreator(org.terasology.persistence.typeHandling.InstanceCreator) EnumSet(java.util.EnumSet) LinkedHashSet(java.util.LinkedHashSet) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) PrivilegedAction(java.security.PrivilegedAction) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) ParameterizedType(java.lang.reflect.ParameterizedType) TreeMap(java.util.TreeMap) Type(java.lang.reflect.Type) Queue(java.util.Queue) AccessController(java.security.AccessController) ArrayDeque(java.util.ArrayDeque) Comparator(java.util.Comparator) Collections(java.util.Collections) SortedMap(java.util.SortedMap) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Collection(java.util.Collection)

Aggregations

HashMultiset (com.google.common.collect.HashMultiset)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMultiset (com.google.common.collect.ImmutableMultiset)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1 Multiset (com.google.common.collect.Multiset)1 Array (java.lang.reflect.Array)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 AccessController (java.security.AccessController)1 PrivilegedAction (java.security.PrivilegedAction)1 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 EnumSet (java.util.EnumSet)1