Class DynamicModel
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractSet<Statement>
-
- org.eclipse.rdf4j.model.impl.DynamicModel
-
- All Implemented Interfaces:
Serializable,Iterable<Statement>,Collection<Statement>,Set<Statement>,Model,NamespaceAware
public class DynamicModel extends AbstractSet<Statement> implements Model
A LinkedHashModel or a TreeModel achieves fast data access at the cost of higher indexing time. The DynamicModel postpones this cost until such access is actually needed. It stores all data in a LinkedHashMap and supports adding, retrieving and removing data. The model will upgrade to a full model (provided by the modelFactory) if more complex operations are called, for instance removing data according to a pattern (eg. all statements with rdf:type as predicate). DynamicModel is thread safe to the extent that the underlying LinkedHashMap or Model is. The upgrade path is protected by the actual upgrade method being synchronized. The LinkedHashMap storage is not removed once upgraded, so concurrent reads that have started reading from the LinkedHashMap can continue to read even during an upgrade. We do make the LinkedHashMap unmodifiable to reduce the chance of there being a bug.- Author:
- HÃ¥vard Mikkelsen Ottestad
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description DynamicModel(ModelFactory modelFactory)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(Resource subj, IRI pred, Value obj, Resource... contexts)Adds one or more statements to the model.booleanadd(Statement statement)booleanaddAll(Collection<? extends Statement> c)voidclear()booleanclear(Resource... context)Removes statements with the specified context exist in this model.booleancontains(Object o)booleancontains(Resource subj, IRI pred, Value obj, Resource... contexts)Determines if statements with the specified subject, predicate, object and (optionally) context exist in this model.booleancontainsAll(Collection<?> c)Set<Resource>contexts()Returns aSetview of the contexts contained in this model.booleanequals(Object o)Modelfilter(Resource subj, IRI pred, Value obj, Resource... contexts)Returns a filtered view of the statements with the specified subject, predicate, object and (optionally) context.Optional<Namespace>getNamespace(String prefix)Gets the namespace that is associated with the specified prefix, if any.Set<Namespace>getNamespaces()Gets the set that contains the assigned namespaces.Iterable<Statement>getStatements(Resource subject, IRI predicate, Value object, Resource... contexts)inthashCode()booleanisEmpty()Iterator<Statement>iterator()Set<Value>objects()Returns aSetview of the objects contained in this model.Set<IRI>predicates()Returns aSetview of the predicates contained in this model.booleanremove(Object o)booleanremove(Resource subj, IRI pred, Value obj, Resource... contexts)Removes statements with the specified subject, predicate, object and (optionally) context exist in this model.booleanremoveAll(Collection<?> c)Optional<Namespace>removeNamespace(String prefix)Removes a namespace declaration by removing the association between a prefix and a namespace name.booleanretainAll(Collection<?> c)NamespacesetNamespace(String prefix, String name)Sets the prefix for a namespace.voidsetNamespace(Namespace namespace)Sets the prefix for a namespace.intsize()Set<Resource>subjects()Returns aSetview of the subjects contained in this model.Object[]toArray()<T> T[]toArray(T[] a)Modelunmodifiable()Returns an unmodifiable view of this model.-
Methods inherited from class java.util.AbstractCollection
toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface java.util.Set
spliterator
-
-
-
-
Constructor Detail
-
DynamicModel
public DynamicModel(ModelFactory modelFactory)
-
-
Method Detail
-
unmodifiable
public Model unmodifiable()
Description copied from interface:ModelReturns an unmodifiable view of this model. This method provides "read-only" access to this model. Query operations on the returned model "read through" to this model, and attempts to modify the returned model, whether direct or via its iterator, result in anUnsupportedOperationException.- Specified by:
unmodifiablein interfaceModel- Returns:
- an unmodifiable view of the specified set.
-
getNamespace
public Optional<Namespace> getNamespace(String prefix)
Description copied from interface:NamespaceAwareGets 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:
getNamespacein interfaceNamespaceAware- 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:NamespaceAwareGets the set that contains the assigned namespaces.- Specified by:
getNamespacesin interfaceNamespaceAware- Returns:
- A
Setcontaining theNamespaceobjects that are available.
-
setNamespace
public Namespace setNamespace(String prefix, String name)
Description copied from interface:ModelSets the prefix for a namespace. This will replace any existing namespace associated to the prefix.- Specified by:
setNamespacein interfaceModel- Parameters:
prefix- The new prefix.name- The namespace name that the prefix maps to.- Returns:
- The
Namespaceobject for the given namespace.
-
setNamespace
public void setNamespace(Namespace namespace)
Description copied from interface:ModelSets the prefix for a namespace. This will replace any existing namespace associated to the prefix.- Specified by:
setNamespacein interfaceModel- Parameters:
namespace- ANamespaceobject to use in this Model.
-
removeNamespace
public Optional<Namespace> removeNamespace(String prefix)
Description copied from interface:ModelRemoves a namespace declaration by removing the association between a prefix and a namespace name.- Specified by:
removeNamespacein interfaceModel- 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()
-
contains
public boolean contains(Resource subj, IRI pred, Value obj, Resource... contexts)
Description copied from interface:ModelDetermines if statements with the specified subject, predicate, object and (optionally) context exist in this model. Thesubject,predicateandobjectparameters can benullto indicate wildcards. Thecontextsparameter 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 valuenulland explicitly cast it to typeResource.Examples:
model.contains(s1, null, null)is true if any statements in this model have subjects1,
model.contains(null, null, null, c1)is true if any statements in this model have contextc1,
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 contextc1,c2orc3.- Specified by:
containsin interfaceModel- Parameters:
subj- The subject of the statements to match,nullto match statements with any subject.pred- The predicate of the statements to match,nullto match statements with any predicate.obj- The object of the statements to match,nullto 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:
trueif statements match the specified pattern.
-
add
public boolean add(Resource subj, IRI pred, Value obj, Resource... contexts)
Description copied from interface:ModelAdds 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.
-
clear
public boolean clear(Resource... context)
Description copied from interface:ModelRemoves statements with the specified context exist in this model.
-
remove
public boolean remove(Resource subj, IRI pred, Value obj, Resource... contexts)
Description copied from interface:ModelRemoves statements with the specified subject, predicate, object and (optionally) context exist in this model. Thesubject,predicateandobjectparameters can benullto indicate wildcards. Thecontextsparameter 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 valuenulland explicitly cast it to typeResource.Examples:
model.remove(s1, null, null)removes any statements in this model have subjects1,
model.remove(null, null, null, c1)removes any statements in this model have contextc1,
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 contextc1,c2orc3.- Specified by:
removein interfaceModel- Parameters:
subj- The subject of the statements to remove,nullto remove statements with any subject.pred- The predicate of the statements to remove,nullto remove statements with any predicate.obj- The object of the statements to remove,nullto 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:
trueif one or more statements have been removed.
-
filter
public Model filter(Resource subj, IRI pred, Value obj, Resource... contexts)
Description copied from interface:ModelReturns a filtered view of the statements with the specified subject, predicate, object and (optionally) context. Thesubject,predicateandobjectparameters can benullto indicate wildcards. Thecontextsparameter 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 valuenulland explicitly cast it to typeResource.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
removeoperation), the results of the iteration are undefined. The model supports element removal, which removes the corresponding statement from this Model, via theIterator.remove,Set.remove,removeAll,retainAll, andclearoperations. The statements passed to theaddandaddAlloperations must match the parameter pattern.Examples:
model.filter(s1, null, null)matches all statements that have subjects1,
model.filter(null, null, null, c1)matches all statements that have contextc1,
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 contextc1,c2orc3.- Specified by:
filterin interfaceModel- Parameters:
subj- The subject of the statements to match,nullto match statements with any subject.pred- The predicate of the statements to match,nullto match statements with any predicate.obj- The object of the statements to match,nullto 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:
Model.getStatements(Resource, IRI, Value, Resource...)
-
subjects
public Set<Resource> subjects()
Description copied from interface:ModelReturns aSetview of the subjects contained in this model. The set is backed by the model, so changes to the model are reflected in the set, and vice-versa. If the model is modified while an iteration over the set is in progress (except through the iterator's ownremoveoperation), the results of the iteration are undefined. The set supports element removal, which removes all statements from the model for which that element is a subject value, via theIterator.remove,Set.remove,removeAll,retainAll, andclearoperations. It does not support theaddoraddAlloperations if the parameterspredorobjare null.
-
predicates
public Set<IRI> predicates()
Description copied from interface:ModelReturns aSetview of the predicates contained in this model. The set is backed by the model, so changes to the model are reflected in the set, and vice-versa. If the model is modified while an iteration over the set is in progress (except through the iterator's ownremoveoperation), the results of the iteration are undefined. The set supports element removal, which removes all statements from the model for which that element is a predicate value, via theIterator.remove,Set.remove,removeAll,retainAll, andclearoperations. It does not support theaddoraddAlloperations if the parameterssubjorobjare null.- Specified by:
predicatesin interfaceModel- Returns:
- a set view of the predicates contained in this model
-
objects
public Set<Value> objects()
Description copied from interface:ModelReturns aSetview of the objects contained in this model. The set is backed by the model, so changes to the model are reflected in the set, and vice-versa. If the model is modified while an iteration over the set is in progress (except through the iterator's ownremoveoperation), the results of the iteration are undefined. The set supports element removal, which removes all statements from the model for which that element is an object value, via theIterator.remove,Set.remove,removeAll,retainAll, andclearoperations. It does not support theaddoraddAlloperations if the parameterssubjorpredare null.
-
contexts
public Set<Resource> contexts()
Description copied from interface:ModelReturns aSetview of the contexts contained in this model. The set is backed by the model, so changes to the model are reflected in the set, and vice-versa. If the model is modified while an iteration over the set is in progress (except through the iterator's ownremoveoperation), the results of the iteration are undefined. The set supports element removal, which removes all statements from the model for which that element is a context value, via theIterator.remove,Set.remove,removeAll,retainAll, andclearoperations. It does not support theaddoraddAlloperations if the parameterssubj,predorobjare null.
-
size
public int size()
- Specified by:
sizein interfaceCollection<Statement>- Specified by:
sizein interfaceSet<Statement>- Specified by:
sizein classAbstractCollection<Statement>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfaceCollection<Statement>- Specified by:
isEmptyin interfaceSet<Statement>- Overrides:
isEmptyin classAbstractCollection<Statement>
-
contains
public boolean contains(Object o)
- Specified by:
containsin interfaceCollection<Statement>- Specified by:
containsin interfaceSet<Statement>- Overrides:
containsin classAbstractCollection<Statement>
-
toArray
public Object[] toArray()
- Specified by:
toArrayin interfaceCollection<Statement>- Specified by:
toArrayin interfaceSet<Statement>- Overrides:
toArrayin classAbstractCollection<Statement>
-
toArray
public <T> T[] toArray(T[] a)
- Specified by:
toArrayin interfaceCollection<Statement>- Specified by:
toArrayin interfaceSet<Statement>- Overrides:
toArrayin classAbstractCollection<Statement>
-
add
public boolean add(Statement statement)
- Specified by:
addin interfaceCollection<Statement>- Specified by:
addin interfaceSet<Statement>- Overrides:
addin classAbstractCollection<Statement>
-
remove
public boolean remove(Object o)
- Specified by:
removein interfaceCollection<Statement>- Specified by:
removein interfaceSet<Statement>- Overrides:
removein classAbstractCollection<Statement>
-
containsAll
public boolean containsAll(Collection<?> c)
- Specified by:
containsAllin interfaceCollection<Statement>- Specified by:
containsAllin interfaceSet<Statement>- Overrides:
containsAllin classAbstractCollection<Statement>
-
addAll
public boolean addAll(Collection<? extends Statement> c)
- Specified by:
addAllin interfaceCollection<Statement>- Specified by:
addAllin interfaceSet<Statement>- Overrides:
addAllin classAbstractCollection<Statement>
-
retainAll
public boolean retainAll(Collection<?> c)
- Specified by:
retainAllin interfaceCollection<Statement>- Specified by:
retainAllin interfaceSet<Statement>- Overrides:
retainAllin classAbstractCollection<Statement>
-
removeAll
public boolean removeAll(Collection<?> c)
- Specified by:
removeAllin interfaceCollection<Statement>- Specified by:
removeAllin interfaceSet<Statement>- Overrides:
removeAllin classAbstractSet<Statement>
-
clear
public void clear()
- Specified by:
clearin interfaceCollection<Statement>- Specified by:
clearin interfaceSet<Statement>- Overrides:
clearin classAbstractCollection<Statement>
-
getStatements
public Iterable<Statement> getStatements(Resource subject, IRI predicate, Value object, Resource... contexts)
Description copied from interface:ModelReturns anIterableover allStatements in this Model that match the supplied criteria.Examples:
model.getStatements(s1, null, null)matches all statements that have subjects1model.getStatements(s1, p1, null)matches all statements that have subjects1and predicatep1model.getStatements(null, null, null, c1)matches all statements that have contextc1model.getStatements(null, null, null, (Resource)null)matches all statements that have no associated contextmodel.getStatements(null, null, null, c1, c2, c3)matches all statements that have contextc1,c2orc3
- Specified by:
getStatementsin interfaceModel- Parameters:
subject- The subject of the statements to match,nullto match statements with any subject.predicate- The predicate of the statements to match,nullto match statements with any predicate.object- The object of the statements to match,nullto 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 any one of these will match. To match statements without an associated context, specify the valuenulland explicitly cast it to typeResource.- Returns:
- an
Iterableover the statements in this Model that match the specified pattern. - See Also:
Model.filter(Resource, IRI, Value, Resource...)
-
equals
public boolean equals(Object o)
- Specified by:
equalsin interfaceCollection<Statement>- Specified by:
equalsin interfaceSet<Statement>- Overrides:
equalsin classAbstractSet<Statement>
-
hashCode
public int hashCode()
- Specified by:
hashCodein interfaceCollection<Statement>- Specified by:
hashCodein interfaceSet<Statement>- Overrides:
hashCodein classAbstractSet<Statement>
-
-