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.
This can be serialized by custom serializers in squidstore.core for libGDX Json or squidfreeze.core for Kryo. Fory doesn't need a serializer registered.
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.
This can be serialized by custom serializers in squidstore.core for libGDX Json or squidfreeze.core for Kryo. Fory doesn't need a serializer registered.
-
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.static final com.github.tommyettinger.ds.support.util.PartialParser<UniqueIdentifier> A jdkgdxds PartialParser that can be used to read UniqueIdentifier values from String-serialized data structures containing them. -
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(CharSequence serialized) Given a String or other CharSequence containing the output ofstringSerialize()orappendTo(CharSequence), this creates a new UniqueIdentifier with the same data as the UniqueIdentifier that was serialized.UniqueIdentifier(CharSequence data, int start) Given a String or other CharSequence containing the output ofstringSerialize()orappendTo(CharSequence), this creates a new UniqueIdentifier with the same data as the UniqueIdentifier that was serialized. -
Method Summary
Modifier and TypeMethodDescription<S extends CharSequence & Appendable>
SappendTo(S sb) Appends the serialized String format for this UniqueIdentifier to the given Appendable CharSequence, such as aStringBuilderorCharList.intcompareTo(UniqueIdentifier other) booleanintgetA()intgetB()intgetC()intgetD()longgetHi()longgetLo()inthashCode()booleanisValid()static UniqueIdentifiernext()Reads back a CharSequence produced bystringSerialize()orappendTo(CharSequence), storing the result in this UniqueIdentifier.stringDeserialize(CharSequence data, int start) Loads the state of another serialized UniqueIdentifier (orUniqueIdentifier.Generator) into this.Serializes this UniqueIdentifier to a String, where it can be read back bystringDeserialize(CharSequence).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. -
PARSER
A jdkgdxds PartialParser that can be used to read UniqueIdentifier values from String-serialized data structures containing them.
-
-
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 or other CharSequence containing the output ofstringSerialize()orappendTo(CharSequence), this creates a new UniqueIdentifier with the same data as the UniqueIdentifier that was serialized.- Parameters:
serialized- a CharSequence often produced bystringSerialize()orappendTo(CharSequence)
-
UniqueIdentifier
Given a String or other CharSequence containing the output ofstringSerialize()orappendTo(CharSequence), this creates a new UniqueIdentifier with the same data as the UniqueIdentifier that was serialized.- Parameters:
data- a CharSequence often produced bystringSerialize()orappendTo(CharSequence)start- the first index, inclusive, to read 35 chars from
-
-
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(CharSequence). 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(CharSequence)
-
appendTo
Appends the serialized String format for this UniqueIdentifier to the given Appendable CharSequence, such as aStringBuilderorCharList. This separates the fourintvalues, printed as unsigned withBase.BASE16, with underscores.- Type Parameters:
S- any Appendable CharSequence, such as aStringBuilderorCharList- Parameters:
sb- any Appendable CharSequence, such as aStringBuilder- Returns:
- sb, for chaining
-
stringDeserialize
Reads back a CharSequence produced bystringSerialize()orappendTo(CharSequence), storing the result in this UniqueIdentifier. Reads chars from index 0 (inclusive) to 34 (inclusive).- Parameters:
data- a CharSequence often produced bystringSerialize()orappendTo(CharSequence)- Returns:
- this UniqueIdentifier, after it has been modified.
-
stringDeserialize
Loads the state of another serialized UniqueIdentifier (orUniqueIdentifier.Generator) into this. This requires a 35-char CharSequence (such as a String) at minimum after start with four 8-hex-digit sections, but the delimiters between sections are permitted to be anything (such as'$'forUniqueIdentifier.Generatoror'_'for this).- Parameters:
data- a CharSequence often produced bystringSerialize()orappendTo(CharSequence)start- the first index, inclusive, to read 35 chars from- Returns:
- this Generator, after deserializing
-
next
-
compareTo
- Specified by:
compareToin interfaceComparable<UniqueIdentifier>
-