Class SlotHelper<T>

java.lang.Object
org.incenp.obofoundry.sssom.slots.SlotHelper<T>
Type Parameters:
T - The SSSOM object (Mapping or MappingSet) this class is intended to ease the manipulation of.

public class SlotHelper<T> extends Object
A class to facilitate the manipulation of SSSOM slots. This class is mostly intended to hide all the hideous hacks relying on Java reflection.

It provides a visitor-like pattern to easily apply treatments on all slots of a mapping or a mapping set without having to explicitly call the accessor for each slot. For example, to “visit” all (non-null) slots of a Mapping object, serialise them as strings, and collect said strings:

 ISlotVisitor<Mapping,String> myVisitor = ...;
 Mapping mapping = ...;
 
 List<String> slotsAsStrings = SlotHelper.getMappingHelper().visitSlots(mapping, myVisitor);
 
See Also:
  • Method Details

    • getMappingHelper

      public static SlotHelper<Mapping> getMappingHelper()
      Gets the default helper object to manipulate Mapping slots. The default helper is set to visit all slots in the default order.
      Returns:
      The default slot helper for Mapping objects.
    • getMappingHelper

      public static SlotHelper<Mapping> getMappingHelper(boolean newHelper)
      Gets a helper object to manipulate Mapping slots. Use this method to get a distinct helper from the default one. Settings of the returned object can then be modified without altering the default helper.
      Parameters:
      newHelper - true to return a distinct helper object, or false to return the default helper.
      Returns:
      A helper for Mapping objects.
    • getMappingSetHelper

      public static SlotHelper<MappingSet> getMappingSetHelper()
      Gets the default helper object to manipulate MappingSet slots. The default helper is set to visit all slots in the default order.
      Returns:
      The default slot helper for MappingSet objects.
    • getMappingSetHelper

      public static SlotHelper<MappingSet> getMappingSetHelper(boolean newHelper)
      Gets a helper object to manipulate MappingSet slots. Use this method to get a distinct helper from the default one. Settings of the returned object can then be modified without altering the default helper.
      Parameters:
      newHelper - true to return a distinct helper object, or false to return the default helper.
      Returns:
      A helper for MappingSet objects.
    • setAlphabeticalOrder

      public void setAlphabeticalOrder()
      Configures this helper to visit slots in alphabetical order.
    • setSlots

      public void setSlots(List<String> slotNames, boolean forceOrder)
      Explicitly sets the list of slots to visit.
      Parameters:
      slotNames - The names of the slots that this object should visit.
      forceOrder - If true, slots will be visited in the order they appear in the list; otherwise, the order will remain unchanged.
    • setSlots

      public void setSlots(Collection<String> slotNames)
      Explicitly sets the list of slots to visit.
      Parameters:
      slotNames - The names of the slots that this object should visit.
    • excludeSlots

      public void excludeSlots(Collection<String> slotNames)
      Excludes the specified slots from being visited.
      Parameters:
      slotNames - The names of the slots that this object should not visit.
    • getSlots

      public List<Slot<T>> getSlots()
      Gets the current list of slots set to be visited. They are listed in the order they would be visited.
      Returns:
      The current slots list.
    • getSlotNames

      public List<String> getSlotNames()
      Gets the current list of slot names to be visited. They are listed in the order they would be visited.
      Returns:
      The current slot names list.
    • getSlotByName

      public Slot<T> getSlotByName(String name)
      Finds a slot by its name.
      Parameters:
      name - The name of the slot to find, as per the SSSOM specification.
      Returns:
      The corresponding slot, or null if the given name is not a valid slot name.
    • getSlotsByName

      public Collection<Slot<T>> getSlotsByName(Collection<String> names)
      Finds slots by their names.

      This is a convenience method, basically to avoid calling getSlotByName(String) repeatedly.

      Parameters:
      names - The names of the slots to find, as per the SSSOM specification. If a given name is not a valid slot name, it is ignored.
      Returns:
      The corresponding slots.
    • getSlotByURI

      public Slot<T> getSlotByURI(String uri)
      Finds a slot by its associated URI.
      Parameters:
      uri - The URI of the slot to find.
      Returns:
      The corresponding slot, or null if the given URI is not the URI of a SSSOM slot.
    • visitSlots

      public void visitSlots(T object, ISlotVisitor<T> visitor)
      Visits the slots of a given object.
      Parameters:
      object - The object whose slots should be visited.
      visitor - The visitor to use.
    • visitSlots

      public void visitSlots(T object, ISlotVisitor<T> visitor, boolean visitNull)
      Visits the slots of a given object.
      Parameters:
      object - The object whose slots should be visited.
      visitor - The visitor to use.
      visitNull - If true, slots with a null value will be visited as well.
    • visitSlots

      public <V> List<V> visitSlots(T object, ISimpleSlotVisitor<T,V> visitor)
      Visits the slots of a given object, with a visitor that does not distinguish between slot types.
      Type Parameters:
      V - The type of object the visitor should return for each slot.
      Parameters:
      object - The object whose slots should be visited.
      visitor - The visitor to use.
      Returns:
      A list of all values returned by the visitor for each slot.
    • visitSlots

      public <V> List<V> visitSlots(T object, ISimpleSlotVisitor<T,V> visitor, boolean visitNull)
      Visits the slots of a given object, with a visitor that does not distinguish between slot types.
      Type Parameters:
      V - The type of objects the visitor should return for each slot.
      Parameters:
      object - The object whose slots should be visited.
      visitor - The visitor to use.
      visitNull - If true, slots with a null value will be visited as well.
      Returns:
      A list of all values returned by the visitor for each slot.
    • expandIdentifiers

      public void expandIdentifiers(T object, PrefixManager prefixManager)
      Expands identifiers in all slots holding entity references.
      Parameters:
      object - The object whose entity reference slots should be visited.
      prefixManager - The prefix manager to use to expand the short identifiers.
    • getMappingSlotList

      public static Collection<String> getMappingSlotList(String spec)
      Helper method to construct a list of slot names from a string specification.

      The string is expected to be a comma-separated list of names. Each name should be the name of the slot to include, unless it is prefixed by a -, in which case the slot is to be excluded. The following special names can also be used:

      • all: represents all slots;
      • mapping: represents the slots that are about the mapping itself (subject_id, predicate_id, object_id;
      • {code metadata}: represents the slots that are about the mapping metadata (all slots except the above three).

      Examples:

       subject_id,object_id
       
      will construct a list comprising only the subject_id and object_id slots;
       all,-predicate_id
       
      will construct a list comprising all slots except predicate_id;
       mapping,mapping_provider
       
      will construct a list comprising the three slots describing the mapper proper (subject_id, predicate_id, object_id) plus mapping_provider;
      Parameters:
      spec - A textual specification of the slots list.
      Returns:
      The corresponding list of slot names.