Class TSID.Factory.Builder

  • Enclosing class:
    TSID.Factory

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

    It is used to setup a custom TSID.Factory.

    • Field Detail

      • node

        private java.lang.Integer node
      • nodeBits

        private java.lang.Integer nodeBits
      • customEpoch

        private java.lang.Long customEpoch
      • clock

        private java.time.Clock clock
    • Constructor Detail

      • Builder

        public Builder()
    • Method Detail

      • withNode

        public TSID.Factory.Builder withNode​(java.lang.Integer node)
        Set the node identifier.
        Parameters:
        node - a number that must be between 0 and 2^nodeBits-1.
        Returns:
        TSID.Factory.Builder
        Throws:
        java.lang.IllegalArgumentException - if the node identifier is out of the range [0, 2^nodeBits-1] when build() is invoked
      • withNodeBits

        public TSID.Factory.Builder withNodeBits​(java.lang.Integer nodeBits)
        Set the node identifier bits length.
        Parameters:
        nodeBits - a number that must be between 0 and 20.
        Returns:
        TSID.Factory.Builder
        Throws:
        java.lang.IllegalArgumentException - if the node bits are out of the range [0, 20] when build() is invoked
      • withCustomEpoch

        public TSID.Factory.Builder withCustomEpoch​(java.time.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​(java.util.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​(java.util.function.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​(java.util.function.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
      • getNode

        protected java.lang.Integer getNode()
        Get the node identifier.
        Returns:
        a number
        Throws:
        java.lang.IllegalArgumentException - if the node is out of range
      • getNodeBits

        protected java.lang.Integer getNodeBits()
        Get the node identifier bits length within the range 0 to 20.
        Returns:
        a number
        Throws:
        java.lang.IllegalArgumentException - if the node bits are out of range
      • getCustomEpoch

        protected java.lang.Long getCustomEpoch()
        Gets the custom epoch.
        Returns:
        a number
      • getRandom

        protected TSID.Factory.IRandom getRandom()
        Gets the random generator.
        Returns:
        a random generator
      • getClock

        protected java.time.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:
        java.lang.IllegalArgumentException - if the node is out of range
        java.lang.IllegalArgumentException - if the node bits are out of range