Package org.aksw.commons.util.ref
Class RefImpl<T>
- java.lang.Object
-
- org.aksw.commons.util.ref.RefImpl<T>
-
- Type Parameters:
T-
- All Implemented Interfaces:
AutoCloseable,Ref<T>
public class RefImpl<T> extends Object implements Ref<T>
Implementation of aRefTODO Avoid needless synchronization; a ConcurrentHashMap may help; ref.equals could do only reference comparison- Author:
- raven
-
-
Field Summary
Fields Modifier and Type Field Description protected StackTraceElement[]acquisitionStackTraceprotected intactiveChildRefsprotected Map<Ref<T>,Object>childRefsprotected StackTraceElement[]closeStackTraceprotected StackTraceElement[]closeTriggerStackTraceprotected Objectcommentprotected booleanisClosedprotected RefImpl<T>parentprotected AutoCloseablereleaseActionThe release action is run once immediately when the isAlive() state changes to false.protected ObjectsynchronizerObject on which to synchronize on before any change of state of this reference.protected booleantraceAcquisitionsprotected Tvalue
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Ref<T>acquire(Object comment)Acquire a new reference with a given comment object Acquiration fails if isAlive() returns falseprotected voidcheckRelease()voidclose()static <T> Ref<T>create(T value, Object synchronizer, AutoCloseable releaseAction)static <T> Ref<T>create(T value, Object synchronizer, AutoCloseable releaseAction, Object comment)static <T> Ref<T>create2(T value, Object synchronizer, java.util.function.Consumer<? super T> closer)Create method where the close action is created from a provided lambda that accepts the valuestatic <T> Ref<T>createClosed()protected voidfinalize()TODO Switch to Java 9 Cleaner once we upgradestatic <T extends AutoCloseable>
Ref<T>fromCloseable(T value, Object synchronizer)Tget()Get the referent only iff this ref instance has not yet been closed.StackTraceElement[]getAcquisitionStackTrace()Optional operation.StackTraceElement[]getCloseStackTrace()StackTraceElement[]getCloseTriggerStackTrace()ObjectgetComment()Ref<T>getRootRef()Get the root referenceObjectgetSynchronizer()Return the object on which reference acquisition, release and the close action are synchronized on.booleanisAlive()A reference may itself be closed, but references to it may keep it alivebooleanisClosed()Check whether this reference is closedprotected voidrelease(Object childRef)StringtoString()-
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.aksw.commons.util.ref.Ref
acquire, acquireMapped
-
-
-
-
Field Detail
-
traceAcquisitions
protected boolean traceAcquisitions
-
value
protected T value
-
releaseAction
protected AutoCloseable releaseAction
The release action is run once immediately when the isAlive() state changes to false. The release action cannot 'revive' a reference as the reference is already 'dead'. The release action differs depending on how a reference was created: On the root reference, the releaseAction releases the wrapped resource On a child reference, the releaseAction releases itself (the child) from the parent one.
-
synchronizer
protected Object synchronizer
Object on which to synchronize on before any change of state of this reference. This allows for e.g. synchronizing on aMap<K, Reference<V>, such that closing a reference removes the map entry before it can be accessed and conversely, synchronizing on the map prevents the reference from becoming released. TODO Point to the ClaimingLoadingCache implementation
-
comment
protected Object comment
-
isClosed
protected volatile boolean isClosed
-
acquisitionStackTrace
protected StackTraceElement[] acquisitionStackTrace
-
closeStackTrace
protected StackTraceElement[] closeStackTrace
-
closeTriggerStackTrace
protected StackTraceElement[] closeTriggerStackTrace
-
activeChildRefs
protected volatile int activeChildRefs
-
-
Method Detail
-
finalize
protected void finalize() throws ThrowableTODO Switch to Java 9 Cleaner once we upgrade
-
getComment
public Object getComment()
-
getSynchronizer
public Object getSynchronizer()
Description copied from interface:RefReturn the object on which reference acquisition, release and the close action are synchronized on.- Specified by:
getSynchronizerin interfaceRef<T>
-
get
public T get()
Description copied from interface:RefGet the referent only iff this ref instance has not yet been closed. This method fails for closed alive refs. A closed reference is alive if it has unclosed child references. For most use cases the referent should be accessed using this method.
-
acquire
public Ref<T> acquire(Object comment)
Description copied from interface:RefAcquire a new reference with a given comment object Acquiration fails if isAlive() returns false
-
release
protected void release(Object childRef)
-
isAlive
public boolean isAlive()
Description copied from interface:RefA reference may itself be closed, but references to it may keep it alive
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceRef<T>
-
checkRelease
protected void checkRelease()
-
fromCloseable
public static <T extends AutoCloseable> Ref<T> fromCloseable(T value, Object synchronizer)
-
create2
public static <T> Ref<T> create2(T value, Object synchronizer, java.util.function.Consumer<? super T> closer)
Create method where the close action is created from a provided lambda that accepts the value
-
create
public static <T> Ref<T> create(T value, Object synchronizer, AutoCloseable releaseAction)
-
create
public static <T> Ref<T> create(T value, Object synchronizer, AutoCloseable releaseAction, Object comment)
-
createClosed
public static <T> Ref<T> createClosed()
-
isClosed
public boolean isClosed()
Description copied from interface:RefCheck whether this reference is closed
-
getRootRef
public Ref<T> getRootRef()
Description copied from interface:RefGet the root reference- Specified by:
getRootRefin interfaceRef<T>
-
getAcquisitionStackTrace
public StackTraceElement[] getAcquisitionStackTrace()
Description copied from interface:RefOptional operation. References may expose where they were acquired- Specified by:
getAcquisitionStackTracein interfaceRef<T>- Returns:
-
getCloseStackTrace
public StackTraceElement[] getCloseStackTrace()
- Specified by:
getCloseStackTracein interfaceRef<T>
-
getCloseTriggerStackTrace
public StackTraceElement[] getCloseTriggerStackTrace()
- Specified by:
getCloseTriggerStackTracein interfaceRef<T>
-
-