public final class Suid extends Number implements CharSequence, Comparable<Suid>
The bits are distributed over the 64-bit long as depicted below:
HIGH INT LOW INT
________________________________________________________________________________________________
| | |
| 0000 0000 | 000b bbbb | bbbb bbbb | bbbb bbbb | bbbb bbbb | bbbb bbbb | bbbb bbbb | biii iiss |
|_______________________________________________|_______________________________________________|
0 = 11 reserved bits
b = 46 block bits
i = 5 ID bits
s = 2 shard bits
The first 11 bits are reserved and always set to 0. The next 46 bits are used for
the block number. These are handed out by a centralized server. Then there are 5 ID
bits which are to be filled in by the generator. The last 2 bits are reserved for the shard
ID. To prevent a single point of failure, at most 4 individual hosts can be handing out ID's for
a certain domain, each with their own shard ID.
To make the String representation of Suids both short and easily human-readable and
write-able, base-36 encoding is used. Using only lowercase makes suids easy for humans to read,
write and pronounce.
| Modifier and Type | Class and Description |
|---|---|
static class |
Suid.Deserializer
Deserializes from JSON
|
static class |
Suid.Serializer
Serializes to JSON
|
static class |
Suid.SuidConverter |
| Modifier and Type | Field and Description |
|---|---|
static String |
ALPHABET
Alphabet used when converting suid to string
|
static int |
COUNT_BLOCK
Number of block bits
|
static int |
COUNT_ID
Number of ID bits
|
static int |
COUNT_RESERVED
Number of reserved bits
|
static int |
COUNT_SHARD
Number of shard bits
|
static int |
IDSIZE
The number of ID's available in each block.
|
static long |
MASK_BLOCK
Mask that singles out the block bits
|
static long |
MASK_ID
Mask that singles out the ID bits
|
static long |
MASK_RESERVED
Mask that singles out the reserved bits
|
static long |
MASK_SHARD
Mask that singles out the shard bits
|
static Suid |
NULL
Convenient constant for a Suid with a value of
0L. |
static int |
OFFSET_BLOCK
Offset of block bits within suid (from LSB)
|
static int |
OFFSET_ID
Offset of ID bits within suid (from LSB)
|
static String |
PREFIX
Prefix used when serializing to/from JSON.
|
static int |
SHARDSIZE
The number of shards available
|
| Modifier | Constructor and Description |
|---|---|
protected |
Suid(String value)
Constructor needed by Jackson.
|
| Modifier and Type | Method and Description |
|---|---|
char |
charAt(int index)
Returns the char present at the given
index in the base-36 string representation of this suid. |
int |
compareTo(Suid obj) |
double |
doubleValue() |
boolean |
equals(Object obj) |
float |
floatValue() |
long |
getBlock()
Gets the block number.
|
int |
getId()
Gets the generated ID.
|
byte |
getShard()
Gets the shard ID.
|
int |
hashCode() |
int |
intValue() |
int |
length()
Returns the length of the string representation of this suid.
|
long |
longValue()
Returns this suid's underlying value.
|
static boolean |
looksValid(String value)
Indicates whether the given
value looks like a valid suid string. |
static boolean |
looksValidJSON(String json)
Indicates whether the given
json string looks like a valid JSON representation of a suid. |
CharSequence |
subSequence(int start,
int end) |
String |
toJSON()
Serializes this suid to JSON.
|
Long |
toLong()
Converts this Suid to a Java Long.
|
static List<Long> |
toLong(List<Suid> ids) |
String |
toString()
Returns a base-36 string representation of this suid.
|
static List<Suid> |
valueOf(List<Long> ids) |
static Suid |
valueOf(long value)
Creates a suid based on the given long
value. |
static Suid |
valueOf(long block,
int id,
byte shard)
Creates a suid based on the given
block number, generator id and shard id. |
static Suid |
valueOf(String string)
Creates a suid based on the given
string. |
static Suid |
valueOfJSON(String json)
Deserializes the given JSON string into a suid, if possible.
|
byteValue, shortValueclone, finalize, getClass, notify, notifyAll, wait, wait, waitchars, codePointspublic static final Suid NULL
0L.public static final String PREFIX
public static final int IDSIZE
public static final int SHARDSIZE
public static final long MASK_RESERVED
public static final long MASK_BLOCK
public static final long MASK_ID
public static final long MASK_SHARD
public static final int COUNT_RESERVED
public static final int COUNT_BLOCK
public static final int COUNT_ID
public static final int COUNT_SHARD
public static final int OFFSET_BLOCK
public static final int OFFSET_ID
public static final String ALPHABET
protected Suid(String value)
Use the static valueOf methods to instantiate a suid.
value - The value of the new suid as a (possibly JSON) string, or null.public static Suid valueOf(long value)
value.value - The long valuenull.public static Suid valueOf(long block, int id, byte shard)
block number, generator id and shard id.block - The block numberid - The generator ID bitsshard - The shard IDnull.public static Suid valueOf(String string)
string.string - The base36 stringnull if string was null.public String toString()
toString in interface CharSequencetoString in class Objectpublic long longValue()
public float floatValue()
floatValue in class Numberpublic double doubleValue()
doubleValue in class Numberpublic Long toLong()
null.public long getBlock()
public byte getShard()
0 .. 3).public int getId()
0 .. 255).public int length()
Equivalent to toString().length().
length in interface CharSequencepublic char charAt(int index)
index in the base-36 string representation of this suid.
Equivalent to toString().charAt(index).
charAt in interface CharSequencepublic CharSequence subSequence(int start, int end)
subSequence in interface CharSequencepublic int compareTo(Suid obj)
compareTo in interface Comparable<Suid>public String toJSON()
This method returns a JSON string of the form "Suid:xxxxx" where
xxxxx is the suid's string representation. E.G: "Suid:14shd".
null.PREFIX,
valueOfJSON(String),
Suid.Serializer,
Suid.Deserializerpublic static boolean looksValid(String value)
value looks like a valid suid string.
If this method returns true, this only indicates that it *might*
be valid. There are no guarantees.
value - The value, may be null, in which case this method returns false.true if it looks valid, false otherwise.valueOf(String)public static boolean looksValidJSON(String json)
json string looks like a valid JSON representation of a suid.
If this method returns true, this only indicates that the JSON *might* be valid as a suid.
There are no guarantees.
json - The json string, may be null in which case this method returns false.null.looksValid(java.lang.String)public static Suid valueOfJSON(String json)
This method checks whether the given json string
looks valid. If it does, it's deserialized into a suid and returned.
If it does not look valid, null is returned instead.
null.looksValidJSON(String),
toJSON(),
PREFIX,
Suid.DeserializerCopyright © 2015. All rights reserved.