Package com.irurueta.navigation.indoor
Class BeaconIdentifier
java.lang.Object
com.irurueta.navigation.indoor.BeaconIdentifier
- All Implemented Interfaces:
Serializable,Comparable<BeaconIdentifier>
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
FieldsModifier and TypeFieldDescriptionprivate static final PatternParses beacon identifiers in decimal format.private static final char[]Contains digits to represent this instance in hexadecimal format.private static final PatternParses beacon identifiers in hexadecimal format.private static final PatternParses beacon identifiers in hexadecimal format without prefix.private static final intMaximum allowed identifier value from an integer.private static final PatternParses beacon identifiers in UUID format.private byte[]Internal value holding a beacon identifier as a byte array. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedEmpty constructor to prevent deserialization issues.protectedBeaconIdentifier(byte[] value) Creates a nw instance of a beacon identifier. -
Method Summary
Modifier and TypeMethodDescriptionintcompareTo(BeaconIdentifier that) Compares two identifiers.booleanReturns whether both identifiers contain equal value.static BeaconIdentifierfromBytes(byte[] bytes, int start, int end, boolean littleEndian) Creates an identifier from the specified byte array.static BeaconIdentifierfromInt(int intValue) Creates an identifier backed by a two byte array (big endian).static BeaconIdentifierfromLong(long longValue, int desiredByteLength) Creates an identifier backed by an array of length desiredByteLength.static BeaconIdentifierTransforms aUUIDinto an identifier.intReturns the byte length of this identifier.inthashCode()Computes hash code for this instance.static BeaconIdentifierTakes the passed string and tries to figure out what format it is in.static BeaconIdentifierVariant of the parse method that allows specifying the byte length of the identifier.private static BeaconIdentifierParses a string containing a beacon identifier in hexadecimal format.private static voidreverseArray(byte[] bytes) Reverses provided array.byte[]Gives you the byte array backing this identifier.byte[]toByteArrayOfSpecifiedEndianness(boolean bigEndian) Converts identifier to a byte array.Represents the value as a hexadecimal String.inttoInt()Represents the value as anint.toString()Represents the value as a String.toUuid()Gives you the identifier as a UUID if possible.
-
Field Details
-
HEX_PATTERN
Parses beacon identifiers in hexadecimal format. -
HEX_PATTERN_NO_PREFIX
Parses beacon identifiers in hexadecimal format without prefix. -
DECIMAL_PATTERN
Parses beacon identifiers in decimal format. -
UUID_PATTERN
Parses beacon identifiers in UUID format. -
MAX_INTEGER
private static final int MAX_INTEGERMaximum allowed identifier value from an integer.- See Also:
-
HEX_DIGITS
private static final char[] HEX_DIGITSContains digits to represent this instance in hexadecimal format. -
value
private byte[] valueInternal 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
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
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
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
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
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
Transforms aUUIDinto 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
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
public int toInt()Represents the value as anint.- 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
Represents the value as a hexadecimal String. The String is prefixed with0x. For example 0x0034ab.- Returns:
- value as hexadecimal String.
-
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. -
equals
Returns whether both identifiers contain equal value. This is the case when the value is the same and has the same length. -
reverseArray
private static void reverseArray(byte[] bytes) Reverses provided array.- Parameters:
bytes- array to be reversed.
-
parseHex
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.
-