Package elki.result

Class 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.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Metadata​(java.lang.Object o)
      Constructor, use via static methods only!
    • 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.
      • global

        private static final java.util.Map<java.lang.Object,​Metadata> global
        The global metadata map.
      • 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.
    • Constructor Detail

      • Metadata

        private Metadata​(java.lang.Object o)
        Constructor, use via static methods only!
        Parameters:
        o - Object
    • 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 registered ResultListener that a new result was added.
        Parameters:
        child - New child result added
      • notifyChanged

        public void notifyChanged()
        Informs all registered ResultListener that a result has changed.
      • notifyChildRemoved

        private void notifyChildRemoved​(java.lang.Object child)
        Informs all registered ResultListener 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