Class Metadata
- java.lang.Object
-
- java.lang.ref.Reference<T>
-
- java.lang.ref.WeakReference<java.lang.Object>
-
- elki.result.Metadata
-
public class Metadata extends java.lang.ref.WeakReference<java.lang.Object>
Metadata management class.This manages metadata in a global WeakHashMap. Prior to this, we had dedicated Result types, which would store result hierarchy, names, etc. but this caused quite an inheritance and dependency mess. But in particular, it meant all objects returned needed to implement these interfaces.
Therefore, we are now experimenting storing metadata using a weak hash map, so that it can be garbage collected, yet metadata can be attached to arbitrary objects.
Be careful to not overuse this: as this class needs synchronization, there is cost associated with retrieving the metadata of an object.
We also chose to not synchronize all operations; so concurrent changes to the result hierarchy while traversing it may cause confusion.
If you store a WeakReference in the hierarchy, the iterators will automatically call
get()
.TODO: add a modCounter, and fail if concurrent modifications happen?
- Since:
- 0.8.0
- Author:
- Erich Schubert
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
Metadata.CleanerThread
Cleanup thread.private static class
Metadata.EagerIt<O>
Base class for iterators that need to look ahead, e.g., to check conditions on the next element.class
Metadata.Hierarchy
Class to represent hierarchy information.
-
Field Summary
Fields Modifier and Type Field Description private static Metadata.CleanerThread
CLEANER
Cleanup thread.private static java.lang.Object[]
EMPTY_CHILDREN
Empty list.private static Metadata[]
EMPTY_PARENTS
Empty list.private static java.util.Map<java.lang.Object,Metadata>
global
The global metadata map.private Metadata.Hierarchy
hierarchy
Hierarchy information.private java.util.ArrayList<ResultListener>
listeners
Attached result listenersprivate static Logging
LOG
Class loggerprivate java.lang.String
name
Human-readable name of the entry.private static java.lang.ref.ReferenceQueue<? super java.lang.Object>
queue
Reference queue for CLEANER.
-
Constructor Summary
Constructors Modifier Constructor Description private
Metadata(java.lang.Object o)
Constructor, use via static methods only!
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addResultListener(ResultListener listener)
Register a result listener.private void
cleanup()
Cleanup function to help garbage collection.private static java.lang.Object
deref(java.lang.Object ret)
Automatically expand a reference.private void
doNotify(java.util.function.Consumer<ResultListener> f)
Notify, also via all parent listeners.static void
expungeStaleEntries()
Expunges stale entries from the table.static Metadata
get(java.lang.Object o)
Get but do not create the Metadata of an object.java.lang.String
getLongName()
Get the long name of a result.Metadata.Hierarchy
hierarchy()
Access the objects hierarchy information.static Metadata.Hierarchy
hierarchyOf(java.lang.Object o)
Get or create the Hierarchy of an object.void
notifyChanged()
Informs all registeredResultListener
that a result has changed.private void
notifyChildAdded(java.lang.Object child)
Informs all registeredResultListener
that a new result was added.private void
notifyChildRemoved(java.lang.Object child)
Informs all registeredResultListener
that a new result has been removed.static Metadata
of(java.lang.Object o)
Get or create the Metadata of an object.void
removeResultListener(ResultListener listener)
Remove a result listener.void
setLongName(java.lang.String name)
Set the long name of a result.
-
-
-
Field Detail
-
LOG
private static final Logging LOG
Class logger
-
queue
private static java.lang.ref.ReferenceQueue<? super java.lang.Object> queue
Reference queue for CLEANER.
-
CLEANER
private static Metadata.CleanerThread CLEANER
Cleanup thread.
-
global
private static final java.util.Map<java.lang.Object,Metadata> global
The global metadata map.
-
hierarchy
private Metadata.Hierarchy hierarchy
Hierarchy information.
-
listeners
private java.util.ArrayList<ResultListener> listeners
Attached result listeners
-
name
private java.lang.String name
Human-readable name of the entry.
-
EMPTY_CHILDREN
private static final java.lang.Object[] EMPTY_CHILDREN
Empty list.
-
EMPTY_PARENTS
private static final Metadata[] EMPTY_PARENTS
Empty list.
-
-
Method Detail
-
of
public static Metadata of(java.lang.Object o)
Get or create the Metadata of an object.- Parameters:
o
- Object- Returns:
- Metadata
-
get
public static Metadata get(java.lang.Object o)
Get but do not create the Metadata of an object.- Parameters:
o
- Object- Returns:
- Metadata
-
hierarchyOf
public static Metadata.Hierarchy hierarchyOf(java.lang.Object o)
Get or create the Hierarchy of an object.- Parameters:
o
- Object- Returns:
- Hierarchy
-
expungeStaleEntries
public static void expungeStaleEntries()
Expunges stale entries from the table.
-
cleanup
private void cleanup()
Cleanup function to help garbage collection.
-
hierarchy
public Metadata.Hierarchy hierarchy()
Access the objects hierarchy information.- Returns:
- Hierarchy information
-
setLongName
public void setLongName(java.lang.String name)
Set the long name of a result.- Parameters:
name
- Result name
-
getLongName
public java.lang.String getLongName()
Get the long name of a result.- Returns:
- name
-
deref
private static java.lang.Object deref(java.lang.Object ret)
Automatically expand a reference.- Parameters:
ret
- Object- Returns:
- Referenced object (may be null!) or ret.
-
addResultListener
public void addResultListener(ResultListener listener)
Register a result listener.- Parameters:
listener
- Result listener.
-
removeResultListener
public void removeResultListener(ResultListener listener)
Remove a result listener.- Parameters:
listener
- Result listener.
-
notifyChildAdded
private void notifyChildAdded(java.lang.Object child)
Informs all registeredResultListener
that a new result was added.- Parameters:
child
- New child result added
-
notifyChanged
public void notifyChanged()
Informs all registeredResultListener
that a result has changed.
-
notifyChildRemoved
private void notifyChildRemoved(java.lang.Object child)
Informs all registeredResultListener
that a new result has been removed.- Parameters:
child
- result that has been removed
-
doNotify
private void doNotify(java.util.function.Consumer<ResultListener> f)
Notify, also via all parent listeners.- Parameters:
f
- Listener consumer
-
-