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 iiis |
|_______________________________________________|_______________________________________________|
0 = 11 reserved bits
b = 46 block bits
i = 6 ID bits
s = 1 shard bit
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 6 ID
bits which are to be filled in by the generator. The last bit is reserved for the shard
ID. To prevent a single point of failure, two separate hosts can be handing out ID's for a certain
domain, each with their own shard ID (0 or 1).
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
Converts Suid to/from it's database representation.
|
| Modifier and Type | Field and Description |
|---|---|
static String |
ALPHABET
Alphabet used when converting suid to string
|
static long |
BLOCK_SIZE
The number of blocks available.
|
static byte |
COUNT_BLOCK
Number of block bits
|
static byte |
COUNT_ID
Number of ID bits
|
static byte |
COUNT_RESERVED
Number of reserved bits
|
static byte |
COUNT_SHARD
Number of shard bits
|
static long |
IDSIZE
The number of IDs 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 byte |
OFFSET_BLOCK
Offset of block bits within suid (from LSB)
|
static byte |
OFFSET_ID
Offset of ID bits within suid (from LSB)
|
static byte |
OFFSET_RESERVED
Offset of reserved bits within suid (from LSB)
|
static byte |
OFFSET_SHARD
Offset of shard bit within suid (from LSB)
|
static long |
SHARDSIZE
The number of shards available
|
| Constructor and Description |
|---|
Suid(long value)
Creates a suid based on the given long
value. |
Suid(long block,
byte id,
byte shard)
Creates a suid based on the given
block, id and shard constituent parts. |
Suid(String value)
Creates a suid based on the given string
value. |
| 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 that)
Compares this suid to
that for order. |
double |
doubleValue()
Returns the value of this Suid as a double.
|
boolean |
equals(Object that)
Compares this suid to
that for equality. |
float |
floatValue()
Use
longValue() instead. |
long |
getBlock()
Gets the block bits.
|
byte |
getId()
Gets the ID bits.
|
byte |
getShard()
Gets the shard bits.
|
int |
hashCode() |
int |
intValue()
Use
longValue() instead. |
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. |
CharSequence |
subSequence(int start,
int end) |
Long |
toLong()
Converts this Suid to a Java Long.
|
static List<Long> |
toLong(List<Suid> ids)
Converts the given list of
ids to a list of longs. |
String |
toString()
Returns a base-36 string representation of this suid.
|
static List<Suid> |
valueOf(List<Long> ids)
Converts the given list of
ids to a list of Suids. |
byteValue, shortValueclone, finalize, getClass, notify, notifyAll, wait, wait, waitchars, codePointspublic static final String ALPHABET
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 byte COUNT_RESERVED
public static final byte COUNT_BLOCK
public static final byte COUNT_ID
public static final byte COUNT_SHARD
public static final byte OFFSET_RESERVED
public static final byte OFFSET_BLOCK
public static final byte OFFSET_ID
public static final byte OFFSET_SHARD
public static final long BLOCK_SIZE
public static final long IDSIZE
public static final long SHARDSIZE
public Suid(String value) throws NumberFormatException
value.value - The value of the new suid as a base-36 string, or null.NumberFormatException - When the supplied value can not be parsed as base-36.public Suid(long value)
value.value - The long value to set this Suid to.public Suid(long block,
byte id,
byte shard)
block, id and shard constituent parts.block - The block bits for the suid, in a long.id - The id bits for the suid, in a byte.shard - The shard bit for the suid, in a byte.public String toString()
toString in interface CharSequencetoString in class Objectpublic int intValue()
longValue() instead.public long longValue()
Suid's use a long as underlying value. Avoid using
intValue and floatValue as these perform
narrowing conversions. These methods are mainly there
to satisfy the Number interface.
public float floatValue()
longValue() instead.
If you must have a floating point number, use doubleValue which can
actually store all possible suids (they are limited to 53 bits for this purpose).
floatValue in class Numberpublic double doubleValue()
Although suids internally use longs to store the bits, since they
are limited to 53 bits, they can actually be represented as double as
well without loss of precision.
doubleValue in class Numberpublic Long toLong()
null.public long getBlock()
public byte getId()
0 .. 63).public byte getShard()
0 .. 1).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 boolean equals(Object that)
that for equality.public int compareTo(Suid that) throws NullPointerException
that for order.compareTo in interface Comparable<Suid>that - The suid to compare to, never null.-1 if this suid is less than, 0 if it is equals to, and 1 if it is greater than that.NullPointerException - when that is null.public static boolean looksValid(String value)
value looks like a valid suid string.
If this method returns true, this only indicates that it's *probably*
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.public static List<Long> toLong(List<Suid> ids)
ids to a list of longs.ids - The ids to convert, may be empty but not null.null.Copyright © 2016. All rights reserved.