commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [08/11] incubator-commonsrdf git commit: AbstractRDFParserBuilder generic
Date Fri, 09 Sep 2016 15:28:55 GMT
AbstractRDFParserBuilder generic

so that the build methods can return the correct subtype
of itself


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

Branch: refs/heads/jsonld-java
Commit: 0a5be9dbb9634637e19aa3aaeb56e6865b702577
Parents: ddc545e
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Mon Jul 4 12:07:01 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Mon Jul 4 12:07:01 2016 +0100

----------------------------------------------------------------------
 .../rdf/simple/AbstractRDFParserBuilder.java    | 111 +++++++++----------
 .../commons/rdf/simple/RDFParseException.java   |  50 +++++++++
 .../rdf/simple/DummyRDFParserBuilder.java       |   2 +-
 3 files changed, 102 insertions(+), 61 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/0a5be9db/simple/src/main/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilder.java
----------------------------------------------------------------------
diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilder.java
b/simple/src/main/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilder.java
index cc5fad0..4a5b4d1 100644
--- a/simple/src/main/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilder.java
+++ b/simple/src/main/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilder.java
@@ -33,6 +33,7 @@ import org.apache.commons.rdf.api.Graph;
 import org.apache.commons.rdf.api.IRI;
 import org.apache.commons.rdf.api.Quad;
 import org.apache.commons.rdf.api.RDFParserBuilder;
+import org.apache.commons.rdf.api.RDFParserBuilder.ParseResult;
 import org.apache.commons.rdf.api.RDFSyntax;
 import org.apache.commons.rdf.api.RDFTermFactory;
 
@@ -53,28 +54,10 @@ import org.apache.commons.rdf.api.RDFTermFactory;
  * multiple {@link #parse()} calls are thread-safe. The default {@link #parse()}
  * uses a thread pool in {@link #threadGroup} - but implementations can override
  * {@link #parse()} (e.g. because it has its own threading model or use
- * asynchronou  remote execution).
+ * asynchronous remote execution).
  */
-public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Cloneable {
-
-	public class RDFParseException extends Exception {		
-		private static final long serialVersionUID = 5427752643780702976L;
-		public RDFParseException() {
-			super();
-		}
-		public RDFParseException(String message, Throwable cause) {
-			super(message, cause);
-		}
-		public RDFParseException(String message) {
-			super(message);
-		}
-		public RDFParseException(Throwable cause) {
-			super(cause);
-		}
-		public RDFParserBuilder getRDFParserBuilder() {
-			return AbstractRDFParserBuilder.this;
-		}
-	}
+public abstract class AbstractRDFParserBuilder<T extends AbstractRDFParserBuilder<T>>

+	implements RDFParserBuilder, Cloneable {
 	
 	public static final ThreadGroup threadGroup = new ThreadGroup("Commons RDF parsers");
 	private static final ExecutorService threadpool = Executors.newCachedThreadPool(r ->
new Thread(threadGroup, r));
@@ -212,79 +195,85 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder,
Clon
 	private Optional<Dataset> targetDataset;
 	private Optional<Graph> targetGraph;
 
+	@SuppressWarnings("unchecked")
 	@Override
-	public AbstractRDFParserBuilder clone() {
+	public T clone() {
 		try {
-			return (AbstractRDFParserBuilder) super.clone();
+			return (T) super.clone();
 		} catch (CloneNotSupportedException e) {
 			throw new RuntimeException(e);
 		}
 	}
 
+	@SuppressWarnings("unchecked")
+	protected T asT() { 
+		return (T) this;
+	}
+	
 	@Override
-	public RDFParserBuilder rdfTermFactory(RDFTermFactory rdfTermFactory) {
-		AbstractRDFParserBuilder c = clone();
+	public T rdfTermFactory(RDFTermFactory rdfTermFactory) {
+		AbstractRDFParserBuilder<T> c = clone();
 		c.rdfTermFactory = Optional.ofNullable(rdfTermFactory);
-		return c;
+		return c.asT();
 	}
 
 	@Override
-	public RDFParserBuilder contentType(RDFSyntax rdfSyntax) throws IllegalArgumentException
{
-		AbstractRDFParserBuilder c = clone();
+	public T contentType(RDFSyntax rdfSyntax) throws IllegalArgumentException {
+		AbstractRDFParserBuilder<T> c = clone();
 		c.contentTypeSyntax = Optional.ofNullable(rdfSyntax);
 		c.contentType = c.contentTypeSyntax.map(syntax -> syntax.mediaType);
-		return c;
+		return c.asT();
 	}
 
 	@Override
-	public RDFParserBuilder contentType(String contentType) throws IllegalArgumentException
{
-		AbstractRDFParserBuilder c = clone();
+	public T contentType(String contentType) throws IllegalArgumentException {
+		AbstractRDFParserBuilder<T> c = clone();
 		c.contentType = Optional.ofNullable(contentType);
 		c.contentTypeSyntax = c.contentType.flatMap(RDFSyntax::byMediaType);
-		return c;
+		return c.asT();
 	}
 
 	@Override
-	public RDFParserBuilder base(IRI base) {
-		AbstractRDFParserBuilder c = clone();
+	public T base(IRI base) {
+		AbstractRDFParserBuilder<T> c = clone();
 		c.base = Optional.ofNullable(base);
 		c.base.ifPresent(i -> checkIsAbsolute(i));
-		return c;
+		return c.asT();
 	}
 
 	@Override
-	public RDFParserBuilder base(String base) throws IllegalArgumentException {
+	public T base(String base) throws IllegalArgumentException {
 		return base(internalRdfTermFactory.createIRI(base));
 	}
 
 	@Override
-	public RDFParserBuilder source(InputStream inputStream) {
-		AbstractRDFParserBuilder c = clone();
+	public T source(InputStream inputStream) {
+		AbstractRDFParserBuilder<T> c = clone();
 		c.resetSource();
 		c.sourceInputStream = Optional.ofNullable(inputStream);
-		return c;
+		return c.asT();
 	}
 
 	@Override
-	public RDFParserBuilder source(Path file) {
-		AbstractRDFParserBuilder c = clone();
+	public T source(Path file) {
+		AbstractRDFParserBuilder<T> c = clone();
 		c.resetSource();
 		c.sourceFile = Optional.ofNullable(file);
-		return c;
+		return c.asT();
 	}
 
 	@Override
-	public RDFParserBuilder source(IRI iri) {
-		AbstractRDFParserBuilder c = clone();
+	public T source(IRI iri) {
+		AbstractRDFParserBuilder<T> c = clone();
 		c.resetSource();
 		c.sourceIri = Optional.ofNullable(iri);
 		c.sourceIri.ifPresent(i -> checkIsAbsolute(i));
-		return c;
+		return c.asT();
 	}
 
 	@Override
-	public RDFParserBuilder source(String iri) throws IllegalArgumentException {
-		AbstractRDFParserBuilder c = clone();
+	public T source(String iri) throws IllegalArgumentException {
+		AbstractRDFParserBuilder<T> c = clone();
 		c.resetSource();
 		c.sourceIri = Optional.ofNullable(iri).map(internalRdfTermFactory::createIRI);
 		c.sourceIri.ifPresent(i -> checkIsAbsolute(i));
@@ -407,7 +396,7 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder,
Clon
 	 * @throws IOException If the source was not accessible (e.g. a file was not found)
 	 * @throws IllegalStateException If the parser was not in a compatible setting (e.g. contentType
was an invalid string) 
 	 */
-	protected AbstractRDFParserBuilder prepareForParsing() throws IOException, IllegalStateException
{
+	protected T prepareForParsing() throws IOException, IllegalStateException {
 		checkSource();
 		checkBaseRequired();		
 		checkContentType();
@@ -415,7 +404,7 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder,
Clon
 
 		// We'll make a clone of our current state which will be passed to
 		// parseSynchronously()
-		AbstractRDFParserBuilder c = clone();
+		AbstractRDFParserBuilder<T> c = clone();
 
 		// Use a fresh SimpleRDFTermFactory for each parse
 		if (!c.rdfTermFactory.isPresent()) {
@@ -428,7 +417,7 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder,
Clon
 			c.base = Optional.of(internalRdfTermFactory.createIRI(baseUri.toString()));
 		}
 
-		return c;
+		return c.asT();
 	}
 	
 	/**
@@ -519,7 +508,7 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder,
Clon
 
 	@Override
 	public Future<ParseResult> parse() throws IOException, IllegalStateException {
-		final AbstractRDFParserBuilder c = prepareForParsing();
+		final AbstractRDFParserBuilder<T> c = prepareForParsing();
 		return threadpool.submit(() -> {
 			c.parseSynchronusly();
 			return null;
@@ -527,27 +516,29 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder,
Clon
 	}
 
 	@Override
-	public RDFParserBuilder target(Consumer<Quad> consumer) {
-		AbstractRDFParserBuilder c = clone();
+	public T target(Consumer<Quad> consumer) {
+		AbstractRDFParserBuilder<T> c = clone();
 		c.resetTarget();
 		c.target = consumer;
-		return c;
+		return c.asT();
 	}
 	
 	@Override
-	public RDFParserBuilder target(Dataset dataset) {
-		AbstractRDFParserBuilder c = (AbstractRDFParserBuilder) RDFParserBuilder.super.target(dataset);
+	public T target(Dataset dataset) {
+		@SuppressWarnings({ "rawtypes", "unchecked" })
+		AbstractRDFParserBuilder<T> c = (AbstractRDFParserBuilder) RDFParserBuilder.super.target(dataset);
 		c.resetTarget();
 		c.targetDataset = Optional.of(dataset);
-		return c;
+		return c.asT();
 	}
 	
 	@Override
-	public RDFParserBuilder target(Graph graph) {
-		AbstractRDFParserBuilder c = (AbstractRDFParserBuilder) RDFParserBuilder.super.target(graph);
+	public T target(Graph graph) {
+		@SuppressWarnings({ "rawtypes", "unchecked" }) // super calls our .clone()
+		AbstractRDFParserBuilder<T> c = (AbstractRDFParserBuilder) RDFParserBuilder.super.target(graph);
 		c.resetTarget();
 		c.targetGraph = Optional.of(graph);
-		return c;
+		return c.asT();
 	}
 	
 

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/0a5be9db/simple/src/main/java/org/apache/commons/rdf/simple/RDFParseException.java
----------------------------------------------------------------------
diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/RDFParseException.java b/simple/src/main/java/org/apache/commons/rdf/simple/RDFParseException.java
new file mode 100644
index 0000000..ed16bb2
--- /dev/null
+++ b/simple/src/main/java/org/apache/commons/rdf/simple/RDFParseException.java
@@ -0,0 +1,50 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.rdf.simple;
+
+import org.apache.commons.rdf.api.RDFParserBuilder;
+
+public class RDFParseException extends Exception {
+	private static final long serialVersionUID = 5427752643780702976L;
+	private RDFParserBuilder builder;
+
+	public RDFParseException(RDFParserBuilder builder) {
+		super();
+		this.builder = builder;
+	}
+
+	public RDFParseException(RDFParserBuilder builder, String message, Throwable cause) {
+		super(message, cause);
+		this.builder = builder;
+	}
+
+	public RDFParseException(RDFParserBuilder builder, String message) {
+		super(message);
+		this.builder = builder;
+	}
+
+	public RDFParseException(RDFParserBuilder builder, Throwable cause) {
+		super(cause);
+		this.builder = builder;
+	}
+
+	public RDFParserBuilder getRDFParserBuilder() {
+		return builder;
+	}
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/0a5be9db/simple/src/test/java/org/apache/commons/rdf/simple/DummyRDFParserBuilder.java
----------------------------------------------------------------------
diff --git a/simple/src/test/java/org/apache/commons/rdf/simple/DummyRDFParserBuilder.java
b/simple/src/test/java/org/apache/commons/rdf/simple/DummyRDFParserBuilder.java
index 0cb7093..baf9768 100644
--- a/simple/src/test/java/org/apache/commons/rdf/simple/DummyRDFParserBuilder.java
+++ b/simple/src/test/java/org/apache/commons/rdf/simple/DummyRDFParserBuilder.java
@@ -43,7 +43,7 @@ import org.apache.commons.rdf.api.RDFTermFactory;
  * 
  *
  */
-public class DummyRDFParserBuilder extends AbstractRDFParserBuilder {
+public class DummyRDFParserBuilder extends AbstractRDFParserBuilder<DummyRDFParserBuilder>
{
 	
 	@Override
 	protected void parseSynchronusly() throws IOException, IllegalStateException, RDFParseException
{		


Mime
View raw message