- java.lang.Object
-
- com.github.f4b6a3.tsid.TsidFactory.Builder
-
- Enclosing class:
- TsidFactory
public static class TsidFactory.Builder extends Object
A nested class that builds custom TSID factories.It is used to setup a custom
TsidFactory.
-
-
Constructor Summary
Constructors Constructor Description Builder()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TsidFactorybuild()Returns a build TSID factory.protected ClockgetClock()Gets the clock to be used in tests.protected LonggetCustomEpoch()Gets the custom epoch.protected IntegergetNode()Get the node identifier.protected IntegergetNodeBits()Get the node identifier bits length within the range 0 to 20.protected com.github.f4b6a3.tsid.TsidFactory.IRandomgetRandom()Gets the random generator.TsidFactory.BuilderwithClock(Clock clock)Set the clock to be used in tests.TsidFactory.BuilderwithCustomEpoch(Instant customEpoch)Set the custom epoch.TsidFactory.BuilderwithNode(Integer node)Set the node identifier.TsidFactory.BuilderwithNodeBits(Integer nodeBits)Set the node identifier bits length.TsidFactory.BuilderwithRandom(Random random)Set the random generator.TsidFactory.BuilderwithRandomFunction(IntFunction<byte[]> randomFunction)Set the random function.TsidFactory.BuilderwithRandomFunction(IntSupplier randomFunction)Set the random function.
-
-
-
Method Detail
-
withNode
public TsidFactory.Builder withNode(Integer node)
Set the node identifier.- Parameters:
node- a number that must be between 0 and 2^nodeBits-1.- Returns:
TsidFactory.Builder- Throws:
IllegalArgumentException- if the node identifier is out of the range [0, 2^nodeBits-1] whenbuild()is invoked
-
withNodeBits
public TsidFactory.Builder withNodeBits(Integer nodeBits)
Set the node identifier bits length.- Parameters:
nodeBits- a number that must be between 0 and 20.- Returns:
TsidFactory.Builder- Throws:
IllegalArgumentException- if the node bits are out of the range [0, 20] whenbuild()is invoked
-
withCustomEpoch
public TsidFactory.Builder withCustomEpoch(Instant customEpoch)
Set the custom epoch.- Parameters:
customEpoch- an instant that represents the custom epoch.- Returns:
TsidFactory.Builder
-
withRandom
public TsidFactory.Builder withRandom(Random random)
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:
TsidFactory.Builder
-
withRandomFunction
public TsidFactory.Builder withRandomFunction(IntSupplier randomFunction)
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:
TsidFactory.Builder
-
withRandomFunction
public TsidFactory.Builder withRandomFunction(IntFunction<byte[]> randomFunction)
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:
TsidFactory.Builder
-
withClock
public TsidFactory.Builder withClock(Clock clock)
Set the clock to be used in tests.- Parameters:
clock- a clock- Returns:
TsidFactory.Builder
-
getNode
protected Integer getNode()
Get the node identifier.- Returns:
- a number
- Throws:
IllegalArgumentException- if the node is out of range
-
getNodeBits
protected Integer 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
protected Long getCustomEpoch()
Gets the custom epoch.- Returns:
- a number
-
getRandom
protected com.github.f4b6a3.tsid.TsidFactory.IRandom getRandom()
Gets the random generator.- Returns:
- a random generator
-
getClock
protected Clock getClock()
Gets the clock to be used in tests.- Returns:
- a clock
-
build
public TsidFactory build()
Returns a build TSID factory.- Returns:
TsidFactory- Throws:
IllegalArgumentException- if the node is out of rangeIllegalArgumentException- if the node bits are out of range
-
-