commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [2/4] incubator-commonsrdf git commit: COMMONSRDF-12 Graph implements Iterable interface
Date Mon, 13 Apr 2015 09:02:09 GMT
COMMONSRDF-12 Graph implements Iterable interface

..with a default method that simply calls getTriples().iterator

This is mainly to be useful for classical for-each loops in Java.


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

Branch: refs/heads/master
Commit: 149bdaa7d355372c9b9312547babd803643102d8
Parents: 4da53c0
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Fri Apr 10 10:39:10 2015 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Mon Apr 13 09:58:09 2015 +0100

----------------------------------------------------------------------
 .../java/org/apache/commons/rdf/api/Graph.java  | 25 +++++++++++-
 .../commons/rdf/api/AbstractGraphTest.java      | 43 ++++++++++++++++++--
 2 files changed, 63 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/149bdaa7/api/src/main/java/org/apache/commons/rdf/api/Graph.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/org/apache/commons/rdf/api/Graph.java b/api/src/main/java/org/apache/commons/rdf/api/Graph.java
index a1e8cf1..1ca7380 100644
--- a/api/src/main/java/org/apache/commons/rdf/api/Graph.java
+++ b/api/src/main/java/org/apache/commons/rdf/api/Graph.java
@@ -17,6 +17,7 @@
  */
 package org.apache.commons.rdf.api;
 
+import java.util.Iterator;
 import java.util.stream.Stream;
 
 /**
@@ -25,7 +26,7 @@ import java.util.stream.Stream;
  * href="http://www.w3.org/TR/rdf11-concepts/" >RDF-1.1 Concepts and Abstract
  * Syntax</a>, a W3C Recommendation published on 25 February 2014.
  */
-public interface Graph extends AutoCloseable {
+public interface Graph extends AutoCloseable,Iterable<Triple> {
 
     /**
      * Add a triple to the graph, possibly mapping any of the components of the
@@ -146,4 +147,26 @@ public interface Graph extends AutoCloseable {
      */
     Stream<? extends Triple> getTriples(BlankNodeOrIRI subject, IRI predicate,
                                         RDFTerm object);
+
+    /**
+     * Get an iterator for all triples contained by the graph.
+     * <p>
+     * The iteration SHOULD NOT contain any duplicate triples, as determined by
+     * the equals method for each {@link Triple}.
+     * <p>
+     * The behaviour of the iterator is not specified if add, remove, or clear,
+     * are called on the Graph before it terminates. It is undefined if the
+     * returned Iterator supports the remove method.
+     * <p>
+     * Implementations may throw ConcurrentModificationException from Iterator
+     * methods if they detect a concurrency conflict while the Iterator is
+     * active.
+     *
+     * @return A {@link Iterator} over all of the triples in the graph
+     */
+    @SuppressWarnings("unchecked")
+    @Override
+    default Iterator<Triple> iterator() {
+        return (Iterator<Triple>) getTriples().iterator();
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/149bdaa7/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java
----------------------------------------------------------------------
diff --git a/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java b/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java
index d1a4995..15b0544 100644
--- a/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java
+++ b/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java
@@ -17,13 +17,18 @@
  */
 package org.apache.commons.rdf.api;
 
-import org.junit.Assume;
-import org.junit.Before;
-import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Optional;
 
-import static org.junit.Assert.*;
+import org.junit.Assume;
+import org.junit.Before;
+import org.junit.Test;
 
 /**
  * Test Graph implementation
@@ -135,6 +140,36 @@ public abstract class AbstractGraphTest {
     }
 
     @Test
+    public void iterable() throws Exception {
+
+        Assume.assumeTrue(graph.size() > 0);
+
+        List<Triple> triples = new ArrayList<>();
+        for (Triple t : graph) {
+            triples.add(t);
+        }
+        assertEquals(graph.size(), (long)triples.size());
+        if (bobNameTriple != null) {
+            assertTrue(triples.contains(bobNameTriple));
+        }
+
+        // aborted iteration
+        Iterator<Triple> it = graph.iterator();
+
+        assertTrue(it.hasNext());
+        it.next();
+
+        // second iteration - should start from fresh and
+        // get the same count
+        long count = 0;
+        for (Triple t : graph) {
+            count++;
+        }
+        assertEquals(graph.size(), count);
+
+    }
+
+    @Test
     public void contains() throws Exception {
         assertFalse(graph.contains(bob, knows, alice)); // or so he claims..
 


Mime
View raw message