## edu.stanford.nlp.util
Class Interner<T>

    java.lang.Object
      edu.stanford.nlp.util.Interner<T>

    public class Interner<T>

extends Object

For interning (canonicalizing) things.

It 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 WeakHashMap, 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 Interner.globalIntern() method rather than making an instance of Interner and using the instance-level intern().

Author: Dan Klein Date: 9/28/03

Author:
  Dan Klein

Field Summary
`protected static Interner<Object>` `interner`
`protected Map<T,WeakReference<T>>` `map`

Constructor Summary
`Interner()`

Method Summary
`void` `clear()`
`static Interner<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.
`Set<T>` `internAll(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(String[] args)` Test method: interns its arguments and says whether they == themselves.
`static Interner<Object>` `setGlobal(Interner<Object> interner)` For supplying a new instance for the global methods to use.
`int` `size()`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### interner

    protected static Interner<Object> interner

### map

    protected Map<T,WeakReference<T>> map

Constructor Detail

### Interner

    public Interner()

Method Detail

### getGlobal

    public static Interner<Object> getGlobal()

  For getting the instance that global methods use.

### setGlobal

    public static Interner<Object> setGlobal(Interner<Object> interner)

  For supplying a new instance for the global methods to use.

  Returns:
    the previous global interner.

### globalIntern

    public static <T> T globalIntern(T o)

  Returns a unique object o' that .equals the argument o. If o itself is returned, this is the first request for an object .equals to o.

### clear

    public void clear()

### intern

    public T intern(T o)

  Returns a unique object o' that .equals the argument o. If o itself is returned, this is the first request for an object .equals to o.

### internAll

    public Set<T> internAll(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.

### size

    public int size()

### main

    public static void main(String[] args)

  Test method: interns its arguments and says whether they == themselves.

Stanford NLP Group