public class SharedObject extends Object implements Cloneable
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() { ... }
}
// Either use the nest class Reference (which costs an extra allocation),
// or duplicate its code in the class that uses S
// (which duplicates code and is more error-prone).
class U {
// For read-only access, use s.readOnly().
// For writable access, use S ownedS = s.copyOnWrite();
private SharedObject.Reference<S> s;
// 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();
}
public U clone() {
...
c.s = s.clone();
...
}
}
class V {
// For read-only access, use s directly.
// For writable access, use S ownedS = getOwnedS();
private S s;
// 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() {
if(s.getRefCount() > 1) {
S ownedS = s.clone();
s.removeRef();
s = ownedS;
ownedS.addRef();
}
return s;
}
public U clone() {
...
s.addRef();
...
}
protected void finalize() {
...
if(s != null) {
s.removeRef();
s = null;
}
...
}
}
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.| Modifier and Type | Class and Description |
|---|---|
static class |
SharedObject.Reference<T extends SharedObject>
Similar to a smart pointer, basically a port of the static methods of C++ SharedObject.
|
| Constructor and Description |
|---|
SharedObject()
Initializes refCount to 0.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addRef()
Increments the number of references to this object.
|
SharedObject |
clone()
Initializes refCount to 0.
|
void |
deleteIfZeroRefCount() |
int |
getRefCount()
Returns the reference counter.
|
void |
removeRef()
Decrements the number of references to this object,
and auto-deletes "this" if the number becomes 0.
|
public SharedObject clone()
public final void addRef()
public final void removeRef()
public final int getRefCount()
public final void deleteIfZeroRefCount()