Class WeakObjectRegistry<K,E extends K>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractSet<E>
-
- org.eclipse.rdf4j.sail.memory.model.WeakObjectRegistry<K,E>
-
- All Implemented Interfaces:
java.lang.Iterable<E>
,java.util.Collection<E>
,java.util.Set<E>
public class WeakObjectRegistry<K,E extends K> extends java.util.AbstractSet<E>
An object registry that uses weak references to keep track of the stored objects. The registry can be used to retrieve stored objects using another, equivalent object. As such, it can be used to prevent the use of duplicates in another data structure, reducing memory usage. The objects that are being stored should properly implement theObject.equals(java.lang.Object)
andObject.hashCode()
methods.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
WeakObjectRegistry.AdderBasedReadWriteLock
static class
WeakObjectRegistry.AutoCloseableIterator<E>
-
Constructor Summary
Constructors Constructor Description WeakObjectRegistry()
Constructs a new, empty object registry.WeakObjectRegistry(int cacheSize, java.util.Collection<? extends E> c)
Constructs a new WeakObjectRegistry containing the elements in the specified collection.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(E object)
void
clear()
WeakObjectRegistry.AutoCloseableIterator<E>
closeableIterator()
boolean
contains(java.lang.Object key)
E
get(K key)
Retrieves the stored object that is equal to the supplied key object.private int
getIndex(java.lang.Object key)
E
getOrAdd(K key, java.util.function.Supplier<E> supplier)
java.util.Iterator<E>
iterator()
boolean
remove(java.lang.Object object)
int
size()
-
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, retainAll, toArray, toArray, toString
-
-
-
-
Field Detail
-
logger
private static final org.slf4j.Logger logger
-
objectMap
private final java.util.Map<E extends K,java.lang.ref.WeakReference<E extends K>>[] objectMap
The hash map that is used to store the objects.
-
locks
private final WeakObjectRegistry.AdderBasedReadWriteLock[] locks
-
-
Constructor Detail
-
WeakObjectRegistry
public WeakObjectRegistry()
Constructs a new, empty object registry.
-
WeakObjectRegistry
public WeakObjectRegistry(int cacheSize, java.util.Collection<? extends E> c)
Constructs a new WeakObjectRegistry containing the elements in the specified collection.- Parameters:
c
- The collection whose elements are to be placed into this object registry.- Throws:
java.lang.NullPointerException
- If the specified collection is null.
-
-
Method Detail
-
get
public E get(K key)
Retrieves the stored object that is equal to the supplied key object.- Parameters:
key
- The object that should be used as the search key for the operation.- Returns:
- A stored object that is equal to the supplied key, or null if no such object was found.
-
getIndex
private int getIndex(java.lang.Object key)
-
closeableIterator
public WeakObjectRegistry.AutoCloseableIterator<E> closeableIterator()
-
iterator
public java.util.Iterator<E> iterator()
-
size
public int size()
-
contains
public boolean contains(java.lang.Object key)
-
add
public boolean add(E object)
-
remove
public boolean remove(java.lang.Object object)
-
-