Class SharedObject
java.lang.Object
com.ibm.icu.impl.coll.SharedObject
- All Implemented Interfaces:
Cloneable
- Direct Known Subclasses:
CollationSettings
Base class for shared, reference-counted, auto-deleted objects. Java subclasses are mutable and
must implement clone().
In C++, the SharedObject base class is used for both memory and ownership management. In Java, memory management (deletion after last reference is gone) is up to the garbage collector, but the reference counter is still used to see whether the referent is the sole owner.
Usage:
class S extends SharedObject {
public clone() { ... }
}
// Use the nest class Reference (which costs an extra allocation),
class U {
// For read-only access, use s.readOnly().
// For writable access, use S ownedS = s.copyOnWrite();
private SharedObject.Reference<S> s;
public U(Reference<U> ref) {
...
}
// Returns a writable version of s.
// If there is exactly one owner, then s itself is returned.
// If there are multiple owners, then s is replaced with a clone,
// and that is returned.
private S getOwnedS() {
return s.copyOnWrite();
}
}
Either use only Java memory management, or use addRef()/removeRef(). Sharing requires
reference-counting.
TODO: Consider making this more widely available inside ICU, or else adopting a different model.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classSharedObject.Reference<T extends SharedObject>private static final classSharedObject.SharedObjectHolder<T extends SharedObject>A class wrapping SharedObject. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal voidaddRef()Increments the number of references to this object.clone()Initializes refCount to 0.final voidfinal intReturns the reference counter.final voidDecrements the number of references to this object, and auto-deletes "this" if the number becomes 0.
-
Field Details
-
refCount
-
-
Constructor Details
-
SharedObject
public SharedObject()Initializes refCount to 0.
-
-
Method Details
-
clone
-
addRef
public final void addRef()Increments the number of references to this object. Thread-safe. -
removeRef
public final void removeRef()Decrements the number of references to this object, and auto-deletes "this" if the number becomes 0. Thread-safe. -
getRefCount
public final int getRefCount()Returns the reference counter. Uses a memory barrier. -
deleteIfZeroRefCount
public final void deleteIfZeroRefCount()
-