Class UniqueIdentifier
java.lang.Object
com.github.yellowstonegames.core.UniqueIdentifier
- All Implemented Interfaces:
Comparable<UniqueIdentifier>
A substitute for the UUID class, since it isn't available on GWT.
The typical usage is to call
This can be serialized out-of-the-box to Strings using
This is also Comparable, for some reason (UUID is, but since these should all be random, it doesn't mean much). UniqueIdentifier supports up to 2 to the 128 minus 1 unique instances, which should be far more than enough for centuries of generation. If you were using UUID, it only supports 2 to the 122 unique random UUIDs, with a collision 50% likely after 2 to the 61 UUIDs were generated. If this is used properly, it can't collide until all (2 to the 128 minus 1) identifiers have been generated.
next() when you want a new UniqueIdentifier. If the app is closing down and
needs to save its state to be resumed later, GENERATOR must be serialized as well, and deserialized before
calling next() again after resuming. Without this last step, the generated identifiers are extremely
likely to be unique, but not guaranteed to be unique.
This can be serialized out-of-the-box to Strings using
stringSerialize(), but if you do, so must the
GENERATOR that produces new UniqueIdentifier instances and ensures they are unique.
If you are using Fory or another type of serializer that can register arbitrary classes, this can be sent
directly to that without needing any extra serialization code. Like with the String serialization, you must serialize
the GENERATOR field and restore it when restarting from a serialized state. The Base class (in
digital) is all this uses from other code.
This is also Comparable, for some reason (UUID is, but since these should all be random, it doesn't mean much). UniqueIdentifier supports up to 2 to the 128 minus 1 unique instances, which should be far more than enough for centuries of generation. If you were using UUID, it only supports 2 to the 122 unique random UUIDs, with a collision 50% likely after 2 to the 61 UUIDs were generated. If this is used properly, it can't collide until all (2 to the 128 minus 1) identifiers have been generated.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classThe type used as a factory to produce UniqueIdentifiers that are actually unique for a given Generator. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic UniqueIdentifier.GeneratorTheUniqueIdentifier.Generatorthat actually produces unique identifiers. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new, invalid UniqueIdentifier.UniqueIdentifier(int stateA, int stateB, int stateC, int stateD) Creates a new UniqueIdentifier that may or may not actually be unique.UniqueIdentifier(long hi, long lo) Creates a new UniqueIdentifier that may or may not actually be unique.UniqueIdentifier(String serialized) Given a String containing the output ofstringSerialize(), this creates a new UniqueIdentifier with the same data as the UniqueIdentifier that was serialized. -
Method Summary
Modifier and TypeMethodDescriptionintcompareTo(UniqueIdentifier other) booleanintgetA()intgetB()intgetC()intgetD()longgetHi()longgetLo()inthashCode()booleanisValid()static UniqueIdentifiernext()stringDeserialize(String data) Reads back a String produced bystringSerialize(), storing the result in this UniqueIdentifier.Serializes this UniqueIdentifier to a String, where it can be read back bystringDeserialize(String).toString()
-
Field Details
-
GENERATOR
TheUniqueIdentifier.Generatorthat actually produces unique identifiers. If your application pauses and needs to be resumed later by loading serialized state, you must include this field in what you serialize, and load it before creating any additional UniqueIdentifier values withnext()orUniqueIdentifier.Generator.generate(). Failure to maintain the previous GENERATOR value can result in identifiers not being unique.
-
-
Constructor Details
-
UniqueIdentifier
public UniqueIdentifier()Creates a new, invalid UniqueIdentifier. All states will be 0. Usenext()to generate random UniqueIdentifiers. -
UniqueIdentifier
public UniqueIdentifier(long hi, long lo) Creates a new UniqueIdentifier that may or may not actually be unique. This uses the given states verbatim. If both all states are 0, this will be treated as an invalid identifier. Most usage should prefernext()instead.- Parameters:
hi- the high 64 bits, as a longlo- the low 64 bits, as a long
-
UniqueIdentifier
public UniqueIdentifier(int stateA, int stateB, int stateC, int stateD) Creates a new UniqueIdentifier that may or may not actually be unique. This uses the given states verbatim. If all states are 0, this will be treated as an invalid identifier. Most usage should prefernext()instead.- Parameters:
stateA- will be used verbatim for state astateB- will be used verbatim for state bstateC- will be used verbatim for state cstateD- will be used verbatim for state d
-
UniqueIdentifier
Given a String containing the output ofstringSerialize(), this creates a new UniqueIdentifier with the same data as the UniqueIdentifier that was serialized.- Parameters:
serialized- a String almost always produced bystringSerialize()
-
-
Method Details
-
getA
public int getA() -
getB
public int getB() -
getC
public int getC() -
getD
public int getD() -
getHi
public long getHi() -
getLo
public long getLo() -
isValid
public boolean isValid()- Returns:
- false if this instance is the invalid all-0 value; true otherwise
-
equals
-
hashCode
-
toString
-
stringSerialize
Serializes this UniqueIdentifier to a String, where it can be read back bystringDeserialize(String). This is different from most other stringSerialize() methods in that it always produces a 35-character String, consisting ofgetA(), then a'_', thengetB(), then another underscore, c, underscore, and finally d, with a, b, c, and d represented as unsigned hex int Strings.- Returns:
- a 35-character-long String storing this identifier; can be read back with
stringDeserialize(String)
-
stringDeserialize
Reads back a String produced bystringSerialize(), storing the result in this UniqueIdentifier.- Parameters:
data- a String almost certainly produced bystringSerialize()- Returns:
- this UniqueIdentifier, after it has been modified.
-
next
-
compareTo
- Specified by:
compareToin interfaceComparable<UniqueIdentifier>
-