commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [7/9] commons-rdf git commit: WithOption now builds map on the fly
Date Tue, 20 Feb 2018 10:35:38 GMT
WithOption now builds map on the fly


Project: http://git-wip-us.apache.org/repos/asf/commons-rdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-rdf/commit/40d016bb
Tree: http://git-wip-us.apache.org/repos/asf/commons-rdf/tree/40d016bb
Diff: http://git-wip-us.apache.org/repos/asf/commons-rdf/diff/40d016bb

Branch: refs/heads/fluent-parser
Commit: 40d016bbb19b7962b603f5f898dc26c72a34cfd9
Parents: 9c1e638
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Tue Feb 20 10:34:23 2018 +0000
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Tue Feb 20 10:34:23 2018 +0000

----------------------------------------------------------------------
 .../commons/rdf/api/io/NullParserConfig.java    | 53 ++++++++++++--------
 .../commons/rdf/api/io/ParserBuilder.java       |  3 +-
 .../commons/rdf/api/io/ParserConfigBuilder.java | 17 +++----
 3 files changed, 41 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/40d016bb/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/NullParserConfig.java
----------------------------------------------------------------------
diff --git a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/NullParserConfig.java
b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/NullParserConfig.java
index ccbb6b4..7ad78ba 100644
--- a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/NullParserConfig.java
+++ b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/NullParserConfig.java
@@ -61,7 +61,9 @@ class NullParserConfig implements ImmutableParserConfig, Serializable {
 
 	@Override
 	public Map<Option, Object> options() {
-		return Collections.emptyMap();
+		// Always a fresh map, so that our children can
+		// mutate it on the fly
+		return new HashMap<>();
 	}
 
 	@Override
@@ -93,28 +95,35 @@ class NullParserConfig implements ImmutableParserConfig, Serializable
{
 
 	@Override
 	public <V> ParserConfig withOption(Option<V> o, V v) {
-		return new WithOptional(this, o, v);
+		return new WithOption(this, o, v);
 	}
 
 	@SuppressWarnings("rawtypes")
-	final class WithOptional extends WithParent {
+	private static final class WithOption extends WithParent {
 
-		private final Map<Option, Object> options;
+		private Option o;
+		private Object v;
 
-		public <V> WithOptional(final ImmutableParserConfig parent, final Option<V>
o, V v) {
+		public <V> WithOption(final ImmutableParserConfig parent, final Option<V> o,
V v) {
 			super(parent);
-			this.options = new HashMap<>(parent.options());
-			options.put(o, v);
+			this.o = o;
+			this.v = v;
 		}
 
 		@Override
 		public Map<Option, Object> options() {
+			// Add to parent options
+			Map options = super.options();
+			if (v == null) {
+				options.remove(o);
+			} else {
+				options.put(o, v);
+			}
 			return options;
 		}
-
 	}
 
-	final class WithBase extends WithParent {
+	private static final class WithBase extends WithParent {
 		private final IRI base;
 
 		WithBase(final ImmutableParserConfig parent, final IRI base) {
@@ -124,11 +133,11 @@ class NullParserConfig implements ImmutableParserConfig, Serializable
{
 
 		@Override
 		public Optional<IRI> base() {
-			return Optional.of(base);
+			return Optional.ofNullable(base);
 		}
 	}
 
-	final class WithRDF extends WithParent {
+	private static final class WithRDF extends WithParent {
 		private final RDF rdf;
 
 		WithRDF(final ImmutableParserConfig parent, final RDF rdf) {
@@ -138,12 +147,12 @@ class NullParserConfig implements ImmutableParserConfig, Serializable
{
 
 		@Override
 		public Optional<RDF> rdf() {
-			return Optional.of(rdf);
+			return Optional.ofNullable(rdf);
 		}
 	}
 
 	@SuppressWarnings("rawtypes")
-	final class WithTarget extends WithParent {
+	private static final class WithTarget extends WithParent {
 		private final ParserTarget target;
 
 		WithTarget(final ImmutableParserConfig parent, final ParserTarget target) {
@@ -153,12 +162,12 @@ class NullParserConfig implements ImmutableParserConfig, Serializable
{
 
 		@Override
 		public Optional<ParserTarget> target() {
-			return Optional.of(target);
+			return Optional.ofNullable(target);
 		}
 	}
 
 	@SuppressWarnings("rawtypes")
-	public class WithSource extends WithParent {
+	private static final class WithSource extends WithParent {
 		private final ParserSource source;
 
 		WithSource(ImmutableParserConfig parent, ParserSource source) {
@@ -168,11 +177,11 @@ class NullParserConfig implements ImmutableParserConfig, Serializable
{
 
 		@Override
 		public Optional<ParserSource> source() {
-			return Optional.of(source);
+			return Optional.ofNullable(source);
 		}
 	}
 
-	static class WithSyntax extends WithParent {
+	private static final class WithSyntax extends WithParent {
 		private final RDFSyntax syntax;
 
 		WithSyntax(ImmutableParserConfig parent, RDFSyntax syntax) {
@@ -182,7 +191,7 @@ class NullParserConfig implements ImmutableParserConfig, Serializable
{
 
 		@Override
 		public Optional<RDFSyntax> syntax() {
-			return Optional.of(syntax);
+			return Optional.ofNullable(syntax);
 		}
 	}
 
@@ -249,7 +258,7 @@ class NullParserConfig implements ImmutableParserConfig, Serializable
{
 		private final IRI base;
 		private final ParserSource source;
 		private final ParserTarget target;
-		private final Map<Option, Object> options = new HashMap<>();
+		private final Map<Option, Object> options;
 		private final ExecutorService executor;
 
 		SnapshotParserConfig(ParserConfig old) {
@@ -270,7 +279,8 @@ class NullParserConfig implements ImmutableParserConfig, Serializable
{
 			this.base = base;
 			this.source = source;
 			this.target = target;
-			this.options.putAll(options);
+			// We'll make a copy
+			this.options = Collections.unmodifiableMap(new HashMap<Option, Object>(options));
 			this.executor = executor;				
 		}
 
@@ -301,7 +311,8 @@ class NullParserConfig implements ImmutableParserConfig, Serializable
{
 
 		@Override
 		public Map<Option, Object> options() {
-			return options;
+			// return a mutable copy so our children can build on it
+			return new HashMap<Option,Object>(options);
 		}
 	}	
 }

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/40d016bb/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserBuilder.java
----------------------------------------------------------------------
diff --git a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserBuilder.java
b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserBuilder.java
index cd3eae4..ed43444 100644
--- a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserBuilder.java
+++ b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserBuilder.java
@@ -1,7 +1,6 @@
 package org.apache.commons.rdf.api.io;
 
-import org.apache.commons.rdf.api.Dataset;
 import org.apache.commons.rdf.api.fluentparser.OptionalTargetOrSyntax;
 
-public interface ParserBuilder extends OptionalTargetOrSyntax<Dataset> {
+public interface ParserBuilder<T> extends OptionalTargetOrSyntax<T> {
 }

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/40d016bb/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserConfigBuilder.java
----------------------------------------------------------------------
diff --git a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserConfigBuilder.java
b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserConfigBuilder.java
index 457fb6c..406c6fe 100644
--- a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserConfigBuilder.java
+++ b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserConfigBuilder.java
@@ -30,13 +30,12 @@ import org.apache.commons.rdf.api.fluentparser.Async;
 import org.apache.commons.rdf.api.fluentparser.NeedSourceBased;
 import org.apache.commons.rdf.api.fluentparser.NeedSourceOrBase;
 import org.apache.commons.rdf.api.fluentparser.NeedTargetOrRDF;
-import org.apache.commons.rdf.api.fluentparser.NeedTargetOrRDFOrSyntax;
 import org.apache.commons.rdf.api.fluentparser.OptionalTarget;
 import org.apache.commons.rdf.api.fluentparser.Sync;
 import org.apache.commons.rdf.api.io.ParserConfig.ImmutableParserConfig;
 
 @SuppressWarnings({ "unchecked", "rawtypes" })
-public final class ParserConfigBuilder implements NeedTargetOrRDF, NeedTargetOrRDFOrSyntax,
+public final class ParserConfigBuilder implements ParserBuilder, NeedTargetOrRDF, 
 		NeedSourceOrBase, NeedSourceBased, OptionalTarget, Sync, Async {
 
 	public ParserConfigBuilder(ParserConfig mutated) {
@@ -52,11 +51,6 @@ public final class ParserConfigBuilder implements NeedTargetOrRDF, NeedTargetOrR
 		}
 	}
 	
-	@Override
-	public NeedTargetOrRDF syntax(RDFSyntax syntax) {
-		return mutate(config.withSyntax(syntax));
-	}
-
 	public ParserConfig buildConfig() {
 		return config.asImmutableConfig();
 	}
@@ -78,12 +72,12 @@ public final class ParserConfigBuilder implements NeedTargetOrRDF, NeedTargetOrR
 	}
 
 	@Override
-	public NeedSourceBased base(IRI iri) {
+	public ParserConfigBuilder base(IRI iri) {
 		return mutate(config.withBase(iri));
 	}
 
 	@Override
-	public NeedSourceBased base(String iriStr) {
+	public ParserConfigBuilder base(String iriStr) {
 		return base(new IRIImpl(iriStr));
 	}
 
@@ -157,4 +151,9 @@ public final class ParserConfigBuilder implements NeedTargetOrRDF, NeedTargetOrR
 		return mutate(config.asImmutableConfig());
 	}
 
+	@Override
+	public NeedTargetOrRDF syntax(RDFSyntax syntax) {
+		return mutate(config.withSyntax(syntax));
+	}
+
 }


Mime
View raw message