All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.infinispan.commons.util.CollectionFactory Maven / Gradle / Ivy

There is a newer version: 9.1.7.Final
Show newest version
package org.infinispan.commons.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.infinispan.commons.equivalence.Equivalence;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

/**
 * A factory for ConcurrentMaps.
 *
 * @author Manik Surtani
 * @author Galder Zamarreño
 * @since 5.1
 */
public class CollectionFactory {
   private static final float DEFAULT_LOAD_FACTOR = 0.75f;

   public static int computeCapacity(int expectedSize) {
      return computeCapacity(expectedSize, DEFAULT_LOAD_FACTOR);
   }

   public static int computeCapacity(int expectedSize, float loadFactor) {
      return (int) (expectedSize / loadFactor + 1.0f);
   }


   public static  ConcurrentMap makeConcurrentMap() {
      return new ConcurrentHashMap<>();
   }

   public static  ConcurrentMap makeConcurrentMap(int initCapacity) {
      return new ConcurrentHashMap<>(initCapacity);
   }

   public static  ConcurrentMap makeConcurrentMap(int initCapacity, int concurrencyLevel) {
      return new ConcurrentHashMap<>(initCapacity, 0.75f, concurrencyLevel);
   }

   public static  ConcurrentMap makeConcurrentParallelMap(int initCapacity, int concurrencyLevel) {
      return new ConcurrentHashMap<>(initCapacity);
   }

   public static  ConcurrentMap makeConcurrentMap(int initCapacity, float loadFactor, int concurrencyLevel) {
      return new ConcurrentHashMap<>(initCapacity, loadFactor, concurrencyLevel);
   }

   public static  Map makeMap() {
      return new HashMap<>();
   }

   public static  Map makeMap(int initialCapacity) {
      return new HashMap<>(initialCapacity);
   }

   public static  Map makeMap(Map entries) {
      return new HashMap<>(entries);
   }

   public static  Map makeLinkedMap(int initialCapacity, float loadFactor, boolean accessOrder) {
      return new LinkedHashMap<>(initialCapacity, loadFactor, accessOrder);
   }

   public static  Set makeSet() {
      return new HashSet<>();
   }

   public static  Set makeSet(int initialCapacity) {
      return new HashSet<>(initialCapacity);
   }

   /**
    * @deprecated Since 9.0, please use {@link #makeConcurrentMap()} instead.
    */
   @Deprecated
   public static  ConcurrentMap makeConcurrentMap(
         Equivalence keyEq, Equivalence valueEq) {
      return makeConcurrentMap();
   }

   /**
    * @deprecated Since 9.0, please use {@link #makeConcurrentMap(int)} instead.
    */
   @Deprecated
   public static  ConcurrentMap makeConcurrentMap(
         int initCapacity, Equivalence keyEq, Equivalence valueEq) {
      return makeConcurrentMap(initCapacity);
   }

   /**
    * @deprecated Since 9.0, please use {@link #makeConcurrentMap(int, int)} instead.
    */
   @Deprecated
   public static  ConcurrentMap makeConcurrentMap(
         int initCapacity, int concurrencyLevel, Equivalence keyEq, Equivalence valueEq) {
      return makeConcurrentMap(initCapacity, concurrencyLevel);
   }

   /**
    * @deprecated Since 9.0, please use {@link #makeConcurrentParallelMap(int, int)} instead.
    */
   @Deprecated
   public static  ConcurrentMap makeConcurrentParallelMap(
         int initCapacity, int concurrencyLevel, Equivalence keyEq, Equivalence valueEq) {
      return makeConcurrentParallelMap(initCapacity, concurrencyLevel);
   }

   /**
    * @deprecated Since 9.0, please use {@link #makeConcurrentMap(int, float, int)} instead.
    */
   @Deprecated
   public static  ConcurrentMap makeConcurrentMap(
         int initCapacity, float loadFactor, int concurrencyLevel,
         Equivalence keyEq, Equivalence valueEq) {
      return makeConcurrentMap(initCapacity, loadFactor, concurrencyLevel);
   }

   public static  ConcurrentMap makeBoundedConcurrentMap(int maxSize) {
      Cache cache = Caffeine.newBuilder().maximumSize(maxSize).build();
      return cache.asMap();
   }

   /**
    * @deprecated Since 9.0, please use {@link #makeMap()} instead.
    */
   @Deprecated
   public static  Map makeMap(
         Equivalence keyEq, Equivalence valueEq) {
      return new HashMap<>();
   }

   /**
    * @deprecated Since 9.0, please use {@link #makeMap(int)} instead.
    */
   @Deprecated
   public static  Map makeMap(
         int initialCapacity, Equivalence keyEq, Equivalence valueEq) {
      return new HashMap<>(initialCapacity);
   }

   /**
    * @deprecated Since 9.0, please use {@link #makeMap(Map)} instead.
    */
   @Deprecated
   public static  Map makeMap(
         Map entries, Equivalence keyEq, Equivalence valueEq) {
      return new HashMap<>(entries);
   }

   /**
    * @deprecated Since 9.0, please use {@link #makeSet(int)} instead.
    */
   @Deprecated
   public static  Map makeLinkedMap(int initialCapacity,
         float loadFactor, boolean accessOrder,
         Equivalence keyEq, Equivalence valueEq) {
      return new LinkedHashMap<>(initialCapacity, loadFactor, accessOrder);
   }

   /**
    * @deprecated Since 9.0, please use {@link #makeSet()} instead.
    */
   @Deprecated
   public static  Set makeSet(Equivalence entryEq) {
      return new HashSet<>();
   }

   /**
    * @deprecated Since 9.0, please use {@link #makeSet(int)} instead.
    */
   @Deprecated
   public static  Set makeSet(int initialCapacity, Equivalence entryEq) {
      return new HashSet<>(initialCapacity);
   }

   /**
    * Create a Set backed by the specified array.
    *
    * @param entries the array by which the list will be backed
    * @param  type of elements
    * @return a set view of the specified array
    */
   @SafeVarargs
   public static  Set makeSet(T... entries) {
      return new HashSet<>(Arrays.asList(entries));
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy