- 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 within the range 0 to 20.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.The range is 0 to 2^nodeBits-1.
- Parameters:
node- a number between 0 and 2^nodeBits-1.- Returns:
TsidFactory.Builder
-
withNodeBits
public TsidFactory.Builder withNodeBits(Integer nodeBits)
Set the node identifier bits length within the range 0 to 20.- Parameters:
nodeBits- a number between 0 and 20.- Returns:
TsidFactory.Builder
-
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.
If the returned value is NULL or EMPTY, the factory ignores it and just increments the counter when the millisecond changes, for example, when your app requires the counter to always be incremented, no matter if the millisecond has changed or not, like Discord Snowflakes.
- 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
-
getNodeBits
protected Integer getNodeBits()
Get the node identifier bits length within the range 0 to 20.- Returns:
- a number
-
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
-
-