Class IntegerIdGenerator
- All Implemented Interfaces:
StringIdGenerator
This class wraps an existing IdGenerator
that produces long IDs and
provides methods to generate integer IDs instead. This is useful when working
with systems or databases that require integer primary keys rather than longs.
Note that integer IDs have a much smaller range than long IDs (approximately
+/- 2.1 billion vs +/- 9.2 quintillion), so this generator throws an
IntegerIdGenerator.IdOverflowException
when the underlying long ID exceeds the integer range.
This class implements StringIdGenerator
to provide string ID generation
capabilities as well, using the wrapped generator's ID converter.
Implementations of this class are thread-safe and can be used concurrently across multiple threads.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Exception thrown when an ID exceeds the integer value range. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final IdGenerator
The actual ID generator that produces long IDs. -
Constructor Summary
ConstructorsConstructorDescriptionIntegerIdGenerator
(IdGenerator actual) Create a new IntegerIdGenerator that wraps the specified long ID generator. -
Method Summary
Modifier and TypeMethodDescriptionint
generate()
Generate a distributed ID as an integer value.Generate a distributed ID as a string value.
-
Field Details
-
actual
The actual ID generator that produces long IDs.This generator is used as the source of IDs, which are then converted to integer format when requested.
-
-
Constructor Details
-
IntegerIdGenerator
Create a new IntegerIdGenerator that wraps the specified long ID generator.The provided generator will be used to produce the underlying long IDs that are then converted to integers when requested.
- Parameters:
actual
- The actual ID generator that produces long IDs
-
-
Method Details
-
generate
Generate a distributed ID as an integer value.This method generates a unique integer identifier by first generating a long ID from the wrapped generator and then converting it to an integer.
If the generated long ID exceeds the range of valid integer values (Integer.MIN_VALUE to Integer.MAX_VALUE), an
IntegerIdGenerator.IdOverflowException
is thrown.- Returns:
- A unique distributed ID as an integer value
- Throws:
IntegerIdGenerator.IdOverflowException
- if the generated long ID exceeds the integer range
-
generateAsString
Generate a distributed ID as a string value.This method generates a unique string identifier by first generating a long ID from the wrapped generator, ensuring it fits in an integer, and then converting it to a string using the wrapped generator's ID converter.
If the generated long ID exceeds the range of valid integer values (Integer.MIN_VALUE to Integer.MAX_VALUE), an
IntegerIdGenerator.IdOverflowException
is thrown.- Specified by:
generateAsString
in interfaceStringIdGenerator
- Returns:
- A unique distributed ID as a string value
- Throws:
IntegerIdGenerator.IdOverflowException
- if the generated long ID exceeds the integer range
-