Class IdGenerator


  • public class IdGenerator
    extends Object
    Provides a common way to generate sequential ID that are unique for a given root. The root can be null or empty.

    Each value generated is made of two main parts separated by a '-':

    1. The hexadecimal representation of the hash code of the root (if a root exists)
    2. The sequential identifier starting at 1, with a fixed prefix if one was provided.
    • Constructor Detail

      • IdGenerator

        public IdGenerator()
        Creates a generator with a empty root and no prefix.
      • IdGenerator

        public IdGenerator​(String root)
        Creates a generator with a given root and no prefix.
        Parameters:
        root - the root to use (case-sensitive, can be null or empty)
      • IdGenerator

        public IdGenerator​(String root,
                           String prefix)
        Creates a generator with a given root and a given prefix.
        Parameters:
        root - the root to use (case-sensitive, can be null)
        prefix - the prefix to use (case-sensitive, can be null)
    • Method Detail

      • createId

        public String createId()
        Creates a new identifier.
        Returns:
        the new identifier.
      • createIdNotInList

        public String createIdNotInList​(List<String> list)
        Creates a new identifier that is not in the given list.
        Parameters:
        list - the list of identifiers not to use.
        Returns:
        a new identifier that is not in the given list.
      • createId

        public String createId​(String prefix)
        Creates a new identifier with the given prefix
        Parameters:
        prefix - the prefix to be used with this id
        Returns:
        the new identifier.
      • getLastId

        public String getLastId()
        Gets the last identifier generated. This method allows you to get the last identifier that was returned by createId().
        Returns:
        the last identifier generated.
        Throws:
        RuntimeException - if the method createId() has not been called at least once before call this method.
      • setLastId

        public void setLastId​(String lastId)
        sets the internal value that is used to remember the last identifier. Use this method when you create the ID from outside the object, but still need the last id.
        Parameters:
        lastId - the new last id.
      • getRootId

        public String getRootId()
        Gets the id generated from the root string given when creating this object.
        Returns:
        the id of the root for this object (can be null)
      • getSequence

        public long getSequence()
        Get the current sequence number.
        Returns:
        the sequence
      • setSequence

        public void setSequence​(long sequence)
        Set the sequence from outside. Useful to renumber.
        Parameters:
        sequence - the new sequence.
      • reset

        public void reset​(String rootId)
        Reset the IdGenerator with a new root id. Use the same prefix and set the sequence count to 0.
        Parameters:
        rootId - new root id (can be null or empty)
      • setPrefix

        public void setPrefix​(String prefix)
        Sets the prefix to use when creating the id.
        Parameters:
        prefix - the new prefix to use (can be null).