Class TSID.Factory.Builder

java.lang.Object
io.hypersistence.tsid.TSID.Factory.Builder
Enclosing class:
TSID.Factory

public static class TSID.Factory.Builder extends Object
A nested class that builds custom TSID factories.

It is used to setup a custom TSID.Factory.

  • Constructor Details

    • Builder

      public Builder()
  • Method Details

    • withNode

      public TSID.Factory.Builder withNode(Integer node)
      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] when build() is invoked
    • withNodeBits

      public TSID.Factory.Builder withNodeBits(Integer nodeBits)
      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] when build() is invoked
    • withCustomEpoch

      public TSID.Factory.Builder withCustomEpoch(Instant customEpoch)
      Set the custom epoch.
      Parameters:
      customEpoch - an instant that represents the custom epoch.
      Returns:
      TSID.Factory.Builder
    • withRandom

      public TSID.Factory.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 - a Random generator
      Returns:
      TSID.Factory.Builder
    • withRandomFunction

      public TSID.Factory.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:
      TSID.Factory.Builder
    • withRandomFunction

      public TSID.Factory.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:
      TSID.Factory.Builder
    • withClock

      public TSID.Factory.Builder withClock(Clock clock)
      Set the clock to be used in tests.
      Parameters:
      clock - a clock
      Returns:
      TSID.Factory.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 io.hypersistence.tsid.TSID.Factory.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 TSID.Factory build()
      Returns a build TSID factory.
      Returns:
      TSID.Factory
      Throws:
      IllegalArgumentException - if the node is out of range
      IllegalArgumentException - if the node bits are out of range