Class FlagUtils

java.lang.Object
eu.getmangos.utils.FlagUtils

public class FlagUtils
extends Object
This class must help to manage flags that are using bits inside integer E.g: 2 = 0010, meaning that the second flag is set and all the others are not.
Author:
GetMangos
  • Constructor Summary

    Constructors 
    Constructor Description
    FlagUtils()  
  • Method Summary

    Modifier and Type Method Description
    static short getByte​(int flags, int pos)
    Provides the byte value at the position X, starting from the right.
    static short getByte​(long flags, int pos)
    Provides the byte value at the position X, starting from the right.
    static short getByte​(short flags, int pos)
    Provides the byte value at the position X, starting from the right.
    static boolean hasFlag​(byte field, byte flag)
    Indicates whether the field has the given flag.
    static boolean hasFlag​(int field, int flag)
    Indicates whether the field has the given flag.
    static boolean hasFlag​(long field, long flag)
    Indicates whether the field has the given flag.
    static boolean hasFlag​(short field, short flag)
    Indicates whether the field has the given flag.
    static boolean isSet​(byte field, int pos)
    Indicates whether the field has the given flag set.
    static boolean isSet​(int field, int pos)
    Indicates whether the field has the given flag set.
    static boolean isSet​(long field, int pos)
    Indicates whether the field has the given flag set.
    static boolean isSet​(short field, int pos)
    Indicates whether the field has the given flag set.
    static int setByte​(int field, int pos, short value)
    Set the byte value at the given position.
    static long setByte​(long field, int pos, short value)
    Set the byte value at the given position.
    static short setByte​(short field, int pos, short value)
    Set the byte value at the given position.
    static byte setFlag​(byte field, int pos, boolean value)
    Returns the value of the field with the flag at the position 'pos' set at the value 'value'.
    static int setFlag​(int field, int pos, boolean value)
    Returns the value of the field with the flag at the position 'pos' set at the value 'value'.
    static long setFlag​(long field, int pos, boolean value)
    Returns the value of the field with the flag at the position 'pos' set at the value 'value'.
    static short setFlag​(short field, int pos, boolean value)
    Returns the value of the field with the flag at the position 'pos' set at the value 'value'.

    Methods inherited from class java.lang.Object

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

  • Method Details

    • getByte

      public static short getByte​(short flags, int pos)
      Provides the byte value at the position X, starting from the right. E.g: The short 260 is represented as 00000000 00010100 in memory, 00010100 must be returned.
      Parameters:
      flags - The flags value from which we need to extract the byte.
      pos - The byte position to be extracted. This value must be included between 1 and 2.
      Returns:
      The corresponding byte value (as a short) or -1 if the position provided is incorrect.
    • setByte

      public static short setByte​(short field, int pos, short value)
      Set the byte value at the given position.
      Parameters:
      field - The original value.
      pos - The position of the byte to be updated.
      value - The new value for the given byte.
      Returns:
      A short (2-bytes size) primitive updated with the provided value
    • getByte

      public static short getByte​(int flags, int pos)
      Provides the byte value at the position X, starting from the right. E.g: The int 260 is represented as 00000000 00000000 00000000 00010100 in memory, 00010100 must be returned.
      Parameters:
      flags - The flags value from which we need to extract the byte.
      pos - The byte position to be extracted. This value must be included between 1 and 4.
      Returns:
      The corresponding byte value (as a short) or -1 if the position provided is incorrect.
    • setByte

      public static int setByte​(int field, int pos, short value)
      Set the byte value at the given position.
      Parameters:
      field - The original value.
      pos - The position of the byte to be updated.
      value - The new value for the given byte.
      Returns:
      An integer (4-bytes size) primitive updated with the provided value
    • getByte

      public static short getByte​(long flags, int pos)
      Provides the byte value at the position X, starting from the right.
      Parameters:
      flags - The flags value from which we need to extract the byte.
      pos - The byte position to be extracted. This value must be included between 1 and 8.
      Returns:
      The corresponding byte value (as a short) or -1 if the position provided is incorrect.
    • setByte

      public static long setByte​(long field, int pos, short value)
      Set the byte value at the given position.
      Parameters:
      field - The original value.
      pos - The position of the byte to be updated.
      value - The new value for the given byte.
      Returns:
      A long (8-bytes size) primitive updated with the provided value
    • hasFlag

      public static boolean hasFlag​(byte field, byte flag)
      Indicates whether the field has the given flag. E.g: The byte 00000100 has the 3rd flag set, which means that the flag value should be 8.
      Parameters:
      field - The byte value, represented as short for signed reasons, for which the flag needs to be compared. This value is truncated, which means that only the first byte is taken into account.
      flag - The flag to be found.
      Returns:
      A boolean value indicating whether the flag is set or not.
    • hasFlag

      public static boolean hasFlag​(short field, short flag)
      Indicates whether the field has the given flag. E.g: The byte 00000100 has the 3rd flag set, which means that the flag value should be 8.
      Parameters:
      field - The byte value, represented as short for signed reasons, for which the flag needs to be compared. This value is truncated, which means that only the first byte is taken into account.
      flag - The flag to be found.
      Returns:
      A boolean value indicating whether the flag is set or not.
    • hasFlag

      public static boolean hasFlag​(int field, int flag)
      Indicates whether the field has the given flag. E.g: The byte 00000100 has the 3rd flag set, which means that the flag value should be 8.
      Parameters:
      field - The byte value, represented as short for signed reasons, for which the flag needs to be compared. This value is truncated, which means that only the first byte is taken into account.
      flag - The flag to be found.
      Returns:
      A boolean value indicating whether the flag is set or not.
    • hasFlag

      public static boolean hasFlag​(long field, long flag)
      Indicates whether the field has the given flag. E.g: The byte 00000100 has the 3rd flag set, which means that the flag value should be 8.
      Parameters:
      field - The byte value, represented as short for signed reasons, for which the flag needs to be compared. This value is truncated, which means that only the first byte is taken into account.
      flag - The flag to be found.
      Returns:
      A boolean value indicating whether the flag is set or not.
    • isSet

      public static boolean isSet​(byte field, int pos)
      Indicates whether the field has the given flag set. E.g: The byte 00000100 has the 3rd flag set.
      Parameters:
      field - The byte value, represented as short for signed reasons, for which the flag needs to be compared. This value is truncated, which means that only the first byte is taken into account.
      pos - The position of the flag that has to be set.
      Returns:
      A boolean value indicating whether the flag is set or not.
    • isSet

      public static boolean isSet​(short field, int pos)
      Indicates whether the field has the given flag set. E.g: The byte 00000100 has the 3rd flag set.
      Parameters:
      field - The byte value, represented as short for signed reasons, for which the flag needs to be compared. This value is truncated, which means that only the first byte is taken into account.
      pos - The position of the flag that has to be set.
      Returns:
      A boolean value indicating whether the flag is set or not.
    • isSet

      public static boolean isSet​(int field, int pos)
      Indicates whether the field has the given flag set. E.g: The byte 00000100 has the 3rd flag set.
      Parameters:
      field - The byte value, represented as short for signed reasons, for which the flag needs to be compared. This value is truncated, which means that only the first byte is taken into account.
      pos - The position of the flag that has to be set.
      Returns:
      A boolean value indicating whether the flag is set or not.
    • isSet

      public static boolean isSet​(long field, int pos)
      Indicates whether the field has the given flag set. E.g: The byte 00000100 has the 3rd flag set.
      Parameters:
      field - The byte value, represented as short for signed reasons, for which the flag needs to be compared. This value is truncated, which means that only the first byte is taken into account.
      pos - The position of the flag that has to be set.
      Returns:
      A boolean value indicating whether the flag is set or not.
    • setFlag

      public static byte setFlag​(byte field, int pos, boolean value)
      Returns the value of the field with the flag at the position 'pos' set at the value 'value'.
      Parameters:
      field - The byte value for which the flag needs to be set/unset. This value is truncated, which means that only the first byte is taken into account.
      pos - The position of the flag to be set/unset. This value must be included between 1 or 8.
      value - The boolean flag indicating whether the flag must be set (=true) or unset (=false)
      Returns:
      The byte value, converted as short for signed reason, with the flag set/unset. -1 is returned if the position is incorrect.
    • setFlag

      public static short setFlag​(short field, int pos, boolean value)
      Returns the value of the field with the flag at the position 'pos' set at the value 'value'.
      Parameters:
      field - The byte value for which the flag needs to be set/unset. This value is truncated, which means that only the first byte is taken into account.
      pos - The position of the flag to be set/unset. This value must be included between 1 or 8.
      value - The boolean flag indicating whether the flag must be set (=true) or unset (=false)
      Returns:
      The byte value, converted as short for signed reason, with the flag set/unset. -1 is returned if the position is incorrect.
    • setFlag

      public static int setFlag​(int field, int pos, boolean value)
      Returns the value of the field with the flag at the position 'pos' set at the value 'value'.
      Parameters:
      field - The byte value for which the flag needs to be set/unset. This value is truncated, which means that only the first byte is taken into account.
      pos - The position of the flag to be set/unset. This value must be included between 1 or 16.
      value - The boolean flag indicating whether the flag must be set (=true) or unset (=false)
      Returns:
      The byte value, converted as short for signed reason, with the flag set/unset. -1 is returned if the position is incorrect.
    • setFlag

      public static long setFlag​(long field, int pos, boolean value)
      Returns the value of the field with the flag at the position 'pos' set at the value 'value'.
      Parameters:
      field - The byte value for which the flag needs to be set/unset. This value is truncated, which means that only the first byte is taken into account.
      pos - The position of the flag to be set/unset. This value must be included between 1 or 8.
      value - The boolean flag indicating whether the flag must be set (=true) or unset (=false)
      Returns:
      The byte value, converted as short for signed reason, with the flag set/unset. -1 is returned if the position is incorrect.