Class BeaconIdentifier

java.lang.Object
com.irurueta.navigation.indoor.BeaconIdentifier
All Implemented Interfaces:
Serializable, Comparable<BeaconIdentifier>

public class BeaconIdentifier extends Object implements Comparable<BeaconIdentifier>, Serializable
Encapsulates a beacon identifier of arbitrary byte length. It can encapsulate an identifier that is a 16-byte UUID, or an integer. Based on: https://github.com/AltBeacon/android-beacon-library/blob/master/src/main/java/org/altbeacon/beacon/Identifier.java
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final Pattern
    Parses beacon identifiers in decimal format.
    private static final char[]
    Contains digits to represent this instance in hexadecimal format.
    private static final Pattern
    Parses beacon identifiers in hexadecimal format.
    private static final Pattern
    Parses beacon identifiers in hexadecimal format without prefix.
    private static final int
    Maximum allowed identifier value from an integer.
    private static final Pattern
    Parses beacon identifiers in UUID format.
    private byte[]
    Internal value holding a beacon identifier as a byte array.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Empty constructor to prevent deserialization issues.
    protected
    BeaconIdentifier(byte[] value)
    Creates a nw instance of a beacon identifier.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Compares two identifiers.
    boolean
    equals(Object that)
    Returns whether both identifiers contain equal value.
    fromBytes(byte[] bytes, int start, int end, boolean littleEndian)
    Creates an identifier from the specified byte array.
    fromInt(int intValue)
    Creates an identifier backed by a two byte array (big endian).
    fromLong(long longValue, int desiredByteLength)
    Creates an identifier backed by an array of length desiredByteLength.
    fromUuid(UUID uuid)
    Transforms a UUID into an identifier.
    int
    Returns the byte length of this identifier.
    int
    Computes hash code for this instance.
    parse(String stringValue)
    Takes the passed string and tries to figure out what format it is in.
    parse(String stringValue, int desiredByteLength)
    Variant of the parse method that allows specifying the byte length of the identifier.
    private static BeaconIdentifier
    parseHex(String identifierString, int desiredByteLength)
    Parses a string containing a beacon identifier in hexadecimal format.
    private static void
    reverseArray(byte[] bytes)
    Reverses provided array.
    byte[]
    Gives you the byte array backing this identifier.
    byte[]
    Converts identifier to a byte array.
    Represents the value as a hexadecimal String.
    int
    Represents the value as an int.
    Represents the value as a String.
    Gives you the identifier as a UUID if possible.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • HEX_PATTERN

      private static final Pattern HEX_PATTERN
      Parses beacon identifiers in hexadecimal format.
    • HEX_PATTERN_NO_PREFIX

      private static final Pattern HEX_PATTERN_NO_PREFIX
      Parses beacon identifiers in hexadecimal format without prefix.
    • DECIMAL_PATTERN

      private static final Pattern DECIMAL_PATTERN
      Parses beacon identifiers in decimal format.
    • UUID_PATTERN

      private static final Pattern UUID_PATTERN
      Parses beacon identifiers in UUID format.
    • MAX_INTEGER

      private static final int MAX_INTEGER
      Maximum allowed identifier value from an integer.
      See Also:
    • HEX_DIGITS

      private static final char[] HEX_DIGITS
      Contains digits to represent this instance in hexadecimal format.
    • value

      private byte[] value
      Internal value holding a beacon identifier as a byte array.
  • Constructor Details

    • BeaconIdentifier

      protected BeaconIdentifier()
      Empty constructor to prevent deserialization issues.
    • BeaconIdentifier

      protected BeaconIdentifier(byte[] value)
      Creates a nw instance of a beacon identifier.
      Parameters:
      value - value to use.
      Throws:
      NullPointerException - if provided value is null.
  • Method Details

    • parse

      public static BeaconIdentifier parse(String stringValue)
      Takes the passed string and tries to figure out what format it is in. Then turns the string into plain bytes and constructs an identifier.

      This method parses UUIDs without dashes for compatibility (although this is not a standard behaviour).

      Allowed formats:

      • UUID: 2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 (16 bytes)
      • Hexadecimal: 0x000000000003 (variable length)
      • Decimal: 1337 (2 bytes)
      Parameters:
      stringValue - string to be parsed.
      Returns:
      an identifier representing the specified value.
      Throws:
      NullPointerException - if string value is null.
      IllegalArgumentException - if parsing fails for some other reason (invalid format, etc.).
      See Also:
    • parse

      public static BeaconIdentifier parse(String stringValue, int desiredByteLength)
      Variant of the parse method that allows specifying the byte length of the identifier.
      Parameters:
      stringValue - value to be parsed.
      desiredByteLength - requested number of bytes to hold the identifier or -1 if not specified.
      Returns:
      the parsed identifier.
      Throws:
      NullPointerException - if string value is null.
      IllegalArgumentException - if parsing fails for some other reason (invalid format, etc.).
    • fromLong

      public static BeaconIdentifier fromLong(long longValue, int desiredByteLength)
      Creates an identifier backed by an array of length desiredByteLength.
      Parameters:
      longValue - a long to put into the identifier.
      desiredByteLength - how many bytes to make the identifier.
      Returns:
      the parsed identifier.
      Throws:
      IllegalArgumentException - if desired number of bytes is negative.
    • fromInt

      public static BeaconIdentifier fromInt(int intValue)
      Creates an identifier backed by a two byte array (big endian).
      Parameters:
      intValue - an integer between 0 and 65535 (inclusive).
      Returns:
      an identifier with the specified value.
      Throws:
      IllegalArgumentException - if provided value is out of valid range (from 0 to 65535).
    • fromBytes

      public static BeaconIdentifier fromBytes(byte[] bytes, int start, int end, boolean littleEndian)
      Creates an identifier from the specified byte array.
      Parameters:
      bytes - array to copy from.
      start - the start index, inclusive.
      end - the end index, exclusive.
      littleEndian - whether the bytes are ordered in little endian.
      Returns:
      a new identifier.
      Throws:
      NullPointerException - if bytes is null.
      ArrayIndexOutOfBoundsException - if start or end are outside the bounds of the array.
      IllegalArgumentException - start is larger than end.
    • fromUuid

      public static BeaconIdentifier fromUuid(UUID uuid)
      Transforms a UUID into an identifier. No mangling with strings, only the underlying bytes of the UUID are used so this is fast and stable.
      Parameters:
      uuid - UUID to create identifier from.
      Returns:
      a new identifier.
    • toString

      public String toString()
      Represents the value as a String. The output varies based on the length of the value.
      • When the value is 2 bytes long: decimal, for example 6536.
      • When the value is 16 bytes long: uuid, for example 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6
      • Else: hexadecimal prefixed with 0x, for example 0x0012ab
      Overrides:
      toString in class Object
      Returns:
      string representation of the current value.
    • toInt

      public int toInt()
      Represents the value as an int.
      Returns:
      value represented as int.
      Throws:
      UnsupportedOperationException - when value length is longer than 2.
    • toByteArrayOfSpecifiedEndianness

      public byte[] toByteArrayOfSpecifiedEndianness(boolean bigEndian)
      Converts identifier to a byte array.
      Parameters:
      bigEndian - true if bytes are MSB first.
      Returns:
      a new byte array with a copy of the value.
    • getByteCount

      public int getByteCount()
      Returns the byte length of this identifier.
      Returns:
      length of identifier.
    • toHexString

      public String toHexString()
      Represents the value as a hexadecimal String. The String is prefixed with 0x. For example 0x0034ab.
      Returns:
      value as hexadecimal String.
    • toUuid

      public UUID toUuid()
      Gives you the identifier as a UUID if possible.
      Returns:
      the identifier as a UUID.
      Throws:
      UnsupportedOperationException - if conversion to UUID fails.
    • toByteArray

      public byte[] toByteArray()
      Gives you the byte array backing this identifier. Note that identifiers are immutable, so changing that the returned array will not result in a changed identifier.
      Returns:
      a deep copy of the data backing this identifier.
    • hashCode

      public int hashCode()
      Computes hash code for this instance.
      Overrides:
      hashCode in class Object
      Returns:
      this instance hash code.
    • equals

      public boolean equals(Object that)
      Returns whether both identifiers contain equal value. This is the case when the value is the same and has the same length.
      Overrides:
      equals in class Object
      Parameters:
      that - object to compare to.
      Returns:
      whether that equals this.
    • compareTo

      public int compareTo(BeaconIdentifier that)
      Compares two identifiers. When the identifiers don't have the same length, the identifier having the shortest array is considered smaller than the other.
      Specified by:
      compareTo in interface Comparable<BeaconIdentifier>
      Parameters:
      that - the other identifier.
      Returns:
      0 if both identifiers are equal. Otherwise, returns -1 or 1 depending on which is bigger than th other.
      See Also:
    • reverseArray

      private static void reverseArray(byte[] bytes)
      Reverses provided array.
      Parameters:
      bytes - array to be reversed.
    • parseHex

      private static BeaconIdentifier parseHex(String identifierString, int desiredByteLength)
      Parses a string containing a beacon identifier in hexadecimal format.
      Parameters:
      identifierString - string to be parsed.
      desiredByteLength - length of byte array to create to hold provided value.
      Returns:
      the parsed identifier.