commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ans...@apache.org
Subject [03/18] incubator-commonsrdf git commit: Optimise GraphImpl.add(Triple)
Date Mon, 30 Mar 2015 01:09:57 GMT
Optimise GraphImpl.add(Triple)

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

Branch: refs/heads/master
Commit: 7e42caeef4d8eb3d784358b8b43b4d1262b567cc
Parents: 243e85c
Author: Peter Ansell <p_ansell@yahoo.com>
Authored: Sat Mar 28 20:31:32 2015 +1100
Committer: Peter Ansell <p_ansell@yahoo.com>
Committed: Sat Mar 28 20:32:09 2015 +1100

----------------------------------------------------------------------
 .../apache/commons/rdf/simple/GraphImpl.java    | 34 ++++++++++++--------
 1 file changed, 21 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/7e42caee/simple/src/main/java/org/apache/commons/rdf/simple/GraphImpl.java
----------------------------------------------------------------------
diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/GraphImpl.java b/simple/src/main/java/org/apache/commons/rdf/simple/GraphImpl.java
index 3fd4bca..5f81048 100644
--- a/simple/src/main/java/org/apache/commons/rdf/simple/GraphImpl.java
+++ b/simple/src/main/java/org/apache/commons/rdf/simple/GraphImpl.java
@@ -19,7 +19,6 @@ package org.apache.commons.rdf.simple;
 
 import java.util.LinkedHashSet;
 import java.util.Objects;
-import java.util.Optional;
 import java.util.Set;
 import java.util.function.Predicate;
 import java.util.stream.Collectors;
@@ -52,10 +51,28 @@ final class GraphImpl implements Graph {
 
 	@Override
 	public void add(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
-		add(new TripleImpl(Objects.requireNonNull(subject),
-				Objects.requireNonNull(predicate),
-				Objects.requireNonNull(object)));
+		BlankNodeOrIRI newSubject = (BlankNodeOrIRI) internallyMap(subject);
+		IRI newPredicate = (IRI) internallyMap(predicate);
+		RDFTerm newObject = internallyMap(object);
+		triples.add(factory.createTriple(newSubject, newPredicate, newObject));
+	}
 
+	@Override
+	public void add(Triple triple) {
+		BlankNodeOrIRI newSubject = (BlankNodeOrIRI) internallyMap(triple
+				.getSubject());
+		IRI newPredicate = (IRI) internallyMap(triple.getPredicate());
+		RDFTerm newObject = internallyMap(triple.getObject());
+		// Check if any of the object references changed during the mapping, to
+		// avoid creating a new Triple object if possible
+		if (newSubject == triple.getSubject()
+				&& newPredicate == triple.getPredicate()
+				&& newObject == triple.getObject()) {
+			triples.add(triple);
+		} else {
+			triples.add(factory.createTriple(newSubject, newPredicate,
+					newObject));
+		}
 	}
 
 	private <T extends RDFTerm> RDFTerm internallyMap(T object) {
@@ -87,15 +104,6 @@ final class GraphImpl implements Graph {
 	}
 
 	@Override
-	public void add(Triple triple) {
-		BlankNodeOrIRI subject = (BlankNodeOrIRI) internallyMap(triple
-				.getSubject());
-		IRI predicate = (IRI) internallyMap(triple.getPredicate());
-		RDFTerm object = internallyMap(triple.getObject());
-		triples.add(factory.createTriple(subject, predicate, object));
-	}
-
-	@Override
 	public void clear() {
 		triples.clear();
 	}


Mime
View raw message