Package io.hekate.util
Class StampedStateGuard
- java.lang.Object
-
- io.hekate.util.StampedStateGuard
-
public class StampedStateGuard extends Object
Helper class that provides support for components lifecycle management and locking.Internally this class is backed by
StampedLock
.- See Also:
StateGuard
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
StampedStateGuard.State
Enumeration ofStampedStateGuard
states.
-
Constructor Summary
Constructors Constructor Description StampedStateGuard(Class<?> type)
Constructs new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
becomeInitialized()
Switches this guard toStampedStateGuard.State.INITIALIZED
state.void
becomeInitializing()
Switches this guard toStampedStateGuard.State.INITIALIZING
state.boolean
becomeTerminated()
Switches this guard toStampedStateGuard.State.TERMINATED
state if guard is currently in any other state besidesStampedStateGuard.State.TERMINATED
.boolean
becomeTerminating()
Switches this guard toStampedStateGuard.State.TERMINATING
state if guard is currently inStampedStateGuard.State.INITIALIZING
orStampedStateGuard.State.INITIALIZED
state.boolean
isInitialized()
Returnstrue
if this guard is inStampedStateGuard.State.INITIALIZED
state.boolean
isInitializing()
Returnstrue
if this guard is inStampedStateGuard.State.INITIALIZING
state.boolean
isWriteLocked()
Returnstrue
if current thread hold the write lock.long
lockRead()
Acquires the read lock.long
lockReadWithStateCheck()
Acquires the read lock on this guard and checks its current state.long
lockWrite()
Acquires the write lock.long
lockWriteWithStateCheck()
Acquires the write lock on this guard and checks its current state.String
toString()
void
unlockRead(long stamp)
Releases the read lock.void
unlockWrite(long stamp)
Releases the write lock.
-
-
-
Constructor Detail
-
StampedStateGuard
public StampedStateGuard(Class<?> type)
Constructs new instance.- Parameters:
type
- Type of a guarded component (for errors reporting).
-
-
Method Detail
-
becomeInitializing
public void becomeInitializing()
Switches this guard toStampedStateGuard.State.INITIALIZING
state.Important!!! This method must be invoked with
write lock
being held.
-
isInitializing
public boolean isInitializing()
Returnstrue
if this guard is inStampedStateGuard.State.INITIALIZING
state.Important!!! This method must be invoked with
write lock
orread lock
being held.- Returns:
true
if this guard is inStampedStateGuard.State.INITIALIZING
state.
-
becomeInitialized
public void becomeInitialized()
Switches this guard toStampedStateGuard.State.INITIALIZED
state.Important!!! This method must be invoked with
write lock
being held.
-
isInitialized
public boolean isInitialized()
Returnstrue
if this guard is inStampedStateGuard.State.INITIALIZED
state.Important!!! This method must be invoked with
write lock
orread lock
being held.- Returns:
true
if this guard is inStampedStateGuard.State.INITIALIZED
state.
-
becomeTerminating
public boolean becomeTerminating()
Switches this guard toStampedStateGuard.State.TERMINATING
state if guard is currently inStampedStateGuard.State.INITIALIZING
orStampedStateGuard.State.INITIALIZED
state.Important!!! This method must be invoked with
write lock
being held.- Returns:
true
if successfully switched toStampedStateGuard.State.TERMINATING
state.
-
becomeTerminated
public boolean becomeTerminated()
Switches this guard toStampedStateGuard.State.TERMINATED
state if guard is currently in any other state besidesStampedStateGuard.State.TERMINATED
.Important!!! This method must be invoked with
write lock
being held.- Returns:
true
if successfully switched toStampedStateGuard.State.TERMINATED
state.
-
lockReadWithStateCheck
public long lockReadWithStateCheck()
Acquires the read lock on this guard and checks its current state. If state is anything other thanStampedStateGuard.State.INITIALIZED
thenIllegalStateException
will be thrown and lock will be released.- Returns:
- Stamp that can be used to unlock.
- Throws:
IllegalStateException
- If guard is not inStampedStateGuard.State.INITIALIZED
state.
-
lockRead
public long lockRead()
Acquires the read lock.- Returns:
- Stamp that can be used to unlock.
- See Also:
StampedLock.readLock()
-
unlockRead
public void unlockRead(long stamp)
Releases the read lock.- Parameters:
stamp
- Lock stamp (seelockRead()
).- See Also:
StampedLock.unlockRead(long)
-
lockWriteWithStateCheck
public long lockWriteWithStateCheck()
Acquires the write lock on this guard and checks its current state. If state is anything other thanStampedStateGuard.State.INITIALIZED
thenIllegalStateException
will be thrown and lock will be released.- Returns:
- Stamp that can be used to unlock.
- Throws:
IllegalStateException
- If guard is not inStampedStateGuard.State.INITIALIZED
state.
-
lockWrite
public long lockWrite()
Acquires the write lock.- Returns:
- Stamp that can be used to unlock.
- See Also:
StampedLock.writeLock()
-
unlockWrite
public void unlockWrite(long stamp)
Releases the write lock.- Parameters:
stamp
- Lock stamp (seelockWrite()
).- See Also:
StampedLock.unlockWrite(long)
-
isWriteLocked
public boolean isWriteLocked()
Returnstrue
if current thread hold the write lock.- Returns:
true
if current thread hold the write lock.- See Also:
StampedLock.isWriteLocked()
-
-