Package org.goplanit.utils.id
Class IdGenerator
- java.lang.Object
-
- org.goplanit.utils.id.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 Summary
Constructors Constructor Description IdGenerator()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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 groupstatic 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 tokenstatic IdGroupingToken
createIdGroupingToken(String groupDescription)
Factory method to create a new id grouping tokenstatic long
generateId(IdGroupingToken token, Class<? extends Object> theClass)
Generate a unique id for the chosen classstatic long
getLatestIdForToken(IdGroupingToken token, Class<? extends Object> theClass)
collect the latest generated id for a given class and tokenstatic void
reset()
Reset the id generation across all registered tokensstatic void
reset(IdGroupingToken groupId)
Reset the id generation for a specific tokenstatic void
reset(IdGroupingToken groupId, Class<? extends Object> theClass)
Reset the id generation for a specific token and classstatic void
resetTo(IdGroupingToken groupId, Class<? extends Object> theClass, long offset)
Reset the id generation for a specific token and class to the given offset
-
-
-
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 createdtheClass
- 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 resettheClass
- 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 resettheClass
- to resetoffset
- 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 ownergroupOwnerId
- 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 usetheClass
- to use- Returns:
- latest generated id, -1 if not available
-
-