Class IdGenerator


  • public final class IdGenerator
    extends Object
    Convenience class to track unique ids across different classes that decide to use a generator for their id members. Ids are generated starting at zero and in an incremental consecutive fashion. They are unique to their parent though, so if the same class is registered twice with different parents, then their ids will be unique per parent but not across parents.
    Author:
    markr
    • Constructor Detail

      • IdGenerator

        public IdGenerator()
    • Method Detail

      • createIdGeneratorForParent

        protected static org.goplanit.utils.id.LocalIdGenerator createIdGeneratorForParent​(IdGroupingToken token)
        Create new idGenerators for this group such that we track unique id's within this group
        Parameters:
        token - the group for which ids will be generated
        Returns:
        created IdGenerator
      • generateId

        public static long generateId​(IdGroupingToken token,
                                      Class<? extends Object> theClass)
        Generate a unique id for the chosen class
        Parameters:
        token - the group for which the id is created
        theClass - the class for which the id is being generated
        Returns:
        the generated id
      • reset

        public static void reset()
        Reset the id generation across all registered tokens
      • reset

        public static void reset​(IdGroupingToken groupId)
        Reset the id generation for a specific token
        Parameters:
        groupId - to reset
      • reset

        public static void reset​(IdGroupingToken groupId,
                                 Class<? extends Object> theClass)
        Reset the id generation for a specific token and class
        Parameters:
        groupId - to reset
        theClass - to reset
      • resetTo

        public static void resetTo​(IdGroupingToken groupId,
                                   Class<? extends Object> theClass,
                                   long offset)
        Reset the id generation for a specific token and class to the given offset
        Parameters:
        groupId - to reset
        theClass - to reset
        offset - to use
      • createIdGroupingToken

        public static IdGroupingToken createIdGroupingToken​(String groupDescription)
        Factory method to create a new id grouping token
        Parameters:
        groupDescription - description for the group
        Returns:
        idGroupingToken that has been created
      • createIdGroupingToken

        public static IdGroupingToken createIdGroupingToken​(Object groupOwner,
                                                            long groupOwnerId)
        Factory method to create a new id grouping token based on the class instance simple name and the instance id This allows for a human readable token as well as a unique token
        Parameters:
        groupOwner - group owner
        groupOwnerId - id of the group owner
        Returns:
        idGroupingToken that has been created
      • getLatestIdForToken

        public static long getLatestIdForToken​(IdGroupingToken token,
                                               Class<? extends Object> theClass)
        collect the latest generated id for a given class and token
        Parameters:
        token - to use
        theClass - to use
        Returns:
        latest generated id, -1 if not available