public class Uid extends Object implements Comparable<Uid>, Serializable
UUID
.
The main difference is the encoding which is optimized for brevity. In contrast to the hex-encoding used
by UUID
, the toString()
method uses standard
base64url
(RFC 4648). The difference to normal base64 is
that base64url replaces '+' by '-' and '/' by '_' in order to make the encoded string usable in URLs
without any escaping.
Important: The string-representation is case-sensitive!
Examples showing the difference of UUID
vs. Uid
:
WQL8yMHUQ4FhZrB0cLux5g WCRAGMeiz-2PPaKdmn-iww Jd6_KRqpMivfuxXO4JmwtQ 284tn0-92bIMRNV_4M53Tg 8b726260-f9f3-439b-bf21-615bb4b6731d 34fadc2b-5a58-4de8-b04c-6f315a6598cd 15c3f6cb-6275-4557-b24c-2cd57cd07a6d 11934d8c-d201-4a95-a714-e03ff48f5053 46875d87-01ef-4ece-98cf-5a96a5946ef7
A string-encoded UUID
always has a length 36 characters, while a Uid
always has a length
of 22 characters. In other words, the strings are 38.89% shorter.
Instances of this class are immutable.
Modifier and Type | Field and Description |
---|---|
static int |
LENGTH_BYTES
Gets the length of an
Uid in bytes . |
static int |
LENGTH_STRING
Gets the length of an
Uid in its String representation . |
Constructor and Description |
---|
Uid()
Creates a new random
Uid . |
Uid(byte[] bytes) |
Uid(long hi,
long lo)
Creates a new
Uid with the given value. |
Uid(String uidString)
Creates a new
Uid instance from the encoded value in uidString . |
Modifier and Type | Method and Description |
---|---|
int |
compareTo(Uid other) |
boolean |
equals(Object obj) |
int |
hashCode() |
byte[] |
toBytes() |
String |
toString()
Gets a base64url-encoded string-representation of this
Uid . |
static Uid |
valueOf(byte[] bytes)
Creates a new
Uid instance from the binary data in bytes . |
static Uid |
valueOf(String uidString)
Creates a new
Uid instance from the encoded value in uidString . |
public static final int LENGTH_BYTES
Uid
in bytes
.public static final int LENGTH_STRING
Uid
in its String representation
.public Uid()
Uid
.public Uid(long hi, long lo)
Uid
with the given value.
This constructor is equivalent to UUID(long, long)
.
hi
- the most significant bits of the new Uid
.lo
- the least significant bits of the new Uid
.public Uid(byte[] bytes)
public Uid(String uidString)
Uid
instance from the encoded value in uidString
.
This constructor is symmetric to the toString()
method: The output of toString()
can
be passed to this constructor to create a new instance with the same value as (and thus being
equal to) the first instance.
uidString
- the string-encoded value of the Uid. Must not be null
.toString()
public static final Uid valueOf(byte[] bytes)
Uid
instance from the binary data in bytes
.
If the given bytes
is null
or empty (array-length 0), this method returns null
.
Otherwise, it creates an instance using the Uid(byte[])
constructor.
bytes
- the raw binary data of the Uid. May be null
. If not null
,
its length must be exactly 0 (treated the same a null
) or 16.Uid
instance created from the given bytes
or null
, if the input
is null
or empty (array-length 0).public static final Uid valueOf(String uidString)
Uid
instance from the encoded value in uidString
.
If the given uidString
is null
or empty, this method returns null
.
Otherwise, it creates an instance using the Uid(String)
constructor.
uidString
- the string-encoded value of the Uid. May be null
.Uid
instance created from the given uidString
or null
, if the input
is null
or empty.public byte[] toBytes()
public String toString()
Uid
.
The string returned by this method can be passed to Uid(String)
to create a new equal
Uid
instance.
Important: The string-representation is case-sensitive!
Inherited documentation:
public int compareTo(Uid other)
compareTo
in interface Comparable<Uid>
Copyright © 2013–2019. All rights reserved.