commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [1/3] incubator-commonsrdf git commit: Added SimpleRDFTerm marker interface
Date Fri, 08 Apr 2016 15:22:21 GMT
Repository: incubator-commonsrdf
Updated Branches:
  refs/heads/quad 220f75f19 -> 683a2bbdf


Added SimpleRDFTerm marker interface

Now internallyMap() will fail early on mapping
any RDFTerm objects which are not IRI, BlankNode or Literal
(before they would have been added 'as-is')

This also fixes a bug in that Graph would have remapped
any of the Types instances to an IRIImpl.

I added SimpleRDFTerm interface as a package-protected
interface within SimpleRDFTermFactory - thus it should not
be easy to create any additional SimpleRDFTerm subclasses.


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

Branch: refs/heads/quad
Commit: 0a148cbab0c3c35aec9bd7dbd2b61029066d19a4
Parents: 73994db
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Fri Apr 8 16:18:26 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Fri Apr 8 16:18:26 2016 +0100

----------------------------------------------------------------------
 .../apache/commons/rdf/simple/BlankNodeImpl.java  |  3 ++-
 .../org/apache/commons/rdf/simple/GraphImpl.java  | 18 +++++++++++-------
 .../org/apache/commons/rdf/simple/IRIImpl.java    |  2 +-
 .../apache/commons/rdf/simple/LiteralImpl.java    |  2 +-
 .../commons/rdf/simple/SimpleRDFTermFactory.java  | 12 ++++++++++++
 .../java/org/apache/commons/rdf/simple/Types.java |  2 +-
 6 files changed, 28 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/0a148cba/simple/src/main/java/org/apache/commons/rdf/simple/BlankNodeImpl.java
----------------------------------------------------------------------
diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/BlankNodeImpl.java b/simple/src/main/java/org/apache/commons/rdf/simple/BlankNodeImpl.java
index 6e9e217..81c6848 100644
--- a/simple/src/main/java/org/apache/commons/rdf/simple/BlankNodeImpl.java
+++ b/simple/src/main/java/org/apache/commons/rdf/simple/BlankNodeImpl.java
@@ -23,11 +23,12 @@ import java.util.UUID;
 import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.commons.rdf.api.BlankNode;
+import org.apache.commons.rdf.simple.SimpleRDFTermFactory.SimpleRDFTerm;
 
 /**
  * A simple implementation of BlankNode.
  */
-final class BlankNodeImpl implements BlankNode {
+final class BlankNodeImpl implements BlankNode, SimpleRDFTerm {
 
     private static final UUID SALT = UUID.randomUUID();
     private static final AtomicLong COUNTER = new AtomicLong();

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/0a148cba/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 884f273..024da21 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
@@ -18,6 +18,7 @@
 package org.apache.commons.rdf.simple;
 
 import org.apache.commons.rdf.api.*;
+import org.apache.commons.rdf.simple.SimpleRDFTermFactory.SimpleRDFTerm;
 
 import java.util.HashSet;
 import java.util.Objects;
@@ -72,18 +73,23 @@ final class GraphImpl implements Graph {
     }
 
     private <T extends RDFTerm> RDFTerm internallyMap(T object) {
-        if (object instanceof BlankNode && !(object instanceof BlankNodeImpl)) {
+    	if (object == null || object instanceof SimpleRDFTerm) {
+    		// No need to re-map our own objects.
+    		// We support null as internallyMap() is also used by the filters, and the
+    		// factory constructors later do null checks
+    		return object;
+    	}
+        if (object instanceof BlankNode) {
             BlankNode blankNode = (BlankNode) object;
             // This guarantees that adding the same BlankNode multiple times to
             // this graph will generate a local object that is mapped to an
             // equivalent object, based on the code in the package private
             // BlankNodeImpl class
             return factory.createBlankNode(blankNode.uniqueReference());
-        } else if (object instanceof IRI && !(object instanceof IRIImpl)) {
+        } else if (object instanceof IRI) {
             IRI iri = (IRI) object;
             return factory.createIRI(iri.getIRIString());
-        } else if (object instanceof Literal
-                && !(object instanceof LiteralImpl)) {
+        } else if (object instanceof Literal) {
             Literal literal = (Literal) object;
             if (literal.getLanguageTag().isPresent()) {
                 return factory.createLiteral(literal.getLexicalForm(), literal
@@ -93,9 +99,7 @@ final class GraphImpl implements Graph {
                         (IRI) internallyMap(literal.getDatatype()));
             }
         } else {
-            // The object is a local implementation, and is not a BlankNode, so
-            // can be returned directly
-            return object;
+        	throw new IllegalArgumentException("RDFTerm was neither a BlankNode, IRI nor Literal:
" + object);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/0a148cba/simple/src/main/java/org/apache/commons/rdf/simple/IRIImpl.java
----------------------------------------------------------------------
diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/IRIImpl.java b/simple/src/main/java/org/apache/commons/rdf/simple/IRIImpl.java
index 0f6b065..fdec0ac 100644
--- a/simple/src/main/java/org/apache/commons/rdf/simple/IRIImpl.java
+++ b/simple/src/main/java/org/apache/commons/rdf/simple/IRIImpl.java
@@ -24,7 +24,7 @@ import java.net.URI;
 /**
  * A simple implementation of IRI.
  */
-final class IRIImpl implements IRI {
+final class IRIImpl implements IRI, SimpleRDFTermFactory.SimpleRDFTerm {
 
     private final String iri;
 

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/0a148cba/simple/src/main/java/org/apache/commons/rdf/simple/LiteralImpl.java
----------------------------------------------------------------------
diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/LiteralImpl.java b/simple/src/main/java/org/apache/commons/rdf/simple/LiteralImpl.java
index 7a7ed96..3d6874a 100644
--- a/simple/src/main/java/org/apache/commons/rdf/simple/LiteralImpl.java
+++ b/simple/src/main/java/org/apache/commons/rdf/simple/LiteralImpl.java
@@ -28,7 +28,7 @@ import org.apache.commons.rdf.api.Literal;
 /**
  * A simple implementation of Literal.
  */
-final class LiteralImpl implements Literal {
+final class LiteralImpl implements Literal, SimpleRDFTermFactory.SimpleRDFTerm {
 
     private static final String QUOTE = "\"";
 

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/0a148cba/simple/src/main/java/org/apache/commons/rdf/simple/SimpleRDFTermFactory.java
----------------------------------------------------------------------
diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/SimpleRDFTermFactory.java
b/simple/src/main/java/org/apache/commons/rdf/simple/SimpleRDFTermFactory.java
index 97ad340..36694bf 100644
--- a/simple/src/main/java/org/apache/commons/rdf/simple/SimpleRDFTermFactory.java
+++ b/simple/src/main/java/org/apache/commons/rdf/simple/SimpleRDFTermFactory.java
@@ -37,6 +37,18 @@ import org.apache.commons.rdf.api.Triple;
  */
 public class SimpleRDFTermFactory implements RDFTermFactory {
 
+	/**
+	 * Marker interface to say that this RDFTerm is part of the 
+	 * Simple implementation. Used by {@link GraphImpl} to avoid
+	 * double remapping. 
+	 * <p>
+	 * This method is package protected to avoid any third-party
+	 * subclasses.
+	 *
+	 */
+	static interface SimpleRDFTerm extends RDFTerm {		
+	}
+	
     /** Unique salt per instance, for {@link #createBlankNode(String)}
      */
     private final UUID SALT = UUID.randomUUID();

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/0a148cba/simple/src/main/java/org/apache/commons/rdf/simple/Types.java
----------------------------------------------------------------------
diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/Types.java b/simple/src/main/java/org/apache/commons/rdf/simple/Types.java
index d202b8b..3e3f640 100644
--- a/simple/src/main/java/org/apache/commons/rdf/simple/Types.java
+++ b/simple/src/main/java/org/apache/commons/rdf/simple/Types.java
@@ -27,7 +27,7 @@ import java.util.Set;
 /**
  * Types from the RDF and XML Schema vocabularies.
  */
-public final class Types implements IRI {
+public final class Types implements IRI, SimpleRDFTermFactory.SimpleRDFTerm {
 
     /**
      * <tt>http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML</tt>


Mime
View raw message