Holger's
Java API

com.antelmann.db
Class TransactionalNumberGenerator

java.lang.Object
  extended by com.antelmann.db.TransactionalNumberGenerator
All Implemented Interfaces:
IDFactory, Factory<Long>, ValueRetriever<Long>, Wrapped<ValueRetriever<Long>>

public class TransactionalNumberGenerator
extends Object
implements Wrapped<ValueRetriever<Long>>, ValueRetriever<Long>, Factory<Long>, IDFactory

provides a wrapper around a given number generator that provides a counter that can increment and cache its generated values within a running transaction without having to make the potentially expensive call to the embedded ValueRetriever. In addition, this class implements various related interfaces to allow for flexible usage.

Since:
01.04.2010, 14:13:16
Author:
Holger Antelmann
See Also:
SQLNumberGenerator

Constructor Summary
TransactionalNumberGenerator(ValueRetriever<Long> numberGenerator, DBTransaction tx)
           
 
Method Summary
 Long createInstance(EventObject eo)
          calls getRelevantValue()
 TransactionListener enableUpdateOnFinishTransaction(ValueSetter<Long> setter)
          calling this method will add a TransactionListener to the database that will call the given setter with the current count if this instance is 'dirty' upon finishTransaction.
protected  long generateNewCount()
          this method requires to be synchronized on the transaction
 Object generateNewID(Class<? extends DBEntry> entryClass)
          calls getRelevantValue()
 Long getCurrentCount()
          may return -1 if getRelevantValue() has never been called
 Long getRelevantValue()
          calls generateNewCount() in a transactionally synchronized context
 DBTransaction getTransaction()
           
 boolean isDirty()
          the instance is 'dirty' if getRelevantValue() was called and returned a cached value that was not directly retrieved from the embedded NumberGenerator
 ValueRetriever<Long> unwrap()
          returns the embedded instance that this wrapper encloses
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TransactionalNumberGenerator

public TransactionalNumberGenerator(ValueRetriever<Long> numberGenerator,
                                    DBTransaction tx)
Method Detail

getTransaction

public DBTransaction getTransaction()

unwrap

public ValueRetriever<Long> unwrap()
Description copied from interface: Wrapped
returns the embedded instance that this wrapper encloses

Specified by:
unwrap in interface Wrapped<ValueRetriever<Long>>

isDirty

public boolean isDirty()
the instance is 'dirty' if getRelevantValue() was called and returned a cached value that was not directly retrieved from the embedded NumberGenerator

See Also:
getCurrentCount()

enableUpdateOnFinishTransaction

public TransactionListener enableUpdateOnFinishTransaction(ValueSetter<Long> setter)
calling this method will add a TransactionListener to the database that will call the given setter with the current count if this instance is 'dirty' upon finishTransaction.

See Also:
TransactionListener.finishTransaction(DBTransaction), isDirty(), getCurrentCount()

generateNewCount

protected long generateNewCount()
                         throws IOException
this method requires to be synchronized on the transaction

Throws:
IOException

getCurrentCount

public Long getCurrentCount()
may return -1 if getRelevantValue() has never been called

See Also:
isDirty()

getRelevantValue

public Long getRelevantValue()
                      throws DatabaseException
calls generateNewCount() in a transactionally synchronized context

Specified by:
getRelevantValue in interface ValueRetriever<Long>
Throws:
DatabaseException

createInstance

public Long createInstance(EventObject eo)
                    throws Exception
calls getRelevantValue()

Specified by:
createInstance in interface Factory<Long>
Parameters:
eo - the event that triggered this instance creation; normally, this method should also cope with the event to be null.
Throws:
Exception

generateNewID

public Object generateNewID(Class<? extends DBEntry> entryClass)
                     throws DatabaseException
calls getRelevantValue()

Specified by:
generateNewID in interface IDFactory
Returns:
an Object for use as an ID for the given DBEntry class.
Throws:
DatabaseException


(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads