Class PolymorphicRegistry<U>

java.lang.Object
com.aoapps.collections.PolymorphicRegistry<U>

public class PolymorphicRegistry<U> extends Object
A registry of objects by their class, along with all their parent classes and interfaces, up to and including an upper bound. The registry is highly concurrent, and performs registry lookups in O(1).
Author:
AO Industries, Inc.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(U instance)
    Adds a instance to the registry.
    <T extends U>
    List<T>
    get(Class<T> clazz)
    Gets all instances registered of the given class.
    <T extends U>
    List<T>
    get(Class<T> clazz, Predicate<? super T> filter)
    Gets all instances registered of the given class that match the given filter.
    <T extends U>
    T
    getFirst(Class<T> clazz)
    Gets the first instance registered of the given class.
    <T extends U>
    T
    getFirst(Class<T> clazz, Predicate<? super T> filter)
    Gets the first instance registered of the given class that match the given filter.
    <T extends U>
    T
    getLast(Class<T> clazz)
    Gets the last instance registered of the given class.
    <T extends U>
    T
    getLast(Class<T> clazz, Predicate<? super T> filter)
    Gets the last instance registered of the given class that match the given filter.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • PolymorphicRegistry

      public PolymorphicRegistry(Class<U> upperBound)
  • Method Details

    • add

      public void add(U instance)
      Adds a instance to the registry. The instance is registered under all classes and interfaces it extends and implements, up to and including the upper bound U.

      This implementation favors lookup speed at O(1), and pays the price during add(java.lang.Object).

    • get

      public <T extends U> List<T> get(Class<T> clazz)
      Gets all instances registered of the given class. They are returned in the order registered. When an object is registered more than once, it will appear in the list multiple times. The list is a snapshot and will not change over time.
      Returns:
      the unmodifiable list of all objects registered of the given class, or an empty list when none registered
    • get

      public <T extends U> List<T> get(Class<T> clazz, Predicate<? super T> filter)
      Gets all instances registered of the given class that match the given filter. They are returned in the order registered. When an object is registered more than once, it will appear in the list multiple times. The list is a snapshot and will not change over time.
      Returns:
      the unmodifiable list of all objects registered of the given class that match the filter, or an empty list when none registered
    • getFirst

      public <T extends U> T getFirst(Class<T> clazz)
      Gets the first instance registered of the given class.
      Returns:
      the first instance registered or null for none registered
    • getFirst

      public <T extends U> T getFirst(Class<T> clazz, Predicate<? super T> filter)
      Gets the first instance registered of the given class that match the given filter.
      Returns:
      the first instance registered that matches the filter or null for none registered
    • getLast

      public <T extends U> T getLast(Class<T> clazz)
      Gets the last instance registered of the given class.
      Returns:
      the last instance registered or null for none registered
    • getLast

      public <T extends U> T getLast(Class<T> clazz, Predicate<? super T> filter)
      Gets the last instance registered of the given class that match the given filter.
      Returns:
      the last instance registered that matches the filter or null for none registered