Class FlagUtils

java.lang.Object
no.mnemonic.commons.utilities.FlagUtils

public class FlagUtils extends Object
Various bit operations on int/long flag bits.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    changeBit(int flags, int bit, boolean set)
    Set or unset a bit or bit combination on bitmap
    static long
    changeBit(long flags, long bit, boolean set)
    Set or unset a bit or bit combination on bitmap
    static Set<Short>
    Find out for long flag value which bit positions has been assigned 1
    static boolean
    isSet(int flags, int flag)
    Check if a bit or bit combination is set
    static boolean
    isSet(long flags, long flag)
    Check if a bit or bit combination is set
    static long
    or(Collection<Long> flags)
    Combine a set of bitmaps
    static long
    protect(long flags, long protectedFlags)
     
    static long
    protectedSet(long oldFlags, long setFlags, long protectedFlags)
    Sets the setFlags in oldFlags, but leaves all the protected bits untouched
    static long
    protectedUnset(long oldFlags, long unsetFlags, long protectedFlags)
    Unsets the unsetFlags in oldFlags, but leaves all the protected bits untouched
    static int
    setBit(int flags, int bit)
    Set a bit or bit combination on bitmap
    static long
    setBit(long flags, long bit)
    Set a bit or bit combination on bitmap
    static long
    setFlagByBitPositions(long initFlag, Set<Short> positions)
    Construct flag base on initial flag and bit positions to be assigned with 1
    static Set<Integer>
    split(int flags)
    Split bit flags into a set of single bits
    static Set<Long>
    split(long flags)
    Split bit flags into a set of single bits
    static int
    unsetBit(int flags, int bit)
    Unset a bit or bit combination from bitmap
    static long
    unsetBit(long flags, long bit)
    Unset a bit or bit combination from bitmap

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • FlagUtils

      public FlagUtils()
  • Method Details

    • protect

      public static long protect(long flags, long protectedFlags)
      Parameters:
      flags - flags to set
      protectedFlags - bits to leave unset
      Returns:
      Returns the flags, but with protected bits unset
    • protectedSet

      public static long protectedSet(long oldFlags, long setFlags, long protectedFlags)
      Sets the setFlags in oldFlags, but leaves all the protected bits untouched
      Parameters:
      oldFlags - existing flags
      setFlags - flag bits to set
      protectedFlags - all bits which should be protected
      Returns:
      oldFlags ORed with setFlags, except for protected bits, which are returned as set in oldFlags
    • protectedUnset

      public static long protectedUnset(long oldFlags, long unsetFlags, long protectedFlags)
      Unsets the unsetFlags in oldFlags, but leaves all the protected bits untouched
      Parameters:
      oldFlags - existing flags
      unsetFlags - flag bits to set
      protectedFlags - all bits which should be protected
      Returns:
      oldFlags ANDed with setFlags, except for protected bits, which are returned as set in oldFlags
    • isSet

      public static boolean isSet(long flags, long flag)
      Check if a bit or bit combination is set
      Parameters:
      flags - bitmap to test
      flag - flags to check
      Returns:
      true if all bits in flag are set in flags
    • isSet

      public static boolean isSet(int flags, int flag)
      Check if a bit or bit combination is set
      Parameters:
      flags - bitmap to test
      flag - bits to check
      Returns:
      true if all bits in flag are set in flags
    • unsetBit

      public static long unsetBit(long flags, long bit)
      Unset a bit or bit combination from bitmap
      Parameters:
      flags - bitmap to change
      bit - bits to unset
      Returns:
      updated bitmap with specified bits cleared
    • unsetBit

      public static int unsetBit(int flags, int bit)
      Unset a bit or bit combination from bitmap
      Parameters:
      flags - bitmap to change
      bit - bits to unset
      Returns:
      updated bitmap with specified bits cleared
    • setBit

      public static int setBit(int flags, int bit)
      Set a bit or bit combination on bitmap
      Parameters:
      flags - bitmap to change
      bit - bits to set
      Returns:
      updated bitmap with specified bits set
    • setBit

      public static long setBit(long flags, long bit)
      Set a bit or bit combination on bitmap
      Parameters:
      flags - bitmap to change
      bit - bits to set
      Returns:
      updated bitmap with specified bits set
    • changeBit

      public static int changeBit(int flags, int bit, boolean set)
      Set or unset a bit or bit combination on bitmap
      Parameters:
      flags - bitmap to change
      bit - bits to change
      set - if true, set the bits, if not unset them
      Returns:
      updated bitmap with specified bits updated
      See Also:
    • changeBit

      public static long changeBit(long flags, long bit, boolean set)
      Set or unset a bit or bit combination on bitmap
      Parameters:
      flags - bitmap to change
      bit - bits to change
      set - if true, set the bits, if not unset them
      Returns:
      updated bitmap with specified bits updated
      See Also:
    • or

      public static long or(Collection<Long> flags)
      Combine a set of bitmaps
      Parameters:
      flags - set of bitmaps
      Returns:
      a bitmap created from ORing together all the bitmaps
    • split

      public static Set<Long> split(long flags)
      Split bit flags into a set of single bits
      Parameters:
      flags - long with bits to split into flags
      Returns:
      a set of single bits, one for each set bit
    • split

      public static Set<Integer> split(int flags)
      Split bit flags into a set of single bits
      Parameters:
      flags - int with bits to split into flags
      Returns:
      a set of single bits, one for each set bit
    • getFlagBitPositions

      public static Set<Short> getFlagBitPositions(long flag)
      Find out for long flag value which bit positions has been assigned 1
      Parameters:
      flag - long flag
      Returns:
      set of bit positions which have values assigned
    • setFlagByBitPositions

      public static long setFlagByBitPositions(long initFlag, Set<Short> positions)
      Construct flag base on initial flag and bit positions to be assigned with 1
      Parameters:
      initFlag - initial flag
      positions - set of bit positions of flag suppose to be assigned with 1
      Returns:
      new flag after assigned 1 to bit positions on initial flag