Package com.helger.commons.id.factory
Class AbstractPersistingIntIDFactory
- java.lang.Object
-
- com.helger.commons.id.factory.AbstractPersistingIntIDFactory
-
- All Implemented Interfaces:
IBaseIDFactory,IIntIDFactory
- Direct Known Subclasses:
FileIntIDFactory
@ThreadSafe public abstract class AbstractPersistingIntIDFactory extends Object implements IIntIDFactory
This implementation ofIIntIDFactoryreads IDs from a device. It does it by reserving a range of n IDs so that not each ID reservation requires IO. If only 1 ID is effectively used, the other n-1 IDs are lost and will never be assigned to any object again.- Author:
- Philip Helger
-
-
Field Summary
Fields Modifier and Type Field Description protected SimpleLockm_aLock
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractPersistingIntIDFactory(int nReserveCount)Constructor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleanequals(Object o)intgetNewID()Create a new unique integer ID.intgetReserveCount()inthashCode()protected abstract intreadAndUpdateIDCounter(int nReserveCount)Read the current ID from the device.StringtoString()
-
-
-
Field Detail
-
m_aLock
protected final SimpleLock m_aLock
-
-
Constructor Detail
-
AbstractPersistingIntIDFactory
protected AbstractPersistingIntIDFactory(@Nonnegative int nReserveCount)
Constructor.- Parameters:
nReserveCount- The number of IDs to reserve per persistence layer access. Must be > 0.
-
-
Method Detail
-
getReserveCount
@Nonnegative public final int getReserveCount()
- Returns:
- The number of IDs to reserve, as provided in the constructor. Always > 0.
-
readAndUpdateIDCounter
@IsLocked(WRITE) protected abstract int readAndUpdateIDCounter(@Nonnegative int nReserveCount)
Read the current ID from the device. In case the method is called for a non-initialized device, 0 should be returned.
The update should write the read value plus the passed reserve count back to the device. This method should perform an atomic read and update to avoid that ID can be reused.
Pseudo code:protected int readAndUpdateIDCounter (int nReserveCount) { final int nRead = FileIO.read (file); FileIO.write (file, nRead + nReserveCount); return nRead; }- Parameters:
nReserveCount- the number that should be added to the read value. Always > 0.- Returns:
- 0 if this method is called for a non-initialized device, the value
read from the device otherwise or
CGlobal.ILLEGAL_UINTin case of an error.
-
getNewID
public final int getNewID()
Description copied from interface:IIntIDFactoryCreate a new unique integer ID.- Specified by:
getNewIDin interfaceIIntIDFactory- Returns:
- The newly created ID.
-
-