Class ObjectCache

java.lang.Object
org.incenp.linkml.core.ObjectCache

public class ObjectCache extends Object
Global cache for LinkML identifiable objects.

LinkML’s “globally unique objects” (objects belonging to a class that has an identifier slot; in turn, that’s a slot whose definition contains a identifier: true slot), as their name implies, are expected to be unique within a given LinkML context. (In fact they are most often expected to be universally unique without restriction to any context, but whatever may happen outside of a LinkML context is out of control of this runtime, so we do not worry about it.)

This class implements a cache where such objects, along with their identifier, may be registered and looked up afterwards.

Of note, when a LinkML class with an identifier slot has subclasses, the identifier space is shared between that class and all its subclasses. For example, in LinkML’s own meta-schema, slot definitions, class definitions, and enum definitions (among other items) all share the same identifier space (meaning that a slot definition cannot have the same identifier as a class definition or a slot definition), because they all represented by subclasses of the Element class, which is the class that carries the identifier slot.

  • Constructor Details

    • ObjectCache

      public ObjectCache()
  • Method Details

    • getObject

      public <T> T getObject(Class<T> type, String name, boolean create) throws LinkMLRuntimeException
      Looks up for an object with the specified name.
      Type Parameters:
      T - The type of object to return.
      Parameters:
      type - The type of object to return.
      name - The name of the object to return.
      create - If true, the object will be created and added to the cache if it did not already exist.
      Returns:
      The object that was in the cache or have been newly created, or null if the object was not in the cache and create is false.
      Throws:
      LinkMLRuntimeException - If the specified type of object is not one that can be cached (because it has no identifier slot), if another object with an incompatible type already exists in the cache, or if the object could not be created as needed.
    • getObject

      public <T> T getObject(Class<T> type, String name) throws LinkMLRuntimeException
      Looks up for an object with the specified name.
      Type Parameters:
      T - The type of object to return.
      Parameters:
      type - The type of object to return.
      name - The name of the object to return.
      Returns:
      The requested object, or null if the object was not in the cache or was not of the expected type.
      Throws:
      LinkMLRuntimeException - If the specified type of object is not one that can be cached, because it has no identifier slot.
    • getSize

      public int getSize()
      Gets the number of objects in the cache.