-
- All Implemented Interfaces:
-
java.io.Serializable,java.lang.Comparable
public class Identifier implements Comparable<Identifier>, Serializable
Encapsulates a beacon identifier of arbitrary byte length. It can encapsulate an identifier that is a 16-byte UUID, or an integer.
Instances of this class are immutable, so those can be shared without problem between threads.
The value is internally this is stored as a byte array.
-
-
Constructor Summary
Constructors Constructor Description Identifier(Identifier identifier)Creates a new copy of the specified Identifier.
-
Method Summary
Modifier and Type Method Description static Identifierparse(String stringValue)Takes the passed string and tries to figure out what format it is in. static Identifierparse(String stringValue, int desiredByteLength)Variant of the parse method that allows specifying the byte length of the identifier. static IdentifierfromLong(long longValue, int desiredByteLength)Creates an Identifer backed by an array of length desiredByteLength static IdentifierfromInt(int intValue)Creates an Identifier backed by a two byte Array (big endian). static IdentifierfromBytes(Array<byte> bytes, int start, int end, boolean littleEndian)Creates an Identifier from the specified byte array. static IdentifierfromUuid(UUID uuid)Transforms a java.util.UUID into an Identifier. StringtoString()Represents the value as a String. inttoInt()Represents the value as an int.Array<byte>toByteArrayOfSpecifiedEndianness(boolean bigEndian)Converts identifier to a byte array intgetByteCount()Returns the byte length of this identifier. inthashCode()booleanequals(Object that)Returns whether both Identifiers contain equal value. StringtoHexString()Represents the value as a hexadecimal String. StringtoUuidString()Returns the value of this Identifier in UUID format. UUIDtoUuid()Gives you the Identifier as a UUID if possible. Array<byte>toByteArray()Gives you the byte array backing this Identifier. intcompareTo(Identifier that)Compares two identifiers. -
-
Constructor Detail
-
Identifier
Identifier(Identifier identifier)
Creates a new copy of the specified Identifier.- Parameters:
identifier- identifier to copy
-
-
Method Detail
-
parse
static Identifier 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. Known bug: This method happily parses UUIDs without dashes (normally invalid). Although the bug is left unfixed for backward compatibility, please check your UUIDs or even better, use fromUuid directly, which is safe. Allowed formats:
- UUID: 2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 (16 bytes)
- Hexadecimal: 0x000000000003 (variable length)
- Decimal: 1337 (2 bytes)
- Parameters:
stringValue- string to parse
-
parse
static Identifier parse(String stringValue, int desiredByteLength)
Variant of the parse method that allows specifying the byte length of the identifier.
-
fromLong
static Identifier fromLong(long longValue, int desiredByteLength)
Creates an Identifer backed by an array of length desiredByteLength
- Parameters:
longValue- a long to put into the identifierdesiredByteLength- how many bytes to make the identifier
-
fromInt
static Identifier fromInt(int intValue)
Creates an Identifier backed by a two byte Array (big endian).
- Parameters:
intValue- an integer between 0 and 65535 (inclusive)
-
fromBytes
static Identifier fromBytes(Array<byte> bytes, int start, int end, boolean littleEndian)
Creates an Identifier from the specified byte array.
- Parameters:
bytes- array to copy fromstart- the start index, inclusiveend- the end index, exclusivelittleEndian- whether the bytes are ordered in little endian
-
fromUuid
static Identifier fromUuid(UUID uuid)
Transforms a java.util.UUID into an Identifier. No mangling with strings, only the underlying bytes of the UUID are used so this is fast and stable.
-
toString
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
-
toInt
int toInt()
Represents the value as an
int.
-
toByteArrayOfSpecifiedEndianness
Array<byte> toByteArrayOfSpecifiedEndianness(boolean bigEndian)
Converts identifier to a byte array
- Parameters:
bigEndian- true if bytes are MSB first
-
getByteCount
int getByteCount()
Returns the byte length of this identifier.
-
hashCode
int hashCode()
-
equals
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
- Parameters:
that- object to compare to
-
toHexString
String toHexString()
Represents the value as a hexadecimal String. The String is prefixed with
0x. For example 0x0034ab
-
toUuidString
@Deprecated() String toUuidString()
Returns the value of this Identifier in UUID format. For example 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6
-
toByteArray
Array<byte> toByteArray()
Gives you the byte array backing this Identifier. Note that Identifiers are immutable, so changing the the returned array will not result in a changed Identifier.
-
compareTo
int compareTo(Identifier 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.
- Parameters:
that- the other identifier
-
-
-
-