net.sf.okapi.common

## Class IdGenerator

- java.lang.Object
- - net.sf.okapi.common.IdGenerator

-     public class IdGenerator
      extends java.lang.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.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static java.lang.String` | `DEFAULT_ROOT_ID`
    `static java.lang.String` | `DOCUMENT_PART`
    `static java.lang.String` | `END_DOCUMENT`
    `static java.lang.String` | `END_GROUP`
    `static java.lang.String` | `END_SUBDOCUMENT`
    `static java.lang.String` | `END_SUBFILTER`
    `static java.lang.String` | `START_DOCUMENT`
    `static java.lang.String` | `START_GROUP`
    `static java.lang.String` | `START_SUBDOCUMENT`
    `static java.lang.String` | `START_SUBFILTER`
    `static java.lang.String` | `TEXT_UNIT`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `IdGenerator(java.lang.String root)` Creates a generator with a given root and no prefix.
    `IdGenerator(java.lang.String root, java.lang.String prefix)` Creates a generator with a given root and a given prefix.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `java.lang.String` | `createId()` Creates a new identifier.
    `java.lang.String` | `createId(java.lang.String prefix)` Creates a new identifier with the given prefix
    `java.lang.String` | `createIdNotInList(java.util.List<java.lang.String> list)` Creates a new identifier that is not in the given list.
    `java.lang.String` | `getLastId()` Gets the last identifier generated.
    `java.lang.String` | `getRootId()` Gets the id generated from the root string given when creating this object.
    `long` | `getSequence()` Get the current sequence number.
    `void` | `reset(java.lang.String rootId)` Reset the `IdGenerator` with a new root id.
    `void` | `setLastId(java.lang.String lastId)` sets the internal value that is used to remember the last identifier.
    `void` | `setPrefix(java.lang.String prefix)` Sets the prefix to use when creating the id.
    `void` | `setSequence(long sequence)` Set the sequence from outside.
    `java.lang.String` | `toString()` Returns the same value as `getLastId()`.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### START_DOCUMENT

          public static final java.lang.String START_DOCUMENT

      See Also:
        Constant Field Values

    - #### END_DOCUMENT

          public static final java.lang.String END_DOCUMENT

      See Also:
        Constant Field Values

    - #### START_GROUP

          public static final java.lang.String START_GROUP

      See Also:
        Constant Field Values

    - #### END_GROUP

          public static final java.lang.String END_GROUP

      See Also:
        Constant Field Values

    - #### TEXT_UNIT

          public static final java.lang.String TEXT_UNIT

      See Also:
        Constant Field Values

    - #### DOCUMENT_PART

          public static final java.lang.String DOCUMENT_PART

      See Also:
        Constant Field Values

    - #### START_SUBDOCUMENT

          public static final java.lang.String START_SUBDOCUMENT

      See Also:
        Constant Field Values

    - #### END_SUBDOCUMENT

          public static final java.lang.String END_SUBDOCUMENT

      See Also:
        Constant Field Values

    - #### START_SUBFILTER

          public static final java.lang.String START_SUBFILTER

      See Also:
        Constant Field Values

    - #### END_SUBFILTER

          public static final java.lang.String END_SUBFILTER

      See Also:
        Constant Field Values

    - #### DEFAULT_ROOT_ID

          public static final java.lang.String DEFAULT_ROOT_ID

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### IdGenerator

          public IdGenerator(java.lang.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(java.lang.String root,
                     java.lang.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

    - #### toString

          public java.lang.String toString()

      Returns the same value as `getLastId()`.
      Overrides:
        `toString` in class `java.lang.Object`

    - #### createId

          public java.lang.String createId()

      Creates a new identifier.
      Returns:
        the new identifier.

    - #### createIdNotInList

          public java.lang.String createIdNotInList(java.util.List<java.lang.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 java.lang.String createId(java.lang.String prefix)

      Creates a new identifier with the given prefix
      Parameters:
        `prefix` - the prefix to be used with this id
      Returns:
        the new identifier.

    - #### setLastId

          public void setLastId(java.lang.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.

    - #### getLastId

          public java.lang.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:
        `java.lang.RuntimeException` - if the method `createId()` has not been called at least once before call this method.

    - #### getRootId

          public java.lang.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)

    - #### setSequence

          public void setSequence(long sequence)

      Set the sequence from outside. Useful to renumber.
      Parameters:
        `sequence` -

    - #### getSequence

          public long getSequence()

      Get the current sequence number.
      Returns:
        the sequence

    - #### reset

          public void reset(java.lang.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(java.lang.String prefix)

      Sets the prefix to use when creating the id.
      Parameters:
        `prefix` - the new prefix to use (can be null).