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: trueslot), 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
Elementclass, which is the class that carries the identifier slot.
-
-
Constructor Summary
Constructors Constructor Description ObjectCache()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> TgetObject(Class<T> type, String name)Looks up for an object with the specified name.<T> TgetObject(Class<T> type, String name, boolean create)Looks up for an object with the specified name.intgetSize()Gets the number of objects in the cache.
-
-
-
Method Detail
-
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- Iftrue, 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
nullif the object was not in the cache andcreateisfalse. - 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
nullif 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.
-
-