Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.infinispan.commons.util;
  
 
 import java.util.Map;
 import java.util.Set;
A factory for ConcurrentMaps.

Author(s):
Manik Surtani
Galder ZamarreƱo
Since:
5.1
 
 public class CollectionFactory {
    
    private static final ConcurrentMapCreator MAP_CREATOR;
 
    private static interface ConcurrentMapCreator {
       <K, V> ConcurrentMap<K, V> createConcurrentMap();
       <K, V> ConcurrentMap<K, V> createConcurrentMap(int initialCapacity);
       <K, V> ConcurrentMap<K, V> createConcurrentMap(int initialCapacityint concurrencyLevel);
       <K, V> ConcurrentMap<K, V> createConcurrentParallelMap(int initialCapacityint concurrencyLevel);
       <K, V> ConcurrentMap<K, V> createConcurrentMap(int initialCapacityfloat loadFactorint concurrencyLevel);
    }
 
    private static class JdkConcurrentMapCreator implements ConcurrentMapCreator {
 
       @Override
       public <K, V> ConcurrentMap<K, V> createConcurrentMap() {
          return new ConcurrentHashMap<K, V>();
       }
 
       @Override
       public <K, V> ConcurrentMap<K, V> createConcurrentMap(int initialCapacity) {
          return new ConcurrentHashMap<K, V>(initialCapacity);
       }
 
       @Override
       public <K, V> ConcurrentMap<K, V> createConcurrentMap(int initialCapacityint concurrencyLevel) {
          return new ConcurrentHashMap<K, V>(initialCapacity, 0.75f, concurrencyLevel);
       }
 
       @Override
       public <K, V> ConcurrentMap<K, V> createConcurrentMap(int initialCapacityfloat loadFactorint concurrencyLevel) {
          return new ConcurrentHashMap<K, V>(initialCapacityloadFactorconcurrencyLevel);
       }
 
       @Override
       public <K, V> ConcurrentMap<K, V> createConcurrentParallelMap(int initialCapacityint concurrencyLevel) {
          // by the time we baseline on JDK8 this code will be either dropped or adjusted, 
          //for now we need to use ConcurrentParallelHashMapV8
          return new ConcurrentParallelHashMapV8<K, V>(initialCapacity, AnyEquivalence.getInstance(),
                AnyEquivalence.getInstance());
       }
    }
 
    private static class BackportedV8ConcurrentMapCreator implements ConcurrentMapCreator {
 
       @Override
       public <K, V> ConcurrentMap<K, V> createConcurrentMap() {
          return new EquivalentConcurrentHashMapV8<K, V>(
                AnyEquivalence.getInstance(), AnyEquivalence.getInstance());
       }
 
       @Override
       public <K, V> ConcurrentMap<K, V> createConcurrentMap(int initialCapacity) {
          return new EquivalentConcurrentHashMapV8<K, V>(initialCapacity,
                AnyEquivalence.getInstance(), AnyEquivalence.getInstance());
       }
 
       @Override
       public <K, V> ConcurrentMap<K, V> createConcurrentMap(int initialCapacityint concurrencyLevel) {
          return new EquivalentConcurrentHashMapV8<K, V>(initialCapacity, 0.75f,
                concurrencyLevel, AnyEquivalence.getInstance(), AnyEquivalence.getInstance());
       }
 
       @Override
       public <K, V> ConcurrentMap<K, V> createConcurrentMap(int initialCapacityfloat loadFactorint concurrencyLevel) {
          return new EquivalentConcurrentHashMapV8<K, V>(initialCapacityloadFactor,
                concurrencyLevel, AnyEquivalence.getInstance(), AnyEquivalence.getInstance());
       }
 
       @Override
       public <K, V> ConcurrentMap<K, V> createConcurrentParallelMap(int initialCapacityint concurrencyLevel) {
          return new ConcurrentParallelHashMapV8<K, V>(initialCapacity, 0.75f,
               concurrencyLevel, AnyEquivalence.getInstance(), AnyEquivalence.getInstance());
      }
   }
   
   static {
      boolean sunIncompatibleJvm;
      boolean jdk8;
      boolean allowExperimentalMap = Boolean.parseBoolean(System.getProperty("infinispan.unsafe.allow_jdk8_chm""true"));
      try {
         Class.forName("sun.misc.Unsafe");
         sunIncompatibleJvm = false;
      } catch (ClassNotFoundException e) {
         sunIncompatibleJvm = true;
      }
      
      try {
         Class.forName("java.util.concurrent.atomic.LongAdder");
         jdk8 = true;
      } catch (ClassNotFoundException e) {
         jdk8 = false;
      }
      if (jdk8 || sunIncompatibleJvm || !allowExperimentalMap)
          = new JdkConcurrentMapCreator();
      else
          = new BackportedV8ConcurrentMapCreator();
   }
   
   public static <K, V> ConcurrentMap<K, V> makeConcurrentMap() {
      return .createConcurrentMap();
   }
   public static <K, V> ConcurrentMap<K, V> makeConcurrentMap(int initCapacity) {
      return .createConcurrentMap(initCapacity);
   }
   public static <K, V> ConcurrentMap<K, V> makeConcurrentMap(int initCapacityint concurrencyLevel) {
      return .createConcurrentMap(initCapacityconcurrencyLevel);
   }
   
   public static <K, V> ConcurrentMap<K, V> makeConcurrentParallelMap(int initCapacityint concurrencyLevel) {
      return .createConcurrentParallelMap(initCapacityconcurrencyLevel);
   }
   public static <K, V> ConcurrentMap<K, V> makeConcurrentMap(int initCapacityfloat loadFactorint concurrencyLevel) {
      return .createConcurrentMap(initCapacityloadFactorconcurrencyLevel);
   }
   public static <K, V> ConcurrentMap<K, V> makeConcurrentMap(
         Equivalence<? super K> keyEqEquivalence<? super V> valueEq) {
      if (requiresEquivalent(keyEqvalueEq))
         return new EquivalentConcurrentHashMapV8<K, V>(keyEqvalueEq);
      else
         return .createConcurrentMap();
   }
   public static <K, V> ConcurrentMap<K, V> makeConcurrentMap(
         int initCapacityEquivalence<? super K> keyEqEquivalence<? super V> valueEq) {
      if (requiresEquivalent(keyEqvalueEq))
         return new EquivalentConcurrentHashMapV8<K, V>(initCapacitykeyEqvalueEq);
      else
         return .createConcurrentMap(initCapacity);
   }
   public static <K, V> ConcurrentMap<K, V> makeConcurrentMap(
         int initCapacityint concurrencyLevelEquivalence<? super K> keyEqEquivalence<? super V> valueEq) {
      if (requiresEquivalent(keyEqvalueEq))
         return new EquivalentConcurrentHashMapV8<K, V>(
               initCapacityconcurrencyLevelkeyEqvalueEq);
      else
         return .createConcurrentMap(initCapacityconcurrencyLevel);
   }
   
   public static <K, V> ConcurrentMap<K, V> makeConcurrentParallelMap(
         int initCapacityint concurrencyLevelEquivalence<? super K> keyEqEquivalence<? super V> valueEq) {
      if (requiresEquivalent(keyEqvalueEq))
         return new ConcurrentParallelHashMapV8<K, V>(
               initCapacityconcurrencyLevelkeyEqvalueEq);
      else
         return .createConcurrentParallelMap(initCapacityconcurrencyLevel);
   }
   public static <K, V> ConcurrentMap<K, V> makeConcurrentMap(
         int initCapacityfloat loadFactorint concurrencyLevel,
         Equivalence<? super K> keyEqEquivalence<? super V> valueEq) {
      if (requiresEquivalent(keyEqvalueEq))
         return new EquivalentConcurrentHashMapV8<K, V>(
               initCapacityloadFactorconcurrencyLevelkeyEqvalueEq);
      else
         return .createConcurrentMap(initCapacityloadFactorconcurrencyLevel);
   }
   public static <K, V> ConcurrentMap<K, V> makeBoundedConcurrentMap(int maxSize) {
      return new BoundedEquivalentConcurrentHashMapV8<K, V>(maxSize,
         AnyEquivalence.getInstance(), AnyEquivalence.getInstance());
   }
   public static <K, V> Map<K, V> makeMap(
         Equivalence<? super K> keyEqEquivalence<? super V> valueEq) {
      if (requiresEquivalent(keyEqvalueEq))
         return new EquivalentHashMap<K, V>(keyEqvalueEq);
      else
         return new HashMap<K, V>();
   }
   public static <K, V> Map<K, V> makeMap(
         int initialCapacityEquivalence<? super K> keyEqEquivalence<? super V> valueEq) {
      if (requiresEquivalent(keyEqvalueEq))
         return new EquivalentHashMap<K, V>(initialCapacitykeyEqvalueEq);
      else
         return new HashMap<K, V>(initialCapacity);
   }
   public static <K, V> Map<K, V> makeMap(
         Map<? extends K, ? extends V> entriesEquivalence<? super K> keyEqEquivalence<? super V> valueEq) {
      if (requiresEquivalent(keyEqvalueEq))
         return new EquivalentHashMap<K, V>(entrieskeyEqvalueEq);
      else
         return new HashMap<K, V>(entries);
   }
   public static <K, V> Map<K, V> makeLinkedMap(int initialCapacity,
         float loadFactorEquivalentLinkedHashMap.IterationOrder iterationOrder,
         Equivalence<? super K> keyEqEquivalence<? super V> valueEq) {
      if (requiresEquivalent(keyEqvalueEq))
         return new EquivalentLinkedHashMap<K, V>(initialCapacityloadFactoriterationOrderkeyEqvalueEq);
      else
         return new LinkedHashMap<K, V>(initialCapacityloadFactoriterationOrder.toJdkAccessOrder());
   }
   public static <T> Set<T> makeSet(Equivalence<? super T> entryEq) {
      if (requiresEquivalent(entryEq))
         return new EquivalentHashSet<T>(entryEq);
      else
         return new HashSet<T>();
   }
   public static <T> Set<T> makeSet(int initialCapacityEquivalence<? super T> entryEq) {
      if (requiresEquivalent(entryEq))
         return new EquivalentHashSet<T>(initialCapacityentryEq);
      else
         return new HashSet<T>(initialCapacity);
   }

   
Create a Set backed by the specified array.

Parameters:
entries the array by which the list will be backed
<T> type of elements
Returns:
a set view of the specified array
   public static <T> Set<T> makeSet(T... entries) {
      return new HashSet<T>(Arrays.asList(entries));
   }
   private static <K, V> boolean requiresEquivalent(
         Equivalence<K> keyEqEquivalence<V> valueEq) {
      AnyEquivalence<Objectinstance = AnyEquivalence.getInstance();
      return keyEq != instance || valueEq != instance;
   }
   private static <T> boolean requiresEquivalent(Equivalence<T> typeEq) {
      return typeEq != AnyEquivalence.getInstance();
   }
New to GrepCode? Check out our FAQ X