Sequence Toolbox
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.
Properties of a Sequence
A sequence is characterized mainly by three properties:
- Order - the sequence produces tokens in a specific order (normally ins ascendant order). As a consequence the token must contain an object with a natural order.
- Gap Resilience - the sequence may accept gaps or not. Accept gaps means not all token values must be used. Example: an integer sequence car run like : 1, 2, 3 , 6, 7,8 ... not using the 4 or the 5 for the token value.
- Limit - the sequence may stop because no more tokens are possible. Although conceptually a sequence may have an infinite number of tokens, in practice no sequence is infinite. Has so, all sequences in MiddleHeavewn are considered "potentially infinite" , i.e. they would be infinite if there was no computational/environmental limit. thus, limited sequence are those that we know would never be infinite (e.g. a sequence based on the items of an array)
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 the LimitedSequence interface. LimitedSequence adds an hasNext method to check if there are more tokens in the sequence.
Gap resilience 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 rolls-back, 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)
Model
The figure below illustrates the basic set of sequence types in MiddleHeaven and who the relate to each other.
Illustration 1: Sequence basic type model |
Following the Separation of Concerns principle all basic types are modelled as interfaces. All sequences are type-generic and have a next() method that return the next token in the sequence. StateEditableSequence allows for manipulation of the sequence state (read/write). This is useful mainly 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 Random can be specified for better control.
Use
Using a sequence is very simple:
| |
Code 1: Example of sequence use |
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.