Class 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 Detail

      • Builder

        public Builder()
    • Method Detail

      • 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 - a Random generator
        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
      • 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