Package net.sf.okapi.common
Class IdGenerator
- java.lang.Object
-
- net.sf.okapi.common.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 '-':
- The hexadecimal representation of the hash code of the root (if a root exists)
- The sequential identifier starting at 1, with a fixed prefix if one was provided.
-
-
Field Summary
Fields Modifier and Type Field Description static StringDEFAULT_ROOT_IDstatic StringDOCUMENT_PARTstatic StringEND_DOCUMENTstatic StringEND_GROUPstatic StringEND_SUBDOCUMENTstatic StringEND_SUBFILTERstatic StringSTART_DOCUMENTstatic StringSTART_GROUPstatic StringSTART_SUBDOCUMENTstatic StringSTART_SUBFILTERstatic StringSUBFILTERED_EVENTstatic StringTEXT_UNIT
-
Constructor Summary
Constructors Constructor Description IdGenerator()Creates a generator with a empty root and no prefix.IdGenerator(String root)Creates a generator with a given root and no prefix.IdGenerator(String root, String prefix)Creates a generator with a given root and a given prefix.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StringcreateId()Creates a new identifier.StringcreateId(String prefix)Creates a new identifier with the given prefixStringcreateIdNotInList(List<String> list)Creates a new identifier that is not in the given list.StringgetLastId()Gets the last identifier generated.StringgetRootId()Gets the id generated from the root string given when creating this object.longgetSequence()Get the current sequence number.voidreset(String rootId)Reset theIdGeneratorwith a new root id.voidsetLastId(String lastId)sets the internal value that is used to remember the last identifier.voidsetPrefix(String prefix)Sets the prefix to use when creating the id.voidsetSequence(long sequence)Set the sequence from outside.StringtoString()Returns the same value asgetLastId().
-
-
-
Field Detail
-
START_DOCUMENT
public static final String START_DOCUMENT
- See Also:
- Constant Field Values
-
END_DOCUMENT
public static final String END_DOCUMENT
- See Also:
- Constant Field Values
-
START_GROUP
public static final String START_GROUP
- See Also:
- Constant Field Values
-
END_GROUP
public static final String END_GROUP
- See Also:
- Constant Field Values
-
TEXT_UNIT
public static final String TEXT_UNIT
- See Also:
- Constant Field Values
-
DOCUMENT_PART
public static final String DOCUMENT_PART
- See Also:
- Constant Field Values
-
START_SUBDOCUMENT
public static final String START_SUBDOCUMENT
- See Also:
- Constant Field Values
-
END_SUBDOCUMENT
public static final String END_SUBDOCUMENT
- See Also:
- Constant Field Values
-
START_SUBFILTER
public static final String START_SUBFILTER
- See Also:
- Constant Field Values
-
SUBFILTERED_EVENT
public static final String SUBFILTERED_EVENT
- See Also:
- Constant Field Values
-
END_SUBFILTER
public static final String END_SUBFILTER
- See Also:
- Constant Field Values
-
DEFAULT_ROOT_ID
public static final String DEFAULT_ROOT_ID
- See Also:
- Constant Field Values
-
-
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)
-
-
Method Detail
-
toString
public String toString()
Returns the same value asgetLastId().
-
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 bycreateId().- Returns:
- the last identifier generated.
- Throws:
RuntimeException- if the methodcreateId()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 theIdGeneratorwith 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).
-
-