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

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

There is a newer version: 15.1.0.Dev04
Show newest version
package org.infinispan.commons.util;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.EnumSet;

/**
 * Utilities method to Enums.
 *
 * @author Pedro Ruivo
 * @since 8.2
 */
public class EnumUtil {

   private EnumUtil() {
   }

   public static final long EMPTY_BIT_SET = 0L;

   public static > long bitSetOf(Collection enums) {
      if (enums == null || enums.isEmpty()) {
         return EMPTY_BIT_SET;
      }
      long flagBitSet = EMPTY_BIT_SET;
      for (Enum f : enums) {
         flagBitSet |= bitSetOf(f);
      }
      return flagBitSet;
   }

   public static long bitSetOf(Enum first) {
      return 1L << first.ordinal();
   }

   public static long bitSetOf(Enum first, Enum second) {
      return bitSetOf(first) | bitSetOf(second);
   }

   public static long bitSetOf(Enum first, Enum second, Enum... remaining) {
      long bitSet = bitSetOf(first, second);
      for (Enum f : remaining) {
         bitSet |= bitSetOf(f);
      }
      return bitSet;
   }

   public static long bitSetOf(Enum[] flags) {
      long bitSet = EMPTY_BIT_SET;
      for (Enum flag : flags) {
         bitSet |= bitSetOf(flag);
      }
      return bitSet;
   }

   public static > EnumSet enumSetOf(long bitSet, Class eClass) {
      if (bitSet == EMPTY_BIT_SET) {
         return EnumSet.noneOf(eClass);
      }
      EnumSet flagSet = EnumSet.noneOf(eClass);
      for (E f : eClass.getEnumConstants()) {
         if (hasEnum(bitSet, f)) {
            flagSet.add(f);
         }
      }
      return flagSet;
   }

   public static boolean hasEnum(long bitSet, Enum anEnum) {
      return (bitSet & bitSetOf(anEnum)) != 0;
   }

   public static long setEnum(long bitSet, Enum anEnum) {
      return bitSet | bitSetOf(anEnum);
   }

   public static > long setEnums(long bitSet, Collection enums) {
      if (enums == null || enums.isEmpty()) {
         return bitSet;
      }
      for (Enum f : enums) {
         bitSet |= bitSetOf(f);
      }
      return bitSet;
   }

   public static long unsetEnum(long bitSet, Enum anEnum) {
      return bitSet & ~bitSetOf(anEnum);
   }

   public static > String prettyPrintBitSet(long bitSet, Class eClass) {
      return enumSetOf(bitSet, eClass).toString();
   }

   public static long mergeBitSets(long bitSet1, long bitSet2) {
      return bitSet1 | bitSet2;
   }

   public static long diffBitSets(long bitSet1, long bitSet2) {
      return bitSet1 & ~bitSet2;
   }

   public static boolean containsAll(long bitSet, long testBitSet) {
      return (bitSet & testBitSet) == testBitSet;
   }

   public static boolean containsAny(long bitSet, long testBitSet) {
      return (bitSet & testBitSet) != 0;
   }

   public static boolean noneOf(long bitSet, long testBitSet) {
      return (bitSet & testBitSet) == 0;
   }

   public static int bitSetSize(long bitSet) {
      return Long.bitCount(bitSet);
   }

   public static > E[] enumArrayOf(long bitSet, Class eClass) {
      if (bitSet == EMPTY_BIT_SET) {
         return null;
      }

      E[] array = (E[]) Array.newInstance(eClass, bitSetSize(bitSet));
      int i = 0;
      for (E f : eClass.getEnumConstants()) {
         if (hasEnum(bitSet, f)) {
            array[i++] = f;
         }
      }
      return array;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy