commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [3/6] incubator-commonsrdf git commit: RDF4JTermFactory
Date Tue, 03 May 2016 23:36:26 GMT
RDF4JTermFactory


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

Branch: refs/heads/rdf4j
Commit: 730a9cd0cc129bfb069ce8b5cbc72245c519e642
Parents: 71b5893
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Wed May 4 00:04:14 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Wed May 4 00:04:14 2016 +0100

----------------------------------------------------------------------
 .../commons/rdf/rdf4j/RDF4JTermFactory.java     | 479 +++++++++++++++++++
 .../commons/rdf/rdf4j/Rdf4JRDFTermFactory.java  | 479 -------------------
 .../apache/commons/rdf/rdf4j/BlankNodeTest.java |   2 +-
 .../org/apache/commons/rdf/rdf4j/GraphTest.java |   2 +-
 .../commons/rdf/rdf4j/MemoryGraphTest.java      |   2 +-
 .../rdf/rdf4j/Rdf4JRDFTermFactoryTest.java      |   2 +-
 6 files changed, 483 insertions(+), 483 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/730a9cd0/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
new file mode 100644
index 0000000..158d683
--- /dev/null
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JTermFactory.java
@@ -0,0 +1,479 @@
+/**
+ * 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.rdf4j;
+
+import java.nio.charset.StandardCharsets;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.UUID;
+import java.util.stream.Stream;
+
+
+// To avoid confusion, avoid importing 
+// classes that are in both
+// commons.rdf and openrdf.model (e.g. IRI)
+import org.apache.commons.rdf.api.BlankNode;
+import org.apache.commons.rdf.api.BlankNodeOrIRI;
+import org.apache.commons.rdf.api.RDFTerm;
+import org.apache.commons.rdf.api.RDFTermFactory;
+import org.apache.commons.rdf.api.Triple;
+import org.openrdf.model.BNode;
+import org.openrdf.model.Model;
+import org.openrdf.model.Resource;
+import org.openrdf.model.Statement;
+import org.openrdf.model.Value;
+import org.openrdf.model.ValueFactory;
+import org.openrdf.model.impl.LinkedHashModel;
+import org.openrdf.model.impl.SimpleValueFactory;
+import org.openrdf.model.vocabulary.XMLSchema;
+import org.openrdf.rio.turtle.TurtleUtil;
+
+public class RDF4JTermFactory implements RDFTermFactory {
+	
+	private abstract class RDFTermImpl<T extends Value> implements RDF4JTerm<T>
{
+		T value;
+
+		public T asValue() { 
+			return value;
+		}
+		
+		RDFTermImpl(T value) {
+			this.value = value;			
+		}
+	}
+	
+	private final class GraphImpl implements RDF4JGraph {
+		
+		private Model model;
+
+		public Model asModel() { 
+			return model;
+		}
+		
+		GraphImpl(Model model) {
+			this.model = model;		
+		}
+		
+		@Override
+		public long size() {
+			int size = model.size();
+			if (size < Integer.MAX_VALUE) {
+				return size;
+			} else {
+				// Collection.size() can't help us, we'll have to count
+				return model.parallelStream().count();
+			}				
+		}
+
+		@Override
+		public void remove(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI predicate, RDFTerm
object) {
+			model.remove(
+					(Resource)asValue(subject), 
+					(org.openrdf.model.IRI)asValue(predicate), 
+					asValue(object));
+			
+		}
+
+		@Override
+		public void remove(Triple triple) { 
+			remove(triple.getSubject(), triple.getPredicate(), triple.getObject());
+			
+			// This however fails:
+			// model.remove(asStatement(triple));
+		}
+
+		@Override
+		public Stream<? extends Triple> getTriples(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI
predicate, RDFTerm object) {
+			return model.filter(
+					(Resource)asValue(subject), 
+					(org.openrdf.model.IRI)asValue(predicate), 
+					asValue(object)).parallelStream()
+				.map(RDF4JTermFactory.this::asTriple);
+		}
+
+		@Override
+		public Stream<Triple> getTriples() {
+			return model.parallelStream().map(RDF4JTermFactory.this::asTriple);
+		}
+
+		@Override
+		public boolean contains(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI predicate,
RDFTerm object) {
+			return model.contains(
+					(Resource)asValue(subject), 
+					(org.openrdf.model.IRI)asValue(predicate), 
+					asValue(object));
+		}
+
+		@Override
+		public boolean contains(Triple triple) {
+			return model.contains(asStatement(triple));
+		}
+
+		@Override
+		public void clear() {
+			model.clear();
+		}
+
+		@Override
+		public void add(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI predicate, RDFTerm
object) {
+			model.add(
+					(Resource)asValue(subject), 
+					(org.openrdf.model.IRI)asValue(predicate), 
+					asValue(object));				
+		}
+
+		@Override
+		public void add(Triple triple) {
+			model.add(asStatement(triple));
+		}
+	}
+
+	private final class TripleImpl implements Triple, RDF4JTriple {
+		private final Statement statement;
+
+		TripleImpl(Statement statement) {
+			this.statement = statement;
+		}
+
+		public Statement asStatement() { 
+			return statement;
+		}
+		
+		@Override
+		public BlankNodeOrIRI getSubject() {
+			return (BlankNodeOrIRI) asRDFTerm(statement.getSubject());
+		}
+
+		@Override
+		public org.apache.commons.rdf.api.IRI getPredicate() {
+			return (org.apache.commons.rdf.api.IRI) asRDFTerm(statement.getPredicate());
+		}
+
+		@Override
+		public RDFTerm getObject() {
+			return asRDFTerm(statement.getObject());
+		}
+		
+		@Override
+		public boolean equals(Object obj) {
+			if (obj instanceof Triple) {
+				Triple triple = (Triple) obj;
+				return getSubject().equals(triple.getSubject()) &&
+						getPredicate().equals(triple.getPredicate()) && 
+						getObject().equals(triple.getObject());
+			}
+			return false;
+		}
+
+		@Override
+		public int hashCode() {
+			return Objects.hash(getSubject(), getPredicate(), getObject());
+		}
+		
+		@Override
+		public String toString() {
+			return statement.toString();
+		}
+	}
+
+	private final class IRIImpl extends RDFTermImpl<org.openrdf.model.IRI> 
+		implements RDF4JIRI {
+
+		IRIImpl(org.openrdf.model.IRI iri) {
+			super(iri);			
+		}
+		@Override
+		public String ntriplesString() {
+			return "<" + value.toString() +  ">";
+		}
+
+		@Override
+		public String getIRIString() {
+			return value.toString();
+		}
+
+		@Override
+		public String toString() {
+			return value.toString();
+		}
+
+		public int hashCode() {
+			// Same definition
+			return value.hashCode();
+		}
+		@Override
+		public boolean equals(Object obj) {
+			if (obj == this) { return true; }
+			if (obj instanceof IRIImpl) {
+				IRIImpl impl = (IRIImpl) obj; 
+				return asValue().equals(impl.asValue());
+			}
+			if (obj instanceof org.apache.commons.rdf.api.IRI) {
+				org.apache.commons.rdf.api.IRI iri = (org.apache.commons.rdf.api.IRI) obj;
+				return value.toString().equals(iri.getIRIString());
+			}
+			return false;
+		}
+		
+	}
+
+	private final class LiteralImpl 
+		extends RDFTermImpl<org.openrdf.model.Literal>
+	    implements RDF4JLiteral {		
+
+		private static final String QUOTE = "\"";
+		
+		LiteralImpl(org.openrdf.model.Literal literal) {
+			super(literal);			
+		}
+		@Override
+		public String ntriplesString() {
+			// TODO: Use a more efficient StringBuffer
+			String escaped = QUOTE + TurtleUtil.encodeString(value.getLabel()) + QUOTE;
+			if (value.getLanguage().isPresent()) {
+				return escaped + "@" + value.getLanguage().get();
+			}
+			if (value.getDatatype().equals(XMLSchema.STRING)) { 
+				return escaped;
+			}
+			return escaped + "^^<" + TurtleUtil.encodeURIString(value.getDatatype().toString())
+ ">";
+		}
+
+		@Override
+		public String getLexicalForm() {
+			return value.getLabel();
+		}
+
+		@Override
+		public org.apache.commons.rdf.api.IRI getDatatype() {
+			return (org.apache.commons.rdf.api.IRI) asRDFTerm(value.getDatatype());
+		}
+
+		@Override
+		public Optional<String> getLanguageTag() {
+			return value.getLanguage();
+		}
+
+		@Override
+		public String toString() {
+			return ntriplesString();
+		}
+
+		@Override
+		public boolean equals(Object obj) {
+			if (obj == this) { return true; }
+			if (obj instanceof org.apache.commons.rdf.api.Literal) {
+				org.apache.commons.rdf.api.Literal other = (org.apache.commons.rdf.api.Literal) obj;
+				return getLexicalForm().equals(other.getLexicalForm()) &&
+						getDatatype().equals(other.getDatatype()) && 
+						getLanguageTag().equals(other.getLanguageTag());
+				
+			}
+			return false;
+		}
+
+		public int hashCode() {
+			return Objects.hash(value.getLabel(), value.getDatatype(), value.getLanguage());
+		}
+	}
+
+	private final class BlankNodeImpl extends RDFTermImpl<BNode>
+		implements RDF4JBlankNode {
+		
+		BlankNodeImpl(BNode bNode) {
+			super(bNode);			
+		}
+		
+		@Override
+		public String ntriplesString() {
+			if (isValidBlankNodeLabel(value.getID())) { 
+				return "_:" + value.getID();
+			} else {
+				return "_:" + UUID.nameUUIDFromBytes(value.getID().getBytes(StandardCharsets.UTF_8));
+			}
+		}
+
+		private boolean isValidBlankNodeLabel(String id) {
+			// FIXME: Replace with a regular expression?			
+			if (id.isEmpty()) { 
+				return false;
+			}
+			if (! TurtleUtil.isBLANK_NODE_LABEL_StartChar(id.codePointAt(0)))  {
+				return false;
+			}
+			for (int i=1; i<id.length(); i++) { 
+				if (! TurtleUtil.isBLANK_NODE_LABEL_Char(id.codePointAt(i))) { 
+					return false;
+				}
+			}
+			return true;
+		}
+
+		@Override
+		public String uniqueReference() {
+			return salt + value.getID();
+		}
+
+		@Override
+		public int hashCode() {
+			return uniqueReference().hashCode();
+		}
+
+		public boolean equals(Object obj) { 
+			if (obj == this) { 
+				return true;
+			}
+			// NOTE: Do NOT use Bnode.equals() as it has a more generous
+			// equality based only on the value.getID();			
+			if (obj instanceof BlankNode) {
+				BlankNode blankNode = (BlankNode) obj;
+				return uniqueReference().equals(blankNode.uniqueReference());								
+			}
+			return false;
+		}
+	}
+
+	
+
+	private ValueFactory valueFactory;
+	
+	private String salt = "urn:uuid:" + UUID.randomUUID() + "#";
+	
+	public RDF4JTermFactory() {
+		this.valueFactory = SimpleValueFactory.getInstance();
+	}
+	
+	public RDF4JTermFactory(ValueFactory valueFactory) { 
+		this.valueFactory = valueFactory;
+	}	
+	
+	@Override
+	public BlankNodeImpl createBlankNode() throws UnsupportedOperationException {
+		BNode bnode = valueFactory.createBNode();
+		return (BlankNodeImpl)asRDFTerm(bnode);
+	}
+	
+	@Override
+	public BlankNodeImpl createBlankNode(String name) throws UnsupportedOperationException {
+		BNode bnode = valueFactory.createBNode(name);
+		return (BlankNodeImpl)asRDFTerm(bnode);
+	}
+	
+	@Override
+	public LiteralImpl createLiteral(String lexicalForm) throws IllegalArgumentException, UnsupportedOperationException
{
+		org.openrdf.model.Literal lit = valueFactory.createLiteral(lexicalForm);
+		return (LiteralImpl)asRDFTerm(lit);
+	}
+
+	@Override
+	public org.apache.commons.rdf.api.Literal createLiteral(String lexicalForm, org.apache.commons.rdf.api.IRI
dataType)
+			throws IllegalArgumentException, UnsupportedOperationException {
+		org.openrdf.model.IRI iri = valueFactory.createIRI(dataType.getIRIString());
+		org.openrdf.model.Literal lit = valueFactory.createLiteral(lexicalForm, iri);
+		return (org.apache.commons.rdf.api.Literal)asRDFTerm(lit);
+	}
+	
+	@Override
+	public org.apache.commons.rdf.api.Literal createLiteral(String lexicalForm, String languageTag)
+			throws IllegalArgumentException, UnsupportedOperationException {
+		org.openrdf.model.Literal lit = valueFactory.createLiteral(lexicalForm, languageTag);
+		return (org.apache.commons.rdf.api.Literal)asRDFTerm(lit);
+	}
+	
+	@Override
+	public RDF4JIRI createIRI(String iri) throws IllegalArgumentException, UnsupportedOperationException
{
+		return (RDF4JIRI) asRDFTerm(valueFactory.createIRI(iri));
+	}
+	
+	@Override
+	public GraphImpl createGraph() throws UnsupportedOperationException {
+		return asRDFTermGraph(new LinkedHashModel());
+	}
+	
+	public Statement asStatement(Triple triple) {
+		return valueFactory.createStatement(
+				(org.openrdf.model.Resource) asValue(triple.getSubject()), 
+				(org.openrdf.model.IRI) asValue(triple.getPredicate()), 
+				asValue(triple.getObject()));
+	}
+	
+	@Override
+	public Triple createTriple(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI predicate,
RDFTerm object)
+			throws IllegalArgumentException, UnsupportedOperationException {
+		final Statement statement = valueFactory.createStatement(
+				(org.openrdf.model.Resource) asValue(subject), 
+				(org.openrdf.model.IRI) asValue(predicate), 
+				asValue(object));
+		return asTriple(statement);
+	}	
+	
+	public Value asValue(RDFTerm object) {		
+		if (object == null) { 
+			return null;
+		}
+		if (object instanceof RDFTermImpl) {
+			// One of our own - avoid converting again.
+			// (This is crucial to avoid double-escaping in BlankNode)
+			return ((RDFTermImpl<?>)object).asValue();
+		}
+		if (object instanceof org.apache.commons.rdf.api.IRI) {
+			org.apache.commons.rdf.api.IRI iri = (org.apache.commons.rdf.api.IRI) object;
+			return valueFactory.createIRI(iri.getIRIString());
+		}
+		if (object instanceof org.apache.commons.rdf.api.Literal) {
+			org.apache.commons.rdf.api.Literal literal = (org.apache.commons.rdf.api.Literal) object;
+			String label = literal.getLexicalForm();
+			if (literal.getLanguageTag().isPresent()) {
+				String lang = literal.getLanguageTag().get();
+				return valueFactory.createLiteral(label, lang);
+			}
+			org.openrdf.model.IRI dataType = (org.openrdf.model.IRI ) asValue(literal.getDatatype());
+			return valueFactory.createLiteral(label, dataType);
+		}
+		if (object instanceof BlankNode) {
+			// This is where it gets tricky to support round trips!			
+			BlankNode blankNode = (BlankNode) object;
+			// FIXME: The uniqueReference might not be a valid BlankNode identifier..
+			// does it have to be?
+			return valueFactory.createBNode(blankNode.uniqueReference());
+		}
+		throw new IllegalArgumentException("RDFTerm was not an IRI, Literal or BlankNode: " + object.getClass());
+	}
+
+	public GraphImpl asRDFTermGraph(Model model) {
+		return new GraphImpl(model);
+	}
+
+	public RDF4JTriple asTriple(final Statement statement) {
+		return new TripleImpl(statement);
+	}
+
+	public RDF4JTerm<?> asRDFTerm(final org.openrdf.model.Value value) {		
+		if (value instanceof BNode) {
+			return new BlankNodeImpl((BNode) value);
+		}
+		if (value instanceof org.openrdf.model.Literal) {
+			return new LiteralImpl((org.openrdf.model.Literal) value);
+		}
+		if (value instanceof org.openrdf.model.IRI) {
+			return new IRIImpl((org.openrdf.model.IRI) value);
+		}
+		throw new IllegalArgumentException("Value is not a BNode, Literal or IRI: " + value.getClass());
	
+	}
+	
+}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/730a9cd0/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/Rdf4JRDFTermFactory.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/Rdf4JRDFTermFactory.java b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/Rdf4JRDFTermFactory.java
deleted file mode 100644
index 963f6d2..0000000
--- a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/Rdf4JRDFTermFactory.java
+++ /dev/null
@@ -1,479 +0,0 @@
-/**
- * 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.rdf4j;
-
-import java.nio.charset.StandardCharsets;
-import java.util.Objects;
-import java.util.Optional;
-import java.util.UUID;
-import java.util.stream.Stream;
-
-
-// To avoid confusion, avoid importing 
-// classes that are in both
-// commons.rdf and openrdf.model (e.g. IRI)
-import org.apache.commons.rdf.api.BlankNode;
-import org.apache.commons.rdf.api.BlankNodeOrIRI;
-import org.apache.commons.rdf.api.RDFTerm;
-import org.apache.commons.rdf.api.RDFTermFactory;
-import org.apache.commons.rdf.api.Triple;
-import org.openrdf.model.BNode;
-import org.openrdf.model.Model;
-import org.openrdf.model.Resource;
-import org.openrdf.model.Statement;
-import org.openrdf.model.Value;
-import org.openrdf.model.ValueFactory;
-import org.openrdf.model.impl.LinkedHashModel;
-import org.openrdf.model.impl.SimpleValueFactory;
-import org.openrdf.model.vocabulary.XMLSchema;
-import org.openrdf.rio.turtle.TurtleUtil;
-
-public class Rdf4JRDFTermFactory implements RDFTermFactory {
-	
-	private abstract class RDFTermImpl<T extends Value> implements RDF4JTerm<T>
{
-		T value;
-
-		public T asValue() { 
-			return value;
-		}
-		
-		RDFTermImpl(T value) {
-			this.value = value;			
-		}
-	}
-	
-	private final class GraphImpl implements RDF4JGraph {
-		
-		private Model model;
-
-		public Model asModel() { 
-			return model;
-		}
-		
-		GraphImpl(Model model) {
-			this.model = model;		
-		}
-		
-		@Override
-		public long size() {
-			int size = model.size();
-			if (size < Integer.MAX_VALUE) {
-				return size;
-			} else {
-				// Collection.size() can't help us, we'll have to count
-				return model.parallelStream().count();
-			}				
-		}
-
-		@Override
-		public void remove(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI predicate, RDFTerm
object) {
-			model.remove(
-					(Resource)asValue(subject), 
-					(org.openrdf.model.IRI)asValue(predicate), 
-					asValue(object));
-			
-		}
-
-		@Override
-		public void remove(Triple triple) { 
-			remove(triple.getSubject(), triple.getPredicate(), triple.getObject());
-			
-			// This however fails:
-			// model.remove(asStatement(triple));
-		}
-
-		@Override
-		public Stream<? extends Triple> getTriples(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI
predicate, RDFTerm object) {
-			return model.filter(
-					(Resource)asValue(subject), 
-					(org.openrdf.model.IRI)asValue(predicate), 
-					asValue(object)).parallelStream()
-				.map(Rdf4JRDFTermFactory.this::asTriple);
-		}
-
-		@Override
-		public Stream<Triple> getTriples() {
-			return model.parallelStream().map(Rdf4JRDFTermFactory.this::asTriple);
-		}
-
-		@Override
-		public boolean contains(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI predicate,
RDFTerm object) {
-			return model.contains(
-					(Resource)asValue(subject), 
-					(org.openrdf.model.IRI)asValue(predicate), 
-					asValue(object));
-		}
-
-		@Override
-		public boolean contains(Triple triple) {
-			return model.contains(asStatement(triple));
-		}
-
-		@Override
-		public void clear() {
-			model.clear();
-		}
-
-		@Override
-		public void add(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI predicate, RDFTerm
object) {
-			model.add(
-					(Resource)asValue(subject), 
-					(org.openrdf.model.IRI)asValue(predicate), 
-					asValue(object));				
-		}
-
-		@Override
-		public void add(Triple triple) {
-			model.add(asStatement(triple));
-		}
-	}
-
-	private final class TripleImpl implements Triple, RDF4JTriple {
-		private final Statement statement;
-
-		TripleImpl(Statement statement) {
-			this.statement = statement;
-		}
-
-		public Statement asStatement() { 
-			return statement;
-		}
-		
-		@Override
-		public BlankNodeOrIRI getSubject() {
-			return (BlankNodeOrIRI) asRDFTerm(statement.getSubject());
-		}
-
-		@Override
-		public org.apache.commons.rdf.api.IRI getPredicate() {
-			return (org.apache.commons.rdf.api.IRI) asRDFTerm(statement.getPredicate());
-		}
-
-		@Override
-		public RDFTerm getObject() {
-			return asRDFTerm(statement.getObject());
-		}
-		
-		@Override
-		public boolean equals(Object obj) {
-			if (obj instanceof Triple) {
-				Triple triple = (Triple) obj;
-				return getSubject().equals(triple.getSubject()) &&
-						getPredicate().equals(triple.getPredicate()) && 
-						getObject().equals(triple.getObject());
-			}
-			return false;
-		}
-
-		@Override
-		public int hashCode() {
-			return Objects.hash(getSubject(), getPredicate(), getObject());
-		}
-		
-		@Override
-		public String toString() {
-			return statement.toString();
-		}
-	}
-
-	private final class IRIImpl extends RDFTermImpl<org.openrdf.model.IRI> 
-		implements RDF4JIRI {
-
-		IRIImpl(org.openrdf.model.IRI iri) {
-			super(iri);			
-		}
-		@Override
-		public String ntriplesString() {
-			return "<" + value.toString() +  ">";
-		}
-
-		@Override
-		public String getIRIString() {
-			return value.toString();
-		}
-
-		@Override
-		public String toString() {
-			return value.toString();
-		}
-
-		public int hashCode() {
-			// Same definition
-			return value.hashCode();
-		}
-		@Override
-		public boolean equals(Object obj) {
-			if (obj == this) { return true; }
-			if (obj instanceof IRIImpl) {
-				IRIImpl impl = (IRIImpl) obj; 
-				return asValue().equals(impl.asValue());
-			}
-			if (obj instanceof org.apache.commons.rdf.api.IRI) {
-				org.apache.commons.rdf.api.IRI iri = (org.apache.commons.rdf.api.IRI) obj;
-				return value.toString().equals(iri.getIRIString());
-			}
-			return false;
-		}
-		
-	}
-
-	private final class LiteralImpl 
-		extends RDFTermImpl<org.openrdf.model.Literal>
-	    implements RDF4JLiteral {		
-
-		private static final String QUOTE = "\"";
-		
-		LiteralImpl(org.openrdf.model.Literal literal) {
-			super(literal);			
-		}
-		@Override
-		public String ntriplesString() {
-			// TODO: Use a more efficient StringBuffer
-			String escaped = QUOTE + TurtleUtil.encodeString(value.getLabel()) + QUOTE;
-			if (value.getLanguage().isPresent()) {
-				return escaped + "@" + value.getLanguage().get();
-			}
-			if (value.getDatatype().equals(XMLSchema.STRING)) { 
-				return escaped;
-			}
-			return escaped + "^^<" + TurtleUtil.encodeURIString(value.getDatatype().toString())
+ ">";
-		}
-
-		@Override
-		public String getLexicalForm() {
-			return value.getLabel();
-		}
-
-		@Override
-		public org.apache.commons.rdf.api.IRI getDatatype() {
-			return (org.apache.commons.rdf.api.IRI) asRDFTerm(value.getDatatype());
-		}
-
-		@Override
-		public Optional<String> getLanguageTag() {
-			return value.getLanguage();
-		}
-
-		@Override
-		public String toString() {
-			return ntriplesString();
-		}
-
-		@Override
-		public boolean equals(Object obj) {
-			if (obj == this) { return true; }
-			if (obj instanceof org.apache.commons.rdf.api.Literal) {
-				org.apache.commons.rdf.api.Literal other = (org.apache.commons.rdf.api.Literal) obj;
-				return getLexicalForm().equals(other.getLexicalForm()) &&
-						getDatatype().equals(other.getDatatype()) && 
-						getLanguageTag().equals(other.getLanguageTag());
-				
-			}
-			return false;
-		}
-
-		public int hashCode() {
-			return Objects.hash(value.getLabel(), value.getDatatype(), value.getLanguage());
-		}
-	}
-
-	private final class BlankNodeImpl extends RDFTermImpl<BNode>
-		implements RDF4JBlankNode {
-		
-		BlankNodeImpl(BNode bNode) {
-			super(bNode);			
-		}
-		
-		@Override
-		public String ntriplesString() {
-			if (isValidBlankNodeLabel(value.getID())) { 
-				return "_:" + value.getID();
-			} else {
-				return "_:" + UUID.nameUUIDFromBytes(value.getID().getBytes(StandardCharsets.UTF_8));
-			}
-		}
-
-		private boolean isValidBlankNodeLabel(String id) {
-			// FIXME: Replace with a regular expression?			
-			if (id.isEmpty()) { 
-				return false;
-			}
-			if (! TurtleUtil.isBLANK_NODE_LABEL_StartChar(id.codePointAt(0)))  {
-				return false;
-			}
-			for (int i=1; i<id.length(); i++) { 
-				if (! TurtleUtil.isBLANK_NODE_LABEL_Char(id.codePointAt(i))) { 
-					return false;
-				}
-			}
-			return true;
-		}
-
-		@Override
-		public String uniqueReference() {
-			return salt + value.getID();
-		}
-
-		@Override
-		public int hashCode() {
-			return uniqueReference().hashCode();
-		}
-
-		public boolean equals(Object obj) { 
-			if (obj == this) { 
-				return true;
-			}
-			// NOTE: Do NOT use Bnode.equals() as it has a more generous
-			// equality based only on the value.getID();			
-			if (obj instanceof BlankNode) {
-				BlankNode blankNode = (BlankNode) obj;
-				return uniqueReference().equals(blankNode.uniqueReference());								
-			}
-			return false;
-		}
-	}
-
-	
-
-	private ValueFactory valueFactory;
-	
-	private String salt = "urn:uuid:" + UUID.randomUUID() + "#";
-	
-	public Rdf4JRDFTermFactory() {
-		this.valueFactory = SimpleValueFactory.getInstance();
-	}
-	
-	public Rdf4JRDFTermFactory(ValueFactory valueFactory) { 
-		this.valueFactory = valueFactory;
-	}	
-	
-	@Override
-	public BlankNodeImpl createBlankNode() throws UnsupportedOperationException {
-		BNode bnode = valueFactory.createBNode();
-		return (BlankNodeImpl)asRDFTerm(bnode);
-	}
-	
-	@Override
-	public BlankNodeImpl createBlankNode(String name) throws UnsupportedOperationException {
-		BNode bnode = valueFactory.createBNode(name);
-		return (BlankNodeImpl)asRDFTerm(bnode);
-	}
-	
-	@Override
-	public LiteralImpl createLiteral(String lexicalForm) throws IllegalArgumentException, UnsupportedOperationException
{
-		org.openrdf.model.Literal lit = valueFactory.createLiteral(lexicalForm);
-		return (LiteralImpl)asRDFTerm(lit);
-	}
-
-	@Override
-	public org.apache.commons.rdf.api.Literal createLiteral(String lexicalForm, org.apache.commons.rdf.api.IRI
dataType)
-			throws IllegalArgumentException, UnsupportedOperationException {
-		org.openrdf.model.IRI iri = valueFactory.createIRI(dataType.getIRIString());
-		org.openrdf.model.Literal lit = valueFactory.createLiteral(lexicalForm, iri);
-		return (org.apache.commons.rdf.api.Literal)asRDFTerm(lit);
-	}
-	
-	@Override
-	public org.apache.commons.rdf.api.Literal createLiteral(String lexicalForm, String languageTag)
-			throws IllegalArgumentException, UnsupportedOperationException {
-		org.openrdf.model.Literal lit = valueFactory.createLiteral(lexicalForm, languageTag);
-		return (org.apache.commons.rdf.api.Literal)asRDFTerm(lit);
-	}
-	
-	@Override
-	public RDF4JIRI createIRI(String iri) throws IllegalArgumentException, UnsupportedOperationException
{
-		return (RDF4JIRI) asRDFTerm(valueFactory.createIRI(iri));
-	}
-	
-	@Override
-	public GraphImpl createGraph() throws UnsupportedOperationException {
-		return asRDFTermGraph(new LinkedHashModel());
-	}
-	
-	public Statement asStatement(Triple triple) {
-		return valueFactory.createStatement(
-				(org.openrdf.model.Resource) asValue(triple.getSubject()), 
-				(org.openrdf.model.IRI) asValue(triple.getPredicate()), 
-				asValue(triple.getObject()));
-	}
-	
-	@Override
-	public Triple createTriple(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI predicate,
RDFTerm object)
-			throws IllegalArgumentException, UnsupportedOperationException {
-		final Statement statement = valueFactory.createStatement(
-				(org.openrdf.model.Resource) asValue(subject), 
-				(org.openrdf.model.IRI) asValue(predicate), 
-				asValue(object));
-		return asTriple(statement);
-	}	
-	
-	public Value asValue(RDFTerm object) {		
-		if (object == null) { 
-			return null;
-		}
-		if (object instanceof RDFTermImpl) {
-			// One of our own - avoid converting again.
-			// (This is crucial to avoid double-escaping in BlankNode)
-			return ((RDFTermImpl<?>)object).asValue();
-		}
-		if (object instanceof org.apache.commons.rdf.api.IRI) {
-			org.apache.commons.rdf.api.IRI iri = (org.apache.commons.rdf.api.IRI) object;
-			return valueFactory.createIRI(iri.getIRIString());
-		}
-		if (object instanceof org.apache.commons.rdf.api.Literal) {
-			org.apache.commons.rdf.api.Literal literal = (org.apache.commons.rdf.api.Literal) object;
-			String label = literal.getLexicalForm();
-			if (literal.getLanguageTag().isPresent()) {
-				String lang = literal.getLanguageTag().get();
-				return valueFactory.createLiteral(label, lang);
-			}
-			org.openrdf.model.IRI dataType = (org.openrdf.model.IRI ) asValue(literal.getDatatype());
-			return valueFactory.createLiteral(label, dataType);
-		}
-		if (object instanceof BlankNode) {
-			// This is where it gets tricky to support round trips!			
-			BlankNode blankNode = (BlankNode) object;
-			// FIXME: The uniqueReference might not be a valid BlankNode identifier..
-			// does it have to be?
-			return valueFactory.createBNode(blankNode.uniqueReference());
-		}
-		throw new IllegalArgumentException("RDFTerm was not an IRI, Literal or BlankNode: " + object.getClass());
-	}
-
-	public GraphImpl asRDFTermGraph(Model model) {
-		return new GraphImpl(model);
-	}
-
-	public RDF4JTriple asTriple(final Statement statement) {
-		return new TripleImpl(statement);
-	}
-
-	public RDF4JTerm<?> asRDFTerm(final org.openrdf.model.Value value) {		
-		if (value instanceof BNode) {
-			return new BlankNodeImpl((BNode) value);
-		}
-		if (value instanceof org.openrdf.model.Literal) {
-			return new LiteralImpl((org.openrdf.model.Literal) value);
-		}
-		if (value instanceof org.openrdf.model.IRI) {
-			return new IRIImpl((org.openrdf.model.IRI) value);
-		}
-		throw new IllegalArgumentException("Value is not a BNode, Literal or IRI: " + value.getClass());
	
-	}
-	
-}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/730a9cd0/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/BlankNodeTest.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/BlankNodeTest.java b/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/BlankNodeTest.java
index 4d52f36..09ae666 100644
--- a/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/BlankNodeTest.java
+++ b/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/BlankNodeTest.java
@@ -22,7 +22,7 @@ import org.apache.commons.rdf.api.BlankNode;
 
 public class BlankNodeTest extends AbstractBlankNodeTest {
 
-	Rdf4JRDFTermFactory factory = new Rdf4JRDFTermFactory();
+	RDF4JTermFactory factory = new RDF4JTermFactory();
 	
 	@Override
 	protected BlankNode getBlankNode() {

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/730a9cd0/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/GraphTest.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/GraphTest.java b/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/GraphTest.java
index 2a66f97..09ad421 100644
--- a/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/GraphTest.java
+++ b/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/GraphTest.java
@@ -24,7 +24,7 @@ public class GraphTest extends AbstractGraphTest{
 
 	@Override
 	public RDFTermFactory createFactory() {
-		return new Rdf4JRDFTermFactory();
+		return new RDF4JTermFactory();
 	}
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/730a9cd0/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/MemoryGraphTest.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/MemoryGraphTest.java b/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/MemoryGraphTest.java
index 8757193..7acfa4e 100644
--- a/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/MemoryGraphTest.java
+++ b/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/MemoryGraphTest.java
@@ -23,7 +23,7 @@ import org.openrdf.sail.memory.model.MemValueFactory;
 
 public class MemoryGraphTest extends AbstractGraphTest {
 
-	public static final class MemoryStoreFactory extends Rdf4JRDFTermFactory {
+	public static final class MemoryStoreFactory extends RDF4JTermFactory {
 		MemoryStoreFactory() {
 			super(new MemValueFactory());
 		}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/730a9cd0/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/Rdf4JRDFTermFactoryTest.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/Rdf4JRDFTermFactoryTest.java
b/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/Rdf4JRDFTermFactoryTest.java
index a91bf78..b08b966 100644
--- a/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/Rdf4JRDFTermFactoryTest.java
+++ b/rdf4j/src/test/java/org/apache/commons/rdf/rdf4j/Rdf4JRDFTermFactoryTest.java
@@ -26,7 +26,7 @@ public class Rdf4JRDFTermFactoryTest extends AbstractRDFTermFactoryTest
{
 
 	@Override
 	public RDFTermFactory createFactory() {
-		return new Rdf4JRDFTermFactory();
+		return new RDF4JTermFactory();
 	}
 	
 	@Override


Mime
View raw message