Interface IdGenerator<T>

Type Parameters:
T - the type of identifier produced
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface IdGenerator<T>
Contract for generating identifiers of a particular type (for example, sequential Long values, random UUIDs, etc.)

Implementations may or may not guarantee uniqueness, ordering, or repeatability of generated identifiers. Callers should not assume any such guarantees unless documented by the implementation.

Standard implementations can be acquired via these factory methods:

Author:
Mark Allen
  • Method Summary

    Modifier and Type
    Method
    Description
    Generates an identifier.
    Acquires an IdGenerator with a default numeric ID range (values will wrap once maximum is reached) and a best-effort local IP prefix.
    Acquires an IdGenerator with a default numeric ID range (values will wrap once maximum is reached) and the given prefix.
    withRange(Long minimumId, Long maximumId)
    Acquires an IdGenerator with the given minimum and maximum ID values (values will wrap once maximum is reached), and a best-effort local IP prefix.
    withRangeAndPrefix(Long minimumId, Long maximumId, String prefix)
    Returns a DefaultIdGenerator with the given minimum and maximum ID values (values will wrap once maximum is reached), and the specified prefix.
  • Method Details

    • generateId

      Generates an identifier.

      Implementations may choose different strategies (sequential, random, host-based, etc.) and are not required to guarantee uniqueness unless explicitly documented.

      Returns:
      the generated identifier (never null)
    • withDefaults

      Acquires an IdGenerator with a default numeric ID range (values will wrap once maximum is reached) and a best-effort local IP prefix.

      Callers should not rely on reference identity; this method may return a new or cached instance.

      Returns:
      an IdGenerator with default settings
    • withRange

      @Nonnull static IdGenerator withRange(@Nonnull Long minimumId, @Nonnull Long maximumId)
      Acquires an IdGenerator with the given minimum and maximum ID values (values will wrap once maximum is reached), and a best-effort local IP prefix.

      Callers should not rely on reference identity; this method may return a new or cached instance.

      Parameters:
      minimumId - the lowest ID that may be generated (inclusive)
      maximumId - the highest ID that may be generated (inclusive)
      Returns:
      an IdGenerator configured with the given range
    • withRangeAndPrefix

      @Nonnull static IdGenerator withRangeAndPrefix(@Nonnull Long minimumId, @Nonnull Long maximumId, @Nonnull String prefix)
      Returns a DefaultIdGenerator with the given minimum and maximum ID values (values will wrap once maximum is reached), and the specified prefix.

      Callers should not rely on reference identity; this method may return a new or cached instance.

      Parameters:
      minimumId - the lowest ID that may be generated (inclusive)
      maximumId - the highest ID that may be generated (inclusive)
      prefix - a string to prepend to the generated numeric ID
      Returns:
      an IdGenerator configured with the given range and prefix
    • withPrefix

      Acquires an IdGenerator with a default numeric ID range (values will wrap once maximum is reached) and the given prefix.

      Callers should not rely on reference identity; this method may return a new or cached instance.

      Parameters:
      prefix - a string to prepend to the generated numeric ID
      Returns:
      an IdGenerator configured with the given prefix