Package no.mnemonic.commons.utilities
Class FlagUtils
java.lang.Object
no.mnemonic.commons.utilities.FlagUtils
Various bit operations on int/long flag bits.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
changeBit
(int flags, int bit, boolean set) Set or unset a bit or bit combination on bitmapstatic long
changeBit
(long flags, long bit, boolean set) Set or unset a bit or bit combination on bitmapgetFlagBitPositions
(long flag) Find out for long flag value which bit positions has been assigned 1static boolean
isSet
(int flags, int flag) Check if a bit or bit combination is setstatic boolean
isSet
(long flags, long flag) Check if a bit or bit combination is setstatic long
or
(Collection<Long> flags) Combine a set of bitmapsstatic 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 untouchedstatic long
protectedUnset
(long oldFlags, long unsetFlags, long protectedFlags) Unsets the unsetFlags in oldFlags, but leaves all the protected bits untouchedstatic int
setBit
(int flags, int bit) Set a bit or bit combination on bitmapstatic long
setBit
(long flags, long bit) Set a bit or bit combination on bitmapstatic long
setFlagByBitPositions
(long initFlag, Set<Short> positions) Construct flag base on initial flag and bit positions to be assigned with 1split
(int flags) Split bit flags into a set of single bitssplit
(long flags) Split bit flags into a set of single bitsstatic int
unsetBit
(int flags, int bit) Unset a bit or bit combination from bitmapstatic long
unsetBit
(long flags, long bit) Unset a bit or bit combination from bitmap
-
Constructor Details
-
FlagUtils
public FlagUtils()
-
-
Method Details
-
protect
public static long protect(long flags, long protectedFlags) - Parameters:
flags
- flags to setprotectedFlags
- 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 flagssetFlags
- flag bits to setprotectedFlags
- 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 flagsunsetFlags
- flag bits to setprotectedFlags
- 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 testflag
- 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 testflag
- 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 changebit
- 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 changebit
- 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 changebit
- 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 changebit
- 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 changebit
- bits to changeset
- 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 changebit
- bits to changeset
- if true, set the bits, if not unset them- Returns:
- updated bitmap with specified bits updated
- See Also:
-
or
Combine a set of bitmaps- Parameters:
flags
- set of bitmaps- Returns:
- a bitmap created from ORing together all the bitmaps
-
split
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
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
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
Construct flag base on initial flag and bit positions to be assigned with 1- Parameters:
initFlag
- initial flagpositions
- set of bit positions of flag suppose to be assigned with 1- Returns:
- new flag after assigned 1 to bit positions on initial flag
-