public class SynchronizedInterner<T>
extends java.lang.Object
For interning (canonicalizing) things in a multi-threaded environment.
Maps any object to a unique interned version which .equals the presented object. If presented with a new object which has no previous interned version, the presented object becomes the interned version. You can tell if your object has been chosen as the new unique representative by checking whether o == intern(o). The interners use a concurrent map with weak references, meaning that if the only pointers to an interned item are the interners' backing maps, that item can still be garbage collected. Since the gc thread can silently remove things from the backing map, there's no public way to get the backing map, but feel free to add one at your own risk.
Note that in general it is just as good or better to use the static SynchronizedInterner.globalIntern() method rather than making an instance of SynchronizedInterner and using the instance-level intern().Interner
Modifier and Type | Field and Description |
---|---|
protected Interner<T> |
delegate |
protected static java.lang.Object |
globalMutex |
protected static SynchronizedInterner<java.lang.Object> |
interner |
protected java.lang.Object |
mutex |
Constructor and Description |
---|
SynchronizedInterner(Interner<T> delegate) |
SynchronizedInterner(Interner<T> delegate,
java.lang.Object mutex) |
Modifier and Type | Method and Description |
---|---|
void |
clear() |
static SynchronizedInterner<java.lang.Object> |
getGlobal()
For getting the instance that global methods use.
|
static <T> T |
globalIntern(T o)
Returns a unique object o' that .equals the argument o.
|
T |
intern(T o)
Returns a unique object o' that .equals the argument o.
|
java.util.Set<T> |
internAll(java.util.Set<T> s)
Returns a
Set such that each element in the returned set
is a unique object e' that .equals the corresponding element e in the
original set. |
static void |
main(java.lang.String[] args)
Test method: interns its arguments and says whether they == themselves.
|
static SynchronizedInterner<java.lang.Object> |
setGlobal(Interner<java.lang.Object> delegate)
For supplying a new instance for the global methods to use.
|
int |
size() |
protected static final java.lang.Object globalMutex
protected static SynchronizedInterner<java.lang.Object> interner
protected final java.lang.Object mutex
public static SynchronizedInterner<java.lang.Object> getGlobal()
public static SynchronizedInterner<java.lang.Object> setGlobal(Interner<java.lang.Object> delegate)
public static <T> T globalIntern(T o)
public void clear()
public T intern(T o)
public java.util.Set<T> internAll(java.util.Set<T> s)
Set
such that each element in the returned set
is a unique object e' that .equals the corresponding element e in the
original set.public int size()
public static void main(java.lang.String[] args) throws java.lang.InterruptedException
java.lang.InterruptedException