commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [40/50] incubator-commonsrdf git commit: Option... instead of boolean parameters
Date Fri, 07 Oct 2016 15:01:43 GMT
Option... instead of boolean parameters


Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/7f4fe0b0
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/7f4fe0b0
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/7f4fe0b0

Branch: refs/heads/master
Commit: 7f4fe0b04b3c56c4c205fbc55a9e1045ecce0d36
Parents: 756977a
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Fri Oct 7 09:12:37 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Fri Oct 7 09:12:37 2016 +0100

----------------------------------------------------------------------
 .../commons/rdf/rdf4j/RDF4JTermFactory.java     | 96 +++++++++-----------
 1 file changed, 45 insertions(+), 51 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/7f4fe0b0/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JTermFactory.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JTermFactory.java b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JTermFactory.java
index d35ed47..634648a 100644
--- a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JTermFactory.java
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JTermFactory.java
@@ -17,6 +17,8 @@
  */
 package org.apache.commons.rdf.rdf4j;
 
+import java.util.Arrays;
+import java.util.EnumSet;
 import java.util.Objects;
 import java.util.Set;
 import java.util.UUID;
@@ -102,6 +104,19 @@ public final class RDF4JTermFactory implements RDFTermFactory {
 	private static InternalRDF4JFactory rdf4j = new InternalRDF4JFactory() {
 	};
 
+	public enum Option { 
+		/** 
+		 * The Graph/Dataset should include any inferred statements 
+		 */
+		includeInferred,
+		/**
+		 * The graph/dataset should handle {@link Repository#initialize()} (if
+		 * needed) and {@link Repository#shutDown()} on {@link Graph#close()} /
+		 * {@link Dataset#close()}.
+		 */
+		handleInitAndShutdown
+	}
+	
 	/**
 	 * Adapt a RDF4J {@link Value} as a Commons RDF {@link RDFTerm}.
 	 * <p>
@@ -266,34 +281,18 @@ public final class RDF4JTermFactory implements RDFTermFactory {
 	 * {@link RepositoryConnection}s, including
 	 * {@link RDF4JDataset#iterate()}, 
 	 * {@link RDF4JDataset#stream()} and {@link RDF4JDataset#getGraphNames()}.
-	 * 
-	 * @param repository
-	 *            RDF4J {@link Repository} to connect to.
-	 * @return A {@link Dataset} backed by the RDF4J repository.
-	 */
-	public RDF4JDataset asRDFTermDataset(Repository repository) {
-		return rdf4j.createRepositoryDatasetImpl(repository, false, false);
-	}
-
-	/**
-	 * Adapt an RDF4J {@link Repository} as a Commons RDF {@link Dataset}.
-	 * <p>
-	 * Changes to the dataset are reflected in the repository, and vice versa.
-	 * <p>
-	 * <strong>Note:</strong> Some operations on the {@link RDF4JDataset}
-	 * requires the use of try-with-resources to close underlying
-	 * {@link RepositoryConnection}s, including
-	 * {@link RDF4JDataset#iterate()}, 
-	 * {@link RDF4JDataset#stream()} and {@link RDF4JDataset#getGraphNames()}.
 	 *
 	 * @param repository
 	 *            RDF4J {@link Repository} to connect to.
-	 * @param includeInferred
-	 *            If true, any inferred quads are included in the dataset
+	 * @param options
+	 *            Zero or more {@link Option}
 	 * @return A {@link Dataset} backed by the RDF4J repository.
 	 */
-	public RDF4JDataset asRDFTermDataset(Repository repository, boolean includeInferred) {
-		return rdf4j.createRepositoryDatasetImpl(repository, false, includeInferred);
+	public RDF4JDataset asRDFTermDataset(Repository repository, Option... options) {
+		EnumSet<Option> opts = optionSet(options);
+		return rdf4j.createRepositoryDatasetImpl(repository, 
+				opts.contains(Option.handleInitAndShutdown), 
+				opts.contains(Option.includeInferred));
 	}
 
 	/**
@@ -325,10 +324,16 @@ public final class RDF4JTermFactory implements RDFTermFactory {
 	 *
 	 * @param repository
 	 *            RDF4J {@link Repository} to connect to.
+	 * @param options
+	 *            Zero or more {@link Option}
 	 * @return A {@link Graph} backed by the RDF4J repository.
 	 */
-	public RDF4JGraph asRDFTermGraph(Repository repository) {
-		return rdf4j.createRepositoryGraphImpl(repository, false, false);
+	public RDF4JGraph asRDFTermGraph(Repository repository, Option... options) {
+		EnumSet<Option> opts = optionSet(options);
+		return rdf4j.createRepositoryGraphImpl(repository, 
+				opts.contains(Option.handleInitAndShutdown), 
+				opts.contains(Option.includeInferred), 
+				new Resource[0]);
 	}
 
 	/**
@@ -368,36 +373,19 @@ public final class RDF4JTermFactory implements RDFTermFactory {
 	 *            names to use as a context. The set may include the value
 	 *            <code>null</code> to indicate the default graph. The empty set
 	 *            indicates any context, e.g. the <em>union graph</em>.
-	 * 
+	 * @param option
+	 *            Zero or more {@link Option}s
 	 * @return A {@link Graph} backed by the RDF4J repository.
 	 */
-	public RDF4JGraph asRDFTermGraph(Repository repository, Set<? extends BlankNodeOrIRI>
contexts) {
+	public RDF4JGraph asRDFTermGraph(Repository repository, Set<? extends BlankNodeOrIRI>
contexts,
+			Option... option) {
+		EnumSet<Option> opts = optionSet(option);
 		/** NOTE: asValue() deliberately CAN handle <code>null</code> */
 		Resource[] resources = contexts.stream().map(g -> (Resource) asValue(g)).toArray(Resource[]::new);
-		return rdf4j.createRepositoryGraphImpl(Objects.requireNonNull(repository), false, true,
resources);
-	}
-
-	/**
-	 * Adapt an RDF4J {@link Repository} as a Commons RDF {@link Graph}.
-	 * <p>
-	 * The graph will include triples in any contexts (e.g. the union graph).
-	 * <p>
-	 * Changes to the graph are reflected in the repository, and vice versa.
-	 * <p>
-	 * <strong>Note:</strong> Some operations on the {@link RDF4JGraph}
-	 * requires the use of try-with-resources to close underlying
-	 * {@link RepositoryConnection}s, including
-	 * {@link RDF4JGraph#iterate()} and 
-	 * {@link RDF4JGraph#stream()}.
-	 * 
-	 * @param repository
-	 *            RDF4J {@link Repository} to connect to.
-	 * @param includeInferred
-	 *            If true, any inferred triples are included in the graph
-	 * @return A {@link Graph} backed by the RDF4J repository.
-	 */
-	public RDF4JGraph asRDFTermGraph(Repository repository, boolean includeInferred) {
-		return rdf4j.createRepositoryGraphImpl(repository, false, includeInferred);
+		return rdf4j.createRepositoryGraphImpl(Objects.requireNonNull(repository), 
+				opts.contains(Option.handleInitAndShutdown),
+				opts.contains(Option.includeInferred),
+				resources);
 	}
 
 	/**
@@ -598,4 +586,10 @@ public final class RDF4JTermFactory implements RDFTermFactory {
 		return valueFactory;
 	}
 
+	private EnumSet<Option> optionSet(Option... options) {
+		EnumSet<Option> opts = EnumSet.noneOf(Option.class);
+		opts.addAll(Arrays.asList(options));
+		return opts;
+	}
+	
 }


Mime
View raw message