Package org.drasyl.util
Class InternPool<T>
- java.lang.Object
-
- org.drasyl.util.InternPool<T>
-
- Type Parameters:
T
- Type of objects in the pool
public class InternPool<T> extends Object
Represents a pool of unique objects of typeT
. Should only be used if objects of typeT
are immutable and have aObject.equals(Object)
implementation, which returnstrue
only if both objects have the same content.Inspired by: https://github.com/verhas/intern
-
-
Constructor Summary
Constructors Constructor Description InternPool()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description T
intern(T object)
Returns a canonical representation for the object.
-
-
-
Method Detail
-
intern
public T intern(T object)
Returns a canonical representation for the object.A pool of objects of type
T
, initially empty, is maintained privately by this class.When the intern method is invoked, if the pool already contains a object equal to
object
as determined by theObject.equals(Object)
method, then the object from the pool is returned. Otherwise,object
is added to the pool and a reference toobject
is returned.It follows that for any two objects
a
andb
,intern(a) == intern(b)
istrue
if and only ifa.equals(b)
istrue
.- Returns:
- a object that has the same contents as
object
, but is guaranteed to be from a pool of unique objects of typeT
.
-
-