commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [4/7] incubator-commonsrdf git commit: Exctracted rdf4j implementation classes
Date Fri, 03 Jun 2016 13:28:38 GMT
Exctracted rdf4j implementation classes


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

Branch: refs/heads/rdf4j
Commit: 16ff4466cd5e01ec1d59dce55294a28d908e314c
Parents: ed0213d
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Thu Jun 2 18:43:46 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Thu Jun 2 18:43:46 2016 +0100

----------------------------------------------------------------------
 .../commons/rdf/rdf4j/RDF4JTermFactory.java     | 346 +------------------
 .../commons/rdf/rdf4j/impl/AbstractRDFTerm.java |  16 +
 .../commons/rdf/rdf4j/impl/BlankNodeImpl.java   |  93 +++++
 .../commons/rdf/rdf4j/impl/GraphImpl.java       | 119 +++++++
 .../apache/commons/rdf/rdf4j/impl/IRIImpl.java  |  61 ++++
 .../commons/rdf/rdf4j/impl/LiteralImpl.java     |  85 +++++
 .../commons/rdf/rdf4j/impl/TripleImpl.java      |  61 ++++
 7 files changed, 446 insertions(+), 335 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/16ff4466/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 b60c726..e258497 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,12 +17,7 @@
  */
 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
@@ -33,16 +28,18 @@ import org.apache.commons.rdf.api.Graph;
 import org.apache.commons.rdf.api.RDFTerm;
 import org.apache.commons.rdf.api.RDFTermFactory;
 import org.apache.commons.rdf.api.Triple;
+import org.apache.commons.rdf.rdf4j.impl.BlankNodeImpl;
+import org.apache.commons.rdf.rdf4j.impl.GraphImpl;
+import org.apache.commons.rdf.rdf4j.impl.IRIImpl;
+import org.apache.commons.rdf.rdf4j.impl.LiteralImpl;
+import org.apache.commons.rdf.rdf4j.impl.TripleImpl;
 import org.eclipse.rdf4j.model.BNode;
 import org.eclipse.rdf4j.model.Model;
-import org.eclipse.rdf4j.model.Resource;
 import org.eclipse.rdf4j.model.Statement;
 import org.eclipse.rdf4j.model.Value;
 import org.eclipse.rdf4j.model.ValueFactory;
 import org.eclipse.rdf4j.model.impl.LinkedHashModel;
 import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
-import org.eclipse.rdf4j.model.vocabulary.XMLSchema;
-import org.eclipse.rdf4j.rio.turtle.TurtleUtil;
 
 /**
  * RDF4J implementation of RDFTermFactory
@@ -236,15 +233,15 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	}
 
 	@Override
-	public BlankNodeImpl createBlankNode() throws UnsupportedOperationException {
+	public RDF4JBlankNode createBlankNode() throws UnsupportedOperationException {
 		BNode bnode = valueFactory.createBNode();
-		return (BlankNodeImpl)asRDFTerm(bnode);
+		return (RDF4JBlankNode)asRDFTerm(bnode);
 	}
 
 	@Override
-	public BlankNodeImpl createBlankNode(String name) throws UnsupportedOperationException {
+	public RDF4JBlankNode createBlankNode(String name) throws UnsupportedOperationException
{
 		BNode bnode = valueFactory.createBNode(name);
-		return (BlankNodeImpl)asRDFTerm(bnode);
+		return (RDF4JBlankNode)asRDFTerm(bnode);
 	}
 
 	@Override
@@ -258,9 +255,9 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	}
 
 	@Override
-	public LiteralImpl createLiteral(String lexicalForm) throws IllegalArgumentException, UnsupportedOperationException
{
+	public RDF4JLiteral createLiteral(String lexicalForm) throws IllegalArgumentException, UnsupportedOperationException
{
 		org.eclipse.rdf4j.model.Literal lit = valueFactory.createLiteral(lexicalForm);
-		return (LiteralImpl)asRDFTerm(lit);
+		return (RDF4JLiteral)asRDFTerm(lit);
 	}
 
 	@Override
@@ -289,325 +286,4 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	}
 
 	
-	private static final class BlankNodeImpl extends RDFTermImpl<BNode>
-		implements RDF4JBlankNode {
-		
-		private transient int hashCode = 0;
-		private long saltUUIDmost;
-		private long saltUUIDleast;
-		
-		BlankNodeImpl(BNode bNode, UUID salt) {
-			super(bNode);			
-			// Space-efficient storage of salt UUID
-			saltUUIDmost = salt.getMostSignificantBits();
-			saltUUIDleast = salt.getLeastSignificantBits();
-		}
-		
-		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;
-		}
-	
-		@Override
-		public int hashCode() {
-			if (hashCode != 0) {
-				return hashCode;
-			}
-			return hashCode = uniqueReference().hashCode();
-		}
-	
-		@Override
-		public String ntriplesString() {
-			if (isValidBlankNodeLabel(value.getID())) { 
-				return "_:" + value.getID();
-			} else {
-				return "_:" + UUID.nameUUIDFromBytes(value.getID().getBytes(StandardCharsets.UTF_8));
-			}
-		}
-	
-		@Override
-		public String uniqueReference() {
-			UUID uuid = new UUID(saltUUIDmost, saltUUIDleast);
-			return "urn:uuid:" + uuid + "#" + value.getID();
-		}
-	
-		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;
-		}
-	}
-
-	private final static class GraphImpl implements RDF4JGraph {
-		
-		private Model model;
-		private RDF4JTermFactory rdf4jTermFactory;
-	
-		GraphImpl(Model model) {
-			this.model = model;	
-			this.rdf4jTermFactory = new RDF4JTermFactory();
-		}
-
-		GraphImpl(Model model, RDF4JTermFactory rdf4jTermFactory) {
-			this.model = model;	
-			this.rdf4jTermFactory = rdf4jTermFactory;
-		}
-		
-		@Override
-		public void add(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI predicate, RDFTerm
object) {
-			model.add(
-					(Resource)rdf4jTermFactory.asValue(subject), 
-					(org.eclipse.rdf4j.model.IRI)rdf4jTermFactory.asValue(predicate), 
-					rdf4jTermFactory.asValue(object));				
-		}
-		
-		@Override
-		public void add(Triple triple) {
-			model.add(rdf4jTermFactory.asStatement(triple));
-		}
-	
-		public Model asModel() { 
-			return model;
-		}
-	
-		@Override
-		public void clear() {
-			model.clear();
-		}
-	
-		@Override
-		public boolean contains(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI predicate,
RDFTerm object) {
-			return model.contains(
-					(Resource)rdf4jTermFactory.asValue(subject), 
-					(org.eclipse.rdf4j.model.IRI)rdf4jTermFactory.asValue(predicate), 
-					rdf4jTermFactory.asValue(object));
-		}
-	
-		@Override
-		public boolean contains(Triple triple) {
-			return model.contains(rdf4jTermFactory.asStatement(triple));
-		}
-	
-		@Override
-		public Stream<RDF4JTriple> stream() {
-			return model.parallelStream().map(rdf4jTermFactory::asTriple);
-		}
-	
-		@Override
-		public Stream<RDF4JTriple> stream(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI
predicate, RDFTerm object) {
-			return model.filter(
-					(Resource)rdf4jTermFactory.asValue(subject), 
-					(org.eclipse.rdf4j.model.IRI)rdf4jTermFactory.asValue(predicate), 
-					rdf4jTermFactory.asValue(object)).parallelStream()
-				.map(rdf4jTermFactory::asTriple);
-		}
-	
-		@Override
-		public void remove(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI predicate, RDFTerm
object) {
-			model.remove(
-					(Resource)rdf4jTermFactory.asValue(subject), 
-					(org.eclipse.rdf4j.model.IRI)rdf4jTermFactory.asValue(predicate), 
-					rdf4jTermFactory.asValue(object));
-			
-		}
-	
-		@Override
-		public void remove(Triple triple) { 
-			model.remove(rdf4jTermFactory.asStatement(triple));
-		}
-	
-		@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();
-			}				
-		}
-	}
-
-	private final static class IRIImpl extends RDFTermImpl<org.eclipse.rdf4j.model.IRI>

-		implements RDF4JIRI {
-	
-		IRIImpl(org.eclipse.rdf4j.model.IRI iri) {
-			super(iri);			
-		}
-		@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;
-		}
-	
-		@Override
-		public String getIRIString() {
-			return value.toString();
-		}
-	
-		public int hashCode() {
-			// Same definition
-			return value.hashCode();
-		}
-	
-		@Override
-		public String ntriplesString() {
-			return "<" + value.toString() +  ">";
-		}
-		@Override
-		public String toString() {
-			return value.toString();
-		}
-		
-	}
-
-	private final static class LiteralImpl 
-		extends RDFTermImpl<org.eclipse.rdf4j.model.Literal>
-	    implements RDF4JLiteral {		
-	
-		private static final String QUOTE = "\"";
-		
-		LiteralImpl(org.eclipse.rdf4j.model.Literal literal) {
-			super(literal);			
-		}
-		@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;
-		}
-	
-		@Override
-		public org.apache.commons.rdf.api.IRI getDatatype() {
-			return new IRIImpl(value.getDatatype());
-		}
-	
-		@Override
-		public Optional<String> getLanguageTag() {
-			return value.getLanguage();
-		}
-	
-		@Override
-		public String getLexicalForm() {
-			return value.getLabel();
-		}
-	
-		public int hashCode() {
-			return Objects.hash(value.getLabel(), value.getDatatype(), value.getLanguage());
-		}
-	
-		@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 toString() {
-			return ntriplesString();
-		}
-	}	
-
-	private static abstract class RDFTermImpl<T extends Value> implements RDF4JTerm<T>
{
-		T value;
-	
-		RDFTermImpl(T value) {
-			this.value = value;			
-		}
-		
-		public T asValue() { 
-			return value;
-		}
-	}
-	
-	private final static class TripleImpl implements Triple, RDF4JTriple {
-		private final Statement statement;	
-		private UUID salt;
-		
-		TripleImpl(Statement statement, UUID salt) {
-			this.statement = statement;
-			this.salt = salt;
-		}
-	
-		public Statement asStatement() { 
-			return statement;
-		}
-		
-		@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 RDFTerm getObject() {
-			return asRDFTerm(statement.getObject(), salt);
-		}
-	
-		@Override
-		public org.apache.commons.rdf.api.IRI getPredicate() {
-			return (org.apache.commons.rdf.api.IRI) asRDFTerm(statement.getPredicate(), null);
-		}
-		
-		@Override
-		public BlankNodeOrIRI getSubject() {
-			return (BlankNodeOrIRI) asRDFTerm(statement.getSubject(), salt);
-		}
-	
-		@Override
-		public int hashCode() {
-			return Objects.hash(getSubject(), getPredicate(), getObject());
-		}
-		
-		@Override
-		public String toString() {
-			return statement.toString();
-		}
-}
-
-	
 }

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/16ff4466/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/AbstractRDFTerm.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/AbstractRDFTerm.java b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/AbstractRDFTerm.java
new file mode 100644
index 0000000..a5d5367
--- /dev/null
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/AbstractRDFTerm.java
@@ -0,0 +1,16 @@
+package org.apache.commons.rdf.rdf4j.impl;
+
+import org.apache.commons.rdf.rdf4j.RDF4JTerm;
+import org.eclipse.rdf4j.model.Value;
+
+abstract class AbstractRDFTerm<T extends Value> implements RDF4JTerm<T> {
+	T value;
+
+	AbstractRDFTerm(T value) {
+		this.value = value;			
+	}
+	
+	public T asValue() { 
+		return value;
+	}
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/16ff4466/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/BlankNodeImpl.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/BlankNodeImpl.java b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/BlankNodeImpl.java
new file mode 100644
index 0000000..6921f45
--- /dev/null
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/BlankNodeImpl.java
@@ -0,0 +1,93 @@
+/**
+ * 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.impl;
+
+import java.nio.charset.StandardCharsets;
+import java.util.UUID;
+
+import org.apache.commons.rdf.api.BlankNode;
+import org.apache.commons.rdf.rdf4j.RDF4JBlankNode;
+import org.eclipse.rdf4j.model.BNode;
+import org.eclipse.rdf4j.rio.turtle.TurtleUtil;
+
+public final class BlankNodeImpl extends AbstractRDFTerm<BNode>
+	implements RDF4JBlankNode {
+	
+	private transient int hashCode = 0;
+	private long saltUUIDmost;
+	private long saltUUIDleast;
+	
+	public BlankNodeImpl(BNode bNode, UUID salt) {
+		super(bNode);			
+		// Space-efficient storage of salt UUID
+		saltUUIDmost = salt.getMostSignificantBits();
+		saltUUIDleast = salt.getLeastSignificantBits();
+	}
+	
+	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;
+	}
+
+	@Override
+	public int hashCode() {
+		if (hashCode != 0) {
+			return hashCode;
+		}
+		return hashCode = uniqueReference().hashCode();
+	}
+
+	@Override
+	public String ntriplesString() {
+		if (isValidBlankNodeLabel(value.getID())) { 
+			return "_:" + value.getID();
+		} else {
+			return "_:" + UUID.nameUUIDFromBytes(value.getID().getBytes(StandardCharsets.UTF_8));
+		}
+	}
+
+	@Override
+	public String uniqueReference() {
+		UUID uuid = new UUID(saltUUIDmost, saltUUIDleast);
+		return "urn:uuid:" + uuid + "#" + value.getID();
+	}
+
+	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;
+	}
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/16ff4466/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/GraphImpl.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/GraphImpl.java b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/GraphImpl.java
new file mode 100644
index 0000000..966ac32
--- /dev/null
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/GraphImpl.java
@@ -0,0 +1,119 @@
+/**
+ * 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.impl;
+
+import java.util.stream.Stream;
+
+import org.apache.commons.rdf.api.BlankNodeOrIRI;
+import org.apache.commons.rdf.api.RDFTerm;
+import org.apache.commons.rdf.api.Triple;
+import org.apache.commons.rdf.rdf4j.RDF4JGraph;
+import org.apache.commons.rdf.rdf4j.RDF4JTermFactory;
+import org.apache.commons.rdf.rdf4j.RDF4JTriple;
+import org.eclipse.rdf4j.model.Model;
+import org.eclipse.rdf4j.model.Resource;
+
+public final class GraphImpl implements RDF4JGraph {
+	
+	private Model model;
+	private RDF4JTermFactory rdf4jTermFactory;
+
+	public GraphImpl(Model model) {
+		this.model = model;	
+		this.rdf4jTermFactory = new RDF4JTermFactory();
+	}
+
+	public GraphImpl(Model model, RDF4JTermFactory rdf4jTermFactory) {
+		this.model = model;	
+		this.rdf4jTermFactory = rdf4jTermFactory;
+	}
+	
+	@Override
+	public void add(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI predicate, RDFTerm
object) {
+		model.add(
+				(Resource)rdf4jTermFactory.asValue(subject), 
+				(org.eclipse.rdf4j.model.IRI)rdf4jTermFactory.asValue(predicate), 
+				rdf4jTermFactory.asValue(object));				
+	}
+	
+	@Override
+	public void add(Triple triple) {
+		model.add(rdf4jTermFactory.asStatement(triple));
+	}
+
+	public Model asModel() { 
+		return model;
+	}
+
+	@Override
+	public void clear() {
+		model.clear();
+	}
+
+	@Override
+	public boolean contains(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI predicate,
RDFTerm object) {
+		return model.contains(
+				(Resource)rdf4jTermFactory.asValue(subject), 
+				(org.eclipse.rdf4j.model.IRI)rdf4jTermFactory.asValue(predicate), 
+				rdf4jTermFactory.asValue(object));
+	}
+
+	@Override
+	public boolean contains(Triple triple) {
+		return model.contains(rdf4jTermFactory.asStatement(triple));
+	}
+
+	@Override
+	public Stream<RDF4JTriple> stream() {
+		return model.parallelStream().map(rdf4jTermFactory::asTriple);
+	}
+
+	@Override
+	public Stream<RDF4JTriple> stream(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI
predicate, RDFTerm object) {
+		return model.filter(
+				(Resource)rdf4jTermFactory.asValue(subject), 
+				(org.eclipse.rdf4j.model.IRI)rdf4jTermFactory.asValue(predicate), 
+				rdf4jTermFactory.asValue(object)).parallelStream()
+			.map(rdf4jTermFactory::asTriple);
+	}
+
+	@Override
+	public void remove(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI predicate, RDFTerm
object) {
+		model.remove(
+				(Resource)rdf4jTermFactory.asValue(subject), 
+				(org.eclipse.rdf4j.model.IRI)rdf4jTermFactory.asValue(predicate), 
+				rdf4jTermFactory.asValue(object));
+		
+	}
+
+	@Override
+	public void remove(Triple triple) { 
+		model.remove(rdf4jTermFactory.asStatement(triple));
+	}
+
+	@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();
+		}				
+	}
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/16ff4466/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/IRIImpl.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/IRIImpl.java b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/IRIImpl.java
new file mode 100644
index 0000000..9ae6741
--- /dev/null
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/IRIImpl.java
@@ -0,0 +1,61 @@
+/**
+ * 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.impl;
+
+import org.apache.commons.rdf.rdf4j.RDF4JIRI;
+
+public final class IRIImpl extends AbstractRDFTerm<org.eclipse.rdf4j.model.IRI> 
+	implements RDF4JIRI {
+
+	public IRIImpl(org.eclipse.rdf4j.model.IRI iri) {
+		super(iri);			
+	}
+	@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;
+	}
+
+	@Override
+	public String getIRIString() {
+		return value.toString();
+	}
+
+	public int hashCode() {
+		// Same definition
+		return value.hashCode();
+	}
+
+	@Override
+	public String ntriplesString() {
+		return "<" + value.toString() +  ">";
+	}
+	@Override
+	public String toString() {
+		return value.toString();
+	}
+	
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/16ff4466/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/LiteralImpl.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/LiteralImpl.java b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/LiteralImpl.java
new file mode 100644
index 0000000..0305059
--- /dev/null
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/LiteralImpl.java
@@ -0,0 +1,85 @@
+/**
+ * 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.impl;
+
+import java.util.Objects;
+import java.util.Optional;
+
+import org.apache.commons.rdf.rdf4j.RDF4JLiteral;
+import org.eclipse.rdf4j.model.vocabulary.XMLSchema;
+import org.eclipse.rdf4j.rio.turtle.TurtleUtil;
+
+public final class LiteralImpl 
+	extends AbstractRDFTerm<org.eclipse.rdf4j.model.Literal>
+    implements RDF4JLiteral {		
+
+	private static final String QUOTE = "\"";
+	
+	public LiteralImpl(org.eclipse.rdf4j.model.Literal literal) {
+		super(literal);			
+	}
+	@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;
+	}
+
+	@Override
+	public org.apache.commons.rdf.api.IRI getDatatype() {
+		return new IRIImpl(value.getDatatype());
+	}
+
+	@Override
+	public Optional<String> getLanguageTag() {
+		return value.getLanguage();
+	}
+
+	@Override
+	public String getLexicalForm() {
+		return value.getLabel();
+	}
+
+	public int hashCode() {
+		return Objects.hash(value.getLabel(), value.getDatatype(), value.getLanguage());
+	}
+
+	@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 toString() {
+		return ntriplesString();
+	}
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/16ff4466/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/TripleImpl.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/TripleImpl.java b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/TripleImpl.java
new file mode 100644
index 0000000..74564fb
--- /dev/null
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/TripleImpl.java
@@ -0,0 +1,61 @@
+package org.apache.commons.rdf.rdf4j.impl;
+
+import java.util.Objects;
+import java.util.UUID;
+
+import org.apache.commons.rdf.api.BlankNodeOrIRI;
+import org.apache.commons.rdf.api.RDFTerm;
+import org.apache.commons.rdf.api.Triple;
+import org.apache.commons.rdf.rdf4j.RDF4JTermFactory;
+import org.apache.commons.rdf.rdf4j.RDF4JTriple;
+import org.eclipse.rdf4j.model.Statement;
+
+public final class TripleImpl implements Triple, RDF4JTriple {
+		private final Statement statement;	
+		private UUID salt;
+		
+		public TripleImpl(Statement statement, UUID salt) {
+			this.statement = statement;
+			this.salt = salt;
+		}
+	
+		public Statement asStatement() { 
+			return statement;
+		}
+		
+		@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 RDFTerm getObject() {
+			return RDF4JTermFactory.asRDFTerm(statement.getObject(), salt);
+		}
+	
+		@Override
+		public org.apache.commons.rdf.api.IRI getPredicate() {
+			return (org.apache.commons.rdf.api.IRI) RDF4JTermFactory.asRDFTerm(statement.getPredicate(),
null);
+		}
+		
+		@Override
+		public BlankNodeOrIRI getSubject() {
+			return (BlankNodeOrIRI) RDF4JTermFactory.asRDFTerm(statement.getSubject(), salt);
+		}
+	
+		@Override
+		public int hashCode() {
+			return Objects.hash(getSubject(), getPredicate(), getObject());
+		}
+		
+		@Override
+		public String toString() {
+			return statement.toString();
+		}
+}
\ No newline at end of file


Mime
View raw message