segunda-feira, 27 de outubro de 2008

Sequence Toolbox

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
Not all sequences are present as some are implement inside other toolboxes. I'll talk about them later.
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:

1
2 Sequence sequence = new LongSequence () ;
3 Long value = sequence.next () .value () ;

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.

sexta-feira, 24 de outubro de 2008

Starting Point

Hello, I'm Sergio Taborda. I'm the author of MiddleHeaven and this is the first post of a long, I hope enjoyable, serie of posts were I will try do explain what MiddleHeaven is. This is not easy because it can be many things at once.
I will try to explain the main concepts , ideas, goals and trade-off I made along the way.
I'm not trying to convince you to use MiddleHeaven as it is in a very early stage, but I think the process can be very instructive both for you and for me.

Mainly MiddleHeaven is Open Source Java Framework. Some folks may ask "Another framework ?", yes, another framework. Why ? Because I fell current frameworks are very focussed on the technology and little on the business. I wanted a framework that could help me build complicated , distributed , rich applications with little effort by pre-integrating other common frameworks and fill the lacks where need be.

MiddleHeaven is more than a simple framework, is a set of mini-framework working together: the toolboxes.

MiddleHeaven is based on Java 1.6 with eyes on 1.7. and does not despise other frameworks out there. Instead it tries to incorporate the best in each of them and encapsulate it under a common interface so that in the future the underlining framework can be change to a more modern, robust one. MiddleHeaven stands for code library independancy as the JVM stands for OS independant execution (generally speaking).

At this point MiddleHeaven is in an alfa stage of development as it lacks UI abilities. However many toolboxes are already available ,and I will start to explain them in posts to come... Meanwhile you can dig the code.

Tank you