Package io.hypersistence.tsid
Class TSID.Factory.Builder
java.lang.Object
io.hypersistence.tsid.TSID.Factory.Builder
- Enclosing class:
- TSID.Factory
A nested class that builds custom TSID factories.
It is used to setup a custom TSID.Factory.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuild()Returns a build TSID factory.protected ClockgetClock()Gets the clock to be used in tests.protected LongGets the custom epoch.protected IntegergetNode()Get the node identifier.protected IntegerGet the node identifier bits length within the range 0 to 20.protected io.hypersistence.tsid.TSID.Factory.IRandomGets the random generator.Set the clock to be used in tests.withCustomEpoch(Instant customEpoch) Set the custom epoch.Set the node identifier.withNodeBits(Integer nodeBits) Set the node identifier bits length.withRandom(Random random) Set the random generator.withRandomFunction(IntFunction<byte[]> randomFunction) Set the random function.withRandomFunction(IntSupplier randomFunction) Set the random function.
-
Constructor Details
-
Builder
public Builder()
-
-
Method Details
-
withNode
Set the node identifier.- Parameters:
node- a number that must be between 0 and 2^nodeBits-1.- Returns:
TSID.Factory.Builder- Throws:
IllegalArgumentException- if the node identifier is out of the range [0, 2^nodeBits-1] whenbuild()is invoked
-
withNodeBits
Set the node identifier bits length.- Parameters:
nodeBits- a number that must be between 0 and 20.- Returns:
TSID.Factory.Builder- Throws:
IllegalArgumentException- if the node bits are out of the range [0, 20] whenbuild()is invoked
-
withCustomEpoch
Set the custom epoch.- Parameters:
customEpoch- an instant that represents the custom epoch.- Returns:
TSID.Factory.Builder
-
withRandom
Set the random generator.The random generator is used to create a random function that is used to reset the counter when the millisecond changes.
- Parameters:
random- aRandomgenerator- Returns:
TSID.Factory.Builder
-
withRandomFunction
Set the random function.The random function is used to reset the counter when the millisecond changes.
- Parameters:
randomFunction- a random function that returns a integer value- Returns:
TSID.Factory.Builder
-
withRandomFunction
Set the random function.The random function must return a byte array of a given length.
The random function is used to reset the counter when the millisecond changes.
Despite its name, the random function MAY return a fixed value, for example, if your app requires the counter to be reset to ZERO whenever the millisecond changes, like Twitter Snowflakes, this function should return an array filled with ZEROS.
- Parameters:
randomFunction- a random function that returns a byte array- Returns:
TSID.Factory.Builder
-
withClock
Set the clock to be used in tests.- Parameters:
clock- a clock- Returns:
TSID.Factory.Builder
-
getNode
Get the node identifier.- Returns:
- a number
- Throws:
IllegalArgumentException- if the node is out of range
-
getNodeBits
Get the node identifier bits length within the range 0 to 20.- Returns:
- a number
- Throws:
IllegalArgumentException- if the node bits are out of range
-
getCustomEpoch
Gets the custom epoch.- Returns:
- a number
-
getRandom
protected io.hypersistence.tsid.TSID.Factory.IRandom getRandom()Gets the random generator.- Returns:
- a random generator
-
getClock
Gets the clock to be used in tests.- Returns:
- a clock
-
build
Returns a build TSID factory.- Returns:
TSID.Factory- Throws:
IllegalArgumentException- if the node is out of rangeIllegalArgumentException- if the node bits are out of range
-