Class TreeModel

All Implemented Interfaces:
Serializable, Iterable<Statement>, Collection<Statement>, Set<Statement>, SortedSet<Statement>, Model, NamespaceAware

public class TreeModel extends AbstractModel implements SortedSet<Statement>
A Red-Black tree based Model implementation. The model is sorted according to the lexical ordering of terms.

This implementation provides guaranteed log(n) time cost for filtered access by any number of terms. If an index is not yet available for a set of positions, it is created at runtime using a TreeSet.

Note that this implementation is not synchronized. If multiple threads access a model concurrently, even if all of them are read operations, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the model. If no such object exists, the set should be "wrapped" using the Models.synchronizedModel method.

Author:
James Leigh
See Also:
  • Constructor Details

  • Method Details

    • getNamespace

      public Optional<Namespace> getNamespace(String prefix)
      Description copied from interface: NamespaceAware
      Gets the namespace that is associated with the specified prefix, if any. If multiple namespaces match the given prefix, the result may not be consistent over successive calls to this method.
      Specified by:
      getNamespace in interface NamespaceAware
      Parameters:
      prefix - A namespace prefix.
      Returns:
      The namespace name that is associated with the specified prefix, or Optional.empty() if there is no such namespace.
    • getNamespaces

      public Set<Namespace> getNamespaces()
      Description copied from interface: NamespaceAware
      Gets the set that contains the assigned namespaces.
      Specified by:
      getNamespaces in interface NamespaceAware
      Returns:
      A Set containing the Namespace objects that are available.
    • setNamespace

      public Namespace setNamespace(String prefix, String name)
      Description copied from interface: Model
      Sets the prefix for a namespace. This will replace any existing namespace associated to the prefix.
      Specified by:
      setNamespace in interface Model
      Parameters:
      prefix - The new prefix.
      name - The namespace name that the prefix maps to.
      Returns:
      The Namespace object for the given namespace.
    • setNamespace

      public void setNamespace(Namespace namespace)
      Description copied from interface: Model
      Sets the prefix for a namespace. This will replace any existing namespace associated to the prefix.
      Specified by:
      setNamespace in interface Model
      Parameters:
      namespace - A Namespace object to use in this Model.
    • removeNamespace

      public Optional<Namespace> removeNamespace(String prefix)
      Description copied from interface: Model
      Removes a namespace declaration by removing the association between a prefix and a namespace name.
      Specified by:
      removeNamespace in interface Model
      Parameters:
      prefix - The namespace prefix of which the assocation with a namespace name is to be removed.
      Returns:
      the previous namespace bound to the prefix or Optional.empty()
    • size

      public int size()
      Specified by:
      size in interface Collection<Statement>
      Specified by:
      size in interface Set<Statement>
      Specified by:
      size in class AbstractCollection<Statement>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<Statement>
      Specified by:
      clear in interface Set<Statement>
      Overrides:
      clear in class AbstractModel
    • comparator

      public Comparator<? super Statement> comparator()
      Specified by:
      comparator in interface SortedSet<Statement>
    • first

      public Statement first()
      Specified by:
      first in interface SortedSet<Statement>
    • last

      public Statement last()
      Specified by:
      last in interface SortedSet<Statement>
    • lower

      public Statement lower(Statement e)
    • floor

      public Statement floor(Statement e)
    • ceiling

      public Statement ceiling(Statement e)
    • higher

      public Statement higher(Statement e)
    • pollFirst

      public Statement pollFirst()
    • pollLast

      public Statement pollLast()
    • subSet

      public SortedSet<Statement> subSet(Statement fromElement, Statement toElement)
      Specified by:
      subSet in interface SortedSet<Statement>
    • headSet

      public SortedSet<Statement> headSet(Statement toElement)
      Specified by:
      headSet in interface SortedSet<Statement>
    • tailSet

      public SortedSet<Statement> tailSet(Statement fromElement)
      Specified by:
      tailSet in interface SortedSet<Statement>
    • add

      public boolean add(Resource subj, IRI pred, Value obj, Resource... contexts)
      Description copied from interface: Model
      Adds one or more statements to the model. This method creates a statement for each specified context and adds those to the model. If no contexts are specified, a single statement with no associated context is added. If this Model is a filtered Model then null (if context empty) values are permitted and will use the corresponding filtered values.
      Specified by:
      add in interface Model
      Parameters:
      subj - The statement's subject.
      pred - The statement's predicate.
      obj - The statement's object.
      contexts - The contexts to add statements to.
    • contains

      public boolean contains(Resource subj, IRI pred, Value obj, Resource... contexts)
      Description copied from interface: Model
      Determines if statements with the specified subject, predicate, object and (optionally) context exist in this model. The subject, predicate and object parameters can be null to indicate wildcards. The contexts parameter is a wildcard and accepts zero or more values. If no contexts are specified, statements will match disregarding their context. If one or more contexts are specified, statements with a context matching one of these will match. Note: to match statements without an associated context, specify the value null and explicitly cast it to type Resource.

      Examples: model.contains(s1, null, null) is true if any statements in this model have subject s1,
      model.contains(null, null, null, c1) is true if any statements in this model have context c1,
      model.contains(null, null, null, (Resource)null) is true if any statements in this model have no associated context,
      model.contains(null, null, null, c1, c2, c3) is true if any statements in this model have context c1, c2 or c3 .

      Specified by:
      contains in interface Model
      Parameters:
      subj - The subject of the statements to match, null to match statements with any subject.
      pred - The predicate of the statements to match, null to match statements with any predicate.
      obj - The object of the statements to match, null to match statements with any object.
      contexts - The contexts of the statements to match. If no contexts are specified, statements will match disregarding their context. If one or more contexts are specified, statements with a context matching one of these will match.
      Returns:
      true if statements match the specified pattern.
    • remove

      public boolean remove(Resource subj, IRI pred, Value obj, Resource... contexts)
      Description copied from interface: Model
      Removes statements with the specified subject, predicate, object and (optionally) context exist in this model. The subject, predicate and object parameters can be null to indicate wildcards. The contexts parameter is a wildcard and accepts zero or more values. If no contexts are specified, statements will be removed disregarding their context. If one or more contexts are specified, statements with a context matching one of these will be removed. Note: to remove statements without an associated context, specify the value null and explicitly cast it to type Resource.

      Examples: model.remove(s1, null, null) removes any statements in this model have subject s1,
      model.remove(null, null, null, c1) removes any statements in this model have context c1 ,
      model.remove(null, null, null, (Resource)null) removes any statements in this model have no associated context,
      model.remove(null, null, null, c1, c2, c3) removes any statements in this model have context c1, c2 or c3.

      Specified by:
      remove in interface Model
      Parameters:
      subj - The subject of the statements to remove, null to remove statements with any subject.
      pred - The predicate of the statements to remove, null to remove statements with any predicate.
      obj - The object of the statements to remove, null to remove statements with any object.
      contexts - The contexts of the statements to remove. If no contexts are specified, statements will be removed disregarding their context. If one or more contexts are specified, statements with a context matching one of these will be removed.
      Returns:
      true if one or more statements have been removed.
    • iterator

      public Iterator<Statement> iterator()
      Specified by:
      iterator in interface Collection<Statement>
      Specified by:
      iterator in interface Iterable<Statement>
      Specified by:
      iterator in interface Set<Statement>
      Specified by:
      iterator in class AbstractCollection<Statement>
    • filter

      public Model filter(Resource subj, IRI pred, Value obj, Resource... contexts)
      Description copied from interface: Model
      Returns a filtered view of the statements with the specified subject, predicate, object and (optionally) context. The subject, predicate and object parameters can be null to indicate wildcards. The contexts parameter is a wildcard and accepts zero or more values. If no contexts are specified, statements will match disregarding their context. If one or more contexts are specified, statements with a context matching one of these will match. Note: to match statements without an associated context, specify the value null and explicitly cast it to type Resource.

      The returned model is backed by this Model, so changes to this Model are reflected in the returned model, and vice-versa. If this Model is modified while an iteration over the returned model is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The model supports element removal, which removes the corresponding statement from this Model, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. The statements passed to the add and addAll operations must match the parameter pattern.

      Examples: model.filter(s1, null, null) matches all statements that have subject s1,
      model.filter(null, null, null, c1) matches all statements that have context c1,
      model.filter(null, null, null, (Resource)null) matches all statements that have no associated context,
      model.filter(null, null, null, c1, c2, c3) matches all statements that have context c1, c2 or c3.

      Specified by:
      filter in interface Model
      Parameters:
      subj - The subject of the statements to match, null to match statements with any subject.
      pred - The predicate of the statements to match, null to match statements with any predicate.
      obj - The object of the statements to match, null to match statements with any object.
      contexts - The contexts of the statements to match. If no contexts are specified, statements will match disregarding their context. If one or more contexts are specified, statements with a context matching one of these will match.
      Returns:
      The statements that match the specified pattern.
      See Also:
    • removeTermIteration

      public void removeTermIteration(Iterator<Statement> iterator, Resource subj, IRI pred, Value obj, Resource... contexts)
      Description copied from class: AbstractModel
      Called by aggregate sets when a term has been removed from a term iterator. Exactly one of the last four terms will be non-empty.
      Specified by:
      removeTermIteration in class AbstractModel
      Parameters:
      iterator - The iterator used to navigate the live set (never null)
      subj - the subject term to be removed or null
      pred - the predicate term to be removed or null
      obj - the object term to be removed or null
      contexts - an array of one context term to be removed or an empty array
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<Statement>
      Specified by:
      isEmpty in interface Set<Statement>
      Overrides:
      isEmpty in class AbstractModel