public interface ReferenceCounted extends ReferenceOwner
INIT, TMP
Modifier and Type | Method and Description |
---|---|
void |
addReferenceChangeListener(ReferenceChangeListener referenceChangeListener)
Adds a
ReferenceChangeListener that will be notified whenever the reference count changes. |
int |
refCount()
Returns the current reference count of the resource.
|
void |
release(ReferenceOwner id)
Releases the resource by decrementing its reference count by one.
|
default void |
releaseLast()
Releases the resource for the initial owner and ensures that this release is the last usage of the resource.
|
void |
releaseLast(ReferenceOwner id)
Releases the resource and ensures that this release is the last usage of the resource.
|
void |
removeReferenceChangeListener(ReferenceChangeListener referenceChangeListener)
Removes a
ReferenceChangeListener previously added via addReferenceChangeListener(ReferenceChangeListener) . |
void |
reserve(ReferenceOwner id)
Reserves the resource by incrementing its reference count by one.
|
boolean |
reservedBy(ReferenceOwner owner)
Deprecated.
This method is deprecated and may be removed in future versions.
|
default void |
reserveTransfer(ReferenceOwner from,
ReferenceOwner to)
Atomically transfers a reservation from one owner to another by incrementing the reference
count for the new owner and decrementing it for the old owner.
|
boolean |
tryReserve(ReferenceOwner id)
Attempts to reserve the resource and returns
true if successful. |
referenceId, referenceName, temporary
void reserve(ReferenceOwner id) throws ClosedIllegalStateException, ThreadingIllegalStateException
It is required to reserve a resource before using it to prevent it from being freed.
id
- The unique identifier representing the owner reserving the resource.ClosedIllegalStateException
- If the resource has been released or closed.
I.e. its reference counter has as some point reached zero.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe waydefault void reserveTransfer(ReferenceOwner from, ReferenceOwner to) throws ClosedIllegalStateException, ThreadingIllegalStateException
from
- The unique identifier representing the owner releasing the reservation.to
- The unique identifier representing the owner acquiring the reservation.ClosedIllegalStateException
- If the resource has been released or closed.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe wayboolean tryReserve(ReferenceOwner id) throws ClosedIllegalStateException, IllegalArgumentException
true
if successful.
Unlike reserve(ReferenceOwner)
, this method will not throw an exception if the resource
is already freed.id
- The unique identifier representing the owner attempting to reserve the resource.true
if the resource was successfully reserved, false
otherwise.IllegalArgumentException
- If the reference owner is invalid.ClosedIllegalStateException
- If the resource has been released or closed.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe way@Deprecated boolean reservedBy(ReferenceOwner owner) throws ClosedIllegalStateException
true
if unsure.
This method provides a best-effort check and may not be accurate.
owner
- The unique identifier representing the owner to check.false
if it is certain that the owner does not have the resource reserved; true
otherwise.ClosedIllegalStateException
- If the resource has been released or closed.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe wayvoid release(ReferenceOwner id) throws ClosedIllegalStateException
id
- The unique identifier representing the owner releasing the resource.ClosedIllegalStateException
- If the resource has been released or closed.
I.e. its reference counter has as some point reached zero.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe wayvoid releaseLast(ReferenceOwner id) throws ClosedIllegalStateException
id
- The unique identifier representing the owner releasing the resource.ClosedIllegalStateException
- If the resource has been released or closed.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe waydefault void releaseLast() throws ClosedIllegalStateException
ClosedIllegalStateException
- If the resource has been released or closed.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe wayint refCount()
void addReferenceChangeListener(ReferenceChangeListener referenceChangeListener)
ReferenceChangeListener
that will be notified whenever the reference count changes.
This can be used to monitor the usage of the resource and execute code when certain conditions are met.referenceChangeListener
- The listener that will receive notifications of reference count changes.void removeReferenceChangeListener(ReferenceChangeListener referenceChangeListener)
ReferenceChangeListener
previously added via addReferenceChangeListener(ReferenceChangeListener)
.
Note: Object equality is used to determine which listener to remove, so be cautious if the listener implements the equals method in a non-standard way.
referenceChangeListener
- The listener to remove.Copyright © 2023. All rights reserved.