Package com.helger.base.id.factory
Class AbstractPersistingLongIDFactory
java.lang.Object
com.helger.base.id.factory.AbstractPersistingLongIDFactory
- All Implemented Interfaces:
IBaseIDFactory,ILongIDFactory
@ThreadSafe
public abstract class AbstractPersistingLongIDFactory
extends Object
implements ILongIDFactory
This implementation of
ILongIDFactory reads 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 -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractPersistingLongIDFactory(int nReserveCount) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionbooleanfinal longgetNewID()Create a new unique long ID.final intinthashCode()protected final intprotected abstract longreadAndUpdateIDCounter(int nReserveCount) Read the current ID from the device.final voidsetReserveCount(int nReserveCount) Dynamically set the reserve count of this factory.toString()
-
Field Details
-
m_aLock
-
-
Constructor Details
-
AbstractPersistingLongIDFactory
protected AbstractPersistingLongIDFactory(@Nonnegative int nReserveCount) Constructor.- Parameters:
nReserveCount- The number of IDs to reserve per persistence layer access. Must be > 0.
-
-
Method Details
-
getReserveCount
@Nonnegative public final int getReserveCount()- Returns:
- The number of IDs to reserve, as provided in the constructor. Always > 0.
-
internalGetReserveCountUnlocked
@Nonnegative @MustBeLocked(WRITE) protected final int internalGetReserveCountUnlocked()- Returns:
- The number of IDs to reserve, as provided in the constructor. Always > 0.
- Since:
- 12.1.3
-
setReserveCount
public final void setReserveCount(@Nonnegative int nReserveCount) Dynamically set the reserve count of this factory. May be a performance improvement when this is used in an area where many IDs are required.- Parameters:
nReserveCount- The reserve count to use. Must be > 0.- Since:
- 12.1.3
-
readAndUpdateIDCounter
@IsLocked(WRITE) protected abstract long 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 long readAndUpdateIDCounter (int nReserveCount) { final long 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_ULONGin case of an error.
-
getNewID
public final long getNewID()Create a new unique long ID.- Specified by:
getNewIDin interfaceILongIDFactory- Returns:
- The newly created ID.
-
equals
-
hashCode
public int hashCode() -
toString
-