Package org.jboss.jandex
Class StrongInternPool<E>
- java.lang.Object
-
- org.jboss.jandex.StrongInternPool<E>
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
- Direct Known Subclasses:
StrongInternPool.ByteArrayInternPool
,StrongInternPool.FieldInternPool
,StrongInternPool.MethodInternPool
,StrongInternPool.RecordComponentInternPool
,StrongInternPool.StringInternPool
,StrongInternPool.TypeArrayInternPool
,StrongInternPool.TypeInternPool
abstract class StrongInternPool<E> extends java.lang.Object implements java.lang.Cloneable, java.io.Serializable
A strong intern pool. The pool acts as a set where the first stored entry can be retrieved. This can be used to conserve memory by eliminating duplicate objects (those that are equal but have different identity). It however holds strong references to every item in the pool, so it must be cleared to allow for GC.Note: It is very important to use a smaller load factor than you normally would for HashSet, since the implementation is open-addressed with linear probing. With a 50% load-factor a get is expected to return in only 2 probes. However, a 90% load-factor is expected to return in around 50 probes.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
StrongInternPool.ByteArrayInternPool
private static class
StrongInternPool.FieldInternPool
private class
StrongInternPool.IdentityHashSetIterator
class
StrongInternPool.Index
private static class
StrongInternPool.MethodInternPool
private static class
StrongInternPool.RecordComponentInternPool
private static class
StrongInternPool.StringInternPool
private static class
StrongInternPool.TypeArrayInternPool
private static class
StrongInternPool.TypeInternPool
-
Field Summary
Fields Modifier and Type Field Description private static int
DEFAULT_CAPACITY
Same default as HashMap, must be a power of 2private static float
DEFAULT_LOAD_FACTOR
67%, just like IdentityHashMapprivate java.lang.Class<E>
elementType
The class of values that may be present intable
.private StrongInternPool.Index
index
Cache for an indexprivate float
loadFactor
The user defined load factor which defines when to resizeprivate static int
MAXIMUM_CAPACITY
MAX_INT - 1private int
modCount
Counter used to detect changes made outside of an iteratorprivate static java.lang.Object
NULL
Marks null keys.private static long
serialVersionUID
Serialization IDprivate int
size
The current number of key-value pairsprivate java.lang.Object[]
table
The open-addressed tableprivate int
threshold
The next resize
-
Constructor Summary
Constructors Constructor Description StrongInternPool(java.lang.Class<E> elementType)
StrongInternPool(java.lang.Class<E> elementType, int initialCapacity)
StrongInternPool(java.lang.Class<E> elementType, int initialCapacity, float loadFactor)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
clear()
StrongInternPool<E>
clone()
boolean
contains(java.lang.Object entry)
(package private) boolean
eq(java.lang.Object o1, java.lang.Object o2)
(package private) abstract boolean
equality(E o1, E o2)
(package private) static StrongInternPool<byte[]>
forByteArrays()
(package private) static StrongInternPool<FieldInternal>
forFields()
(package private) static StrongInternPool<MethodInternal>
forMethods()
(package private) static StrongInternPool<RecordComponentInternal>
forRecordComponents()
(package private) static StrongInternPool<java.lang.String>
forStrings()
(package private) static StrongInternPool<Type[]>
forTypeArrays()
(package private) static StrongInternPool<Type>
forTypes()
(package private) int
hash(java.lang.Object o)
(package private) abstract int
hashCode(E o)
StrongInternPool.Index
index()
private static int
index(int hashCode, int length)
private void
init(int initialCapacity, float loadFactor)
E
intern(E entry)
Internalizes the specified object by always returning the first ever stored.boolean
isEmpty()
java.util.Iterator<E>
iterator()
private static <K> K
maskNull(K key)
(package private) boolean
mayContain(java.lang.Object o)
private int
nextIndex(int index, int length)
private int
offset(java.lang.Object entry)
void
printDebugStats()
private void
putForCreate(E entry)
private void
readObject(java.io.ObjectInputStream s)
private void
relocate(int start)
boolean
remove(java.lang.Object o)
private void
resize(int from)
int
size()
java.lang.Object[]
toInternalArray()
Advanced method that returns the internal table.java.lang.String
toString()
private static <K> K
unmaskNull(K key)
private void
writeObject(java.io.ObjectOutputStream s)
-
-
-
Field Detail
-
NULL
private static final java.lang.Object NULL
Marks null keys.
-
serialVersionUID
private static final long serialVersionUID
Serialization ID- See Also:
- Constant Field Values
-
DEFAULT_CAPACITY
private static final int DEFAULT_CAPACITY
Same default as HashMap, must be a power of 2- See Also:
- Constant Field Values
-
MAXIMUM_CAPACITY
private static final int MAXIMUM_CAPACITY
MAX_INT - 1- See Also:
- Constant Field Values
-
DEFAULT_LOAD_FACTOR
private static final float DEFAULT_LOAD_FACTOR
67%, just like IdentityHashMap- See Also:
- Constant Field Values
-
elementType
private final java.lang.Class<E> elementType
The class of values that may be present intable
.
-
table
private transient java.lang.Object[] table
The open-addressed tableMust be
Object[]
instead ofE[]
to allow storing theNULL
marker.
-
size
private transient int size
The current number of key-value pairs
-
threshold
private transient int threshold
The next resize
-
loadFactor
private final float loadFactor
The user defined load factor which defines when to resize
-
modCount
private transient int modCount
Counter used to detect changes made outside of an iterator
-
index
private transient StrongInternPool.Index index
Cache for an index
-
-
Method Detail
-
init
private void init(int initialCapacity, float loadFactor)
-
mayContain
boolean mayContain(java.lang.Object o)
-
eq
boolean eq(java.lang.Object o1, java.lang.Object o2)
-
hash
int hash(java.lang.Object o)
-
hashCode
abstract int hashCode(E o)
-
maskNull
private static <K> K maskNull(K key)
-
unmaskNull
private static <K> K unmaskNull(K key)
-
nextIndex
private int nextIndex(int index, int length)
-
index
private static int index(int hashCode, int length)
-
size
public int size()
-
isEmpty
public boolean isEmpty()
-
contains
public boolean contains(java.lang.Object entry)
-
offset
private int offset(java.lang.Object entry)
-
intern
public E intern(E entry)
Internalizes the specified object by always returning the first ever stored. Equal objects with different identity (aka duplicates) can be eliminated with this method.- Parameters:
entry
- the object to internalize- Returns:
- the one true unique object (equal to
entry
)
-
resize
private void resize(int from)
-
remove
public boolean remove(java.lang.Object o)
-
relocate
private void relocate(int start)
-
clear
public void clear()
-
clone
public StrongInternPool<E> clone()
- Overrides:
clone
in classjava.lang.Object
-
toInternalArray
public java.lang.Object[] toInternalArray()
Advanced method that returns the internal table. The resulting array will contain nulls at random places that must be skipped. In addition, it will not operate correctly if a null was inserted into the set. Use at your own risk....- Returns:
- an array containing elements in this set along with randomly placed nulls,
-
printDebugStats
public void printDebugStats()
-
readObject
private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, java.lang.ClassNotFoundException
- Throws:
java.io.IOException
java.lang.ClassNotFoundException
-
putForCreate
private void putForCreate(E entry)
-
writeObject
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException
- Throws:
java.io.IOException
-
iterator
public java.util.Iterator<E> iterator()
-
index
public StrongInternPool.Index index()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
forByteArrays
static StrongInternPool<byte[]> forByteArrays()
-
forStrings
static StrongInternPool<java.lang.String> forStrings()
-
forTypes
static StrongInternPool<Type> forTypes()
-
forTypeArrays
static StrongInternPool<Type[]> forTypeArrays()
-
forMethods
static StrongInternPool<MethodInternal> forMethods()
-
forFields
static StrongInternPool<FieldInternal> forFields()
-
forRecordComponents
static StrongInternPool<RecordComponentInternal> forRecordComponents()
-
-