public class Id
extends java.lang.Object
implements java.io.Serializable, java.lang.Comparable
String
or a byte array.Modifier and Type | Field and Description |
---|---|
static Id |
ZERO_ID
Convenience constant for the all-zero GUID.
|
Constructor and Description |
---|
Id(byte[] bytes)
Constructs an ID with an array of bytes representing its GUID value.
|
Id(java.lang.String id)
Constructs an ID from a string representing its GUID value.
|
Modifier and Type | Method and Description |
---|---|
static Id |
asIdOrNull(java.lang.String id)
Returns an Id if the passed
String is in GUID format. |
int |
compareTo(java.lang.Object arg)
Compares two IDs.
|
int |
compareTo(java.lang.Object arg,
DatabaseType dbType)
Compares two IDs as is done in a database search.
|
static Id |
createId()
Generates an ID with an IETF Version 4 UUID value.
|
static Id |
createSequentialId(DatabaseType dbType)
Generates an ID that has a time- and counter-based portion that is combined with a random portion in such
a way that consecutively created IDs are sequential (monotonic) per the natural ordering applied by the
underlying database.
|
boolean |
equals(java.lang.Object otherId)
Compares two IDs for logical equality.
|
byte |
getByte(int index)
Returns a single byte from this ID.
|
byte[] |
getBytes()
Returns a copy of this ID as an array of bytes.
|
int |
hashCode()
Returns the hashcode for this ID.
|
static boolean |
isId(java.lang.String value)
Determines if a
String is an ID or not. |
java.lang.String |
toString()
Returns the string representation of this ID.
|
public static final Id ZERO_ID
public Id(java.lang.String id)
{1234abcd-56ef-7a89-9fe8-7d65cd43ba21}
An exception is thrown if the input string does not conform to this format.id
- GUID value in string format from which to construct an ID.public Id(byte[] bytes)
bytes
- GUID value in byte array format from which to construct an ID.public static Id createId()
java.security.SecureRandom
.Id
object.public static Id createSequentialId(DatabaseType dbType)
dbType
- A DatabaseType
object that specifies the type of database.Id
object.public static Id asIdOrNull(java.lang.String id)
String
is in GUID format. A valid
string consists of optional whitespace, an optional left brace({), followed
by required 8 hexadecimal characters, a dash, 4 hexadecimal characters,
a dash, 4 hexadecimal characters, a dash, 4 hexadecimal characters, a dash,
and 12 hexadecimal characters, then finally an optional right brace(})
followed by whitespace. The braces must either both be present or both be omitted.
{1234abcd-56ef-7a89-9fe8-7d65cd43ba21}
If the input String
does not conform to this format, the
method returns null
.
This method provides a more efficient alternative to calling
isId(String)
followed conditionally by new Id(String)
.id
- GUID value in string format from which to construct an ID.Id
object or null.public int compareTo(java.lang.Object arg)
this
is less than arg
,
1 if this
is greater than arg
,
and 0 if the IDs are equal.
This comparison is consistent with
how IDs are compared in the DB2 and Oracle databases, but does not match how
MSSQL Server compares IDs. For MSSQL Server databases, use
compareTo(arg, dbType)
.
compareTo
in interface java.lang.Comparable
arg
- A Java Object
representing the ID against which to compare this ID.public int compareTo(java.lang.Object arg, DatabaseType dbType)
this
is less than arg
,
1 if this
is greater than arg
,
and 0 if the IDs are equal.
This comparison matches the way that the databases
compare IDs if done in a database search. dbType
may be null
if the comparison
algorithm does not need to match that of any particular database. Specifying null
for dbType
is the same as calling the single-parameter form of this method
(compareTo(arg)
).
arg
- The GUID against which to compare this GUID.dbType
- An instance of DatabaseType
; can be null
.public boolean equals(java.lang.Object otherId)
equals
in class java.lang.Object
otherId
- The ID against which to compare this ID.true
if otherId
is an Id
object and their
GUIDs are logically equal; otherwise, returns false
.public byte[] getBytes()
public byte getByte(int index)
index
- The index of the byte retrieve.public int hashCode()
hashCode
in class java.lang.Object
public static boolean isId(java.lang.String value)
String
is an ID or not.value
- The String
to verify.true
if the provided String
is in GUID format;
otherwise, returns false
.public java.lang.String toString()
toString
in class java.lang.Object
String
.© Copyright IBM Corporation 2006, 2015. All rights reserved.