Repository: incubator-commonsrdf Updated Branches: refs/heads/rdf4j ffa822e7b -> 69814ee5a
About closing streams Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/f0bdb07e Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/f0bdb07e Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/f0bdb07e Branch: refs/heads/rdf4j Commit: f0bdb07eb9e0932480f1151899f8f99fcb073a48 Parents: ffa822e Author: Stian Soiland-Reyes <st...@apache.org> Authored: Mon Oct 3 18:32:46 2016 +0100 Committer: Stian Soiland-Reyes <st...@apache.org> Committed: Mon Oct 3 18:32:46 2016 +0100 ---------------------------------------------------------------------- .../apache/commons/rdf/rdf4j/RDF4JDataset.java | 59 ++++++++++++++++++++ .../apache/commons/rdf/rdf4j/RDF4JGraph.java | 56 +++++++++++++++---- 2 files changed, 105 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/f0bdb07e/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JDataset.java ---------------------------------------------------------------------- diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JDataset.java b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JDataset.java index b0ca08d..98a90cf 100644 --- a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JDataset.java +++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JDataset.java @@ -17,8 +17,15 @@ */ package org.apache.commons.rdf.rdf4j; +import java.util.Optional; +import java.util.stream.Stream; + +import org.apache.commons.rdf.api.BlankNodeOrIRI; import org.apache.commons.rdf.api.Dataset; +import org.apache.commons.rdf.api.IRI; import org.apache.commons.rdf.api.Quad; +import org.apache.commons.rdf.api.RDFTerm; +import org.eclipse.rdf4j.repository.RepositoryConnection; /** @@ -27,4 +34,56 @@ import org.apache.commons.rdf.api.Quad; */ public interface RDF4JDataset extends Dataset, RDF4JGraphLike<Quad> { + /** + * {@inheritDoc} + * <p> + * Note that the stream must be closed with {@link Stream#close()} to ensure + * the underlying {@link RepositoryConnection} is closed. + * <p> + * This can generally achieved using a try-with-resources block, e.g.: + * <pre> + * int subjects; + * try (Stream<RDF4JQuad> s : graph.stream()) { + * subjects = s.map(RDF4JQuad::getSubject).distinct().count() + * } + * </pre> + */ + @Override + Stream<RDF4JQuad> stream(); + + /** + * {@inheritDoc} + * <p> + * Note that the stream must be closed with {@link Stream#close()} to ensure + * the underlying {@link RepositoryConnection} is closed. + * <p> + * This can generally achieved using a try-with-resources block, e.g.: + * <pre> + * int subjects; + * try (Stream<RDF4JQuad> s : graph.stream()) { + * subjects = s.map(RDF4JQuad::getSubject).distinct().count() + * } + * </pre> + */ + @Override + Stream<RDF4JQuad> stream(Optional<BlankNodeOrIRI> graphName, BlankNodeOrIRI subject, IRI predicate, + RDFTerm object); + + /** + * {@inheritDoc} + * <p> + * Note that the stream must be closed with {@link Stream#close()} to ensure + * the underlying {@link RepositoryConnection} is closed. + * <p> + * This can generally achieved using a try-with-resources block, e.g.: + * <pre> + * int graphs; + * try (Stream<BlankNodeOrIRI> s : graph.stream()) { + * graphs = s.count() + * } + * </pre> + */ + @Override + Stream<BlankNodeOrIRI> getGraphNames(); + } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/f0bdb07e/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JGraph.java ---------------------------------------------------------------------- diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JGraph.java b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JGraph.java index 907300a..e0e1e4a 100644 --- a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JGraph.java +++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JGraph.java @@ -17,16 +17,20 @@ */ package org.apache.commons.rdf.rdf4j; -import java.util.Optional; import java.util.Set; +import java.util.stream.Stream; +import org.apache.commons.rdf.api.BlankNodeOrIRI; import org.apache.commons.rdf.api.Graph; +import org.apache.commons.rdf.api.IRI; +import org.apache.commons.rdf.api.RDFTerm; import org.apache.commons.rdf.api.Triple; import org.apache.commons.rdf.rdf4j.impl.ModelGraphImpl; import org.apache.commons.rdf.rdf4j.impl.RepositoryGraphImpl; import org.eclipse.rdf4j.model.Model; import org.eclipse.rdf4j.model.Resource; import org.eclipse.rdf4j.repository.Repository; +import org.eclipse.rdf4j.repository.RepositoryConnection; /** @@ -48,24 +52,56 @@ public interface RDF4JGraph extends Graph, RDF4JGraphLike<Triple> { * contexts, while retrieval (e.g. {@link #contains(Triple)}) will succeed * if the triple is in at least one of the specified contexts. * <p> - * The context mask array may contain the {@link RDF4JBlankNodeOrIRI} - * <code>null</code>, indicating the default context (the <em>default - * graph</em> in RDF datasets). + * The context mask array may contain <code>null</code>, indicating the + * default context (the <em>default graph</em> in RDF datasets). * <p> * If the context mask is {@link Set#isEmpty()}, then this is a <em>union * graph</em> which triples reflect statements in any contexts. Triples - * added to the graph will be added in the default context, e.g. - * equivalent to <code>new Resource[1]{null}</code>) in RDF4J. + * added to the graph will be added in the default context, e.g. equivalent + * to <code>new Resource[1]{null}</code>) in RDF4J. * <p> * Note that the context mask itself cannot be <code>null</code>. * <p> - * The returned set is an immutable copy, to specify a different mask, use + * The returned set is an immutable copy; to specify a different mask, use * {@link RDF4JTermFactory#asRDFTermGraph(Repository, Set)}. * - * @return The context mask as an array of {@link Resource}s, or - * {@link Optional#empty()} indicating the union graph (any - * context). + * @return The context mask as an set of {@link BlankNodeOrIRI}s, which may + * contain the value <code>null</code>. */ public Set<RDF4JBlankNodeOrIRI<Resource>> getContextMask(); + /** + * {@inheritDoc} + * <p> + * Note that the stream must be closed with {@link Stream#close()} to ensure + * the underlying {@link RepositoryConnection} is closed. + * <p> + * This can generally achieved using a try-with-resources block, e.g.: + * <pre> + * int subjects; + * try (Stream<RDF4JTriple> s : graph.stream()) { + * subjects = s.map(RDF4JTriple::getSubject).distinct().count() + * } + * </pre> + */ + @Override + Stream<RDF4JTriple> stream(); + + /** + * {@inheritDoc} + * <p> + * Note that the stream must be closed with {@link Stream#close()} to ensure + * the underlying {@link RepositoryConnection} is closed. + * <p> + * This can generally achieved using a try-with-resources block, e.g.: + * <pre> + * int subjects; + * try (Stream<RDF4JTriple> s : graph.stream()) { + * subjects = s.map(RDF4JTriple::getSubject).distinct().count() + * } + * </pre> + */ + @Override + Stream<RDF4JTriple> stream(BlankNodeOrIRI subject, IRI predicate, RDFTerm object); + }