commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [1/4] incubator-commonsrdf git commit: COMMONSRDF-12 Graph.iterate() - don't extend Iterable
Date Mon, 13 Apr 2015 09:02:08 GMT
Repository: incubator-commonsrdf
Updated Branches:
  refs/heads/master 4da53c0fd -> 605a861b6


COMMONSRDF-12  Graph.iterate() - don't extend Iterable


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

Branch: refs/heads/master
Commit: 73dba32067f288832879722f59bf6d1c285a5f15
Parents: 149bdaa
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Sat Apr 11 01:24:30 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  | 45 +++++++++++++-------
 .../commons/rdf/api/AbstractGraphTest.java      | 14 +++---
 2 files changed, 36 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/73dba320/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 1ca7380..3b0bbd6 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.ConcurrentModificationException;
 import java.util.Iterator;
 import java.util.stream.Stream;
 
@@ -26,7 +27,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,Iterable<Triple> {
+public interface Graph extends AutoCloseable {
 
     /**
      * Add a triple to the graph, possibly mapping any of the components of the
@@ -149,24 +150,38 @@ public interface Graph extends AutoCloseable,Iterable<Triple>
{
                                         RDFTerm object);
 
     /**
-     * Get an iterator for all triples contained by the graph.
+     * Get an Iterable for iterating over all triples in the graph.
      * <p>
-     * The iteration SHOULD NOT contain any duplicate triples, as determined by
-     * the equals method for each {@link Triple}.
+     * This method is meant to be used with a Java for-each loop, e.g.: 
+     * <code>
+     *  for (Triple t : GraphUtil.iterate(graph)) {
+     *      System.out.println(t);
+     *  }
+     * </code>
+     * The behaviour of the iterator is not specified if {@link #add(Triple)},
+     * {@link #remove(Triple)} or {@link #clear()}, are called on the
+     * {@link Graph} before it terminates. It is undefined if the returned
+     * {@link Iterator} supports the {@link Iterator#remove()} method.
      * <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.
+     * Implementations may throw {@link ConcurrentModificationException} from
+     * Iterator methods if they detect a concurrency conflict while the Iterator
+     * is active.
      * <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
+     * The {@link Iterable#iterator()} must only be called once, that is the
+     * Iterable must only be iterated over once. A {@link IllegalStateException}
+     * may be thrown on attempt to reuse the Iterable.
+     *
+     * @return A {@link Iterable} that returns {@link Iterator} over all of the
+     *         triples in the graph
+     * @throws IllegalStateException
+     *             if the {@link Iterable} has been reused
+     * @throws ConcurrentModificationException
+     *             if a concurrency conflict occurs while the Iterator is
+     *             active.
      */
     @SuppressWarnings("unchecked")
-    @Override
-    default Iterator<Triple> iterator() {
-        return (Iterator<Triple>) getTriples().iterator();
+    default Iterable<Triple> iterate() 
+            throws ConcurrentModificationException, IllegalStateException {
+        return ((Stream<Triple>)getTriples())::iterator;
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/73dba320/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 15b0544..3405ab8 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,9 +17,7 @@
  */
 package org.apache.commons.rdf.api;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
 
 import java.util.ArrayList;
 import java.util.Iterator;
@@ -145,7 +143,7 @@ public abstract class AbstractGraphTest {
         Assume.assumeTrue(graph.size() > 0);
 
         List<Triple> triples = new ArrayList<>();
-        for (Triple t : graph) {
+        for (Triple t : graph.iterate()) {
             triples.add(t);
         }
         assertEquals(graph.size(), (long)triples.size());
@@ -154,7 +152,7 @@ public abstract class AbstractGraphTest {
         }
 
         // aborted iteration
-        Iterator<Triple> it = graph.iterator();
+        Iterator<Triple> it = graph.iterate().iterator();
 
         assertTrue(it.hasNext());
         it.next();
@@ -162,13 +160,13 @@ public abstract class AbstractGraphTest {
         // second iteration - should start from fresh and
         // get the same count
         long count = 0;
-        for (Triple t : graph) {
+        Iterable<Triple> iterable = graph.iterate();
+        for (Triple t : iterable) {
             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