Let's start with a simple toolbox: the sequence toolbox. A Sequence represents a generator of tokens with and inherent order. Normally we will use a sequence to generate numeric tokens but it can be use to generate almost anything you need.
A sequence is characterized mainly by three properties:
All sequences in MiddleHevaen are considerer ordered, unlimited and allow gaps.
Sequences that are not ordered must inherit from RandomSequence
. This is mainly a marker interface as no method is added to the interface.
Sequence that are intended to be limited must implement LimitedSequence
. LimitedSequence
adds an hasNext
method to check if there are more tokens in the sequence.
Gap resiliance is a more difficult feature to implement has it must be defined over a transaction context. It two transactions A
and B
acquire tokens from the same sequence and A
rollback, the dumped tokens must be reused by B
, has there can't be gaps.
TransactableSequence
tries to address this by locking the sequence to a specific transaction until it ends (committing or rolling back)
The figure below illustrates the basic set of sequence types in MiddleHeaven and who the relate to each other.
next()
method that return the next token in the sequence.
StateEditableSequence
allows for manipulation of the sequence state (read/write). This is usefull mainlly
for defining StatePersistableSequence
, a sequence whose state can be persisted so it never resets.
This will be most useful for identity generation has we will see when we discuss the Storage Toolbox.
RandomCharSequence
can be used to generate random sequence of characters (that may be further converted into String)
and RandomNumberSequence
can be used to generate random sequences of numbers. For both instances of
Using a sequence is very simple:
Sequences can be used per se or in conjunction or within other toolboxes. The Storage Toolbox uses sequences to create different identification tokens for storable objects. For database supported storage MiddleHeaven abstracts the native sequence mechanism present in some databases as a Sequence of Long.