From 1d714605fadf37f4a311b0f158535e9415a1599c Mon Sep 17 00:00:00 2001 From: John Date: Thu, 2 Nov 2023 11:13:51 +0800 Subject: [PATCH] [Improve][Java] Make EdgesCollection and VerticesCollection support foreach loop (#270) --- .github/workflows/java.yml | 1 - .../graphar/edges/EdgesCollection.java | 23 ++++++++++++++++++- .../graphar/vertices/VerticesCollection.java | 23 ++++++++++++++++++- .../graphar/edges/EdgesCollectionTest.java | 15 +++++------- .../vertices/VerticesCollectionTest.java | 5 ++-- 5 files changed, 53 insertions(+), 14 deletions(-) diff --git a/.github/workflows/java.yml b/.github/workflows/java.yml index 21154a286..65d6e16be 100644 --- a/.github/workflows/java.yml +++ b/.github/workflows/java.yml @@ -14,7 +14,6 @@ on: branches: - main paths: - - 'cpp/include/**' - 'java/**' - '.github/workflows/java.yml' diff --git a/java/src/main/java/com/alibaba/graphar/edges/EdgesCollection.java b/java/src/main/java/com/alibaba/graphar/edges/EdgesCollection.java index f99e09c61..02780b7cc 100644 --- a/java/src/main/java/com/alibaba/graphar/edges/EdgesCollection.java +++ b/java/src/main/java/com/alibaba/graphar/edges/EdgesCollection.java @@ -21,9 +21,10 @@ import com.alibaba.graphar.stdcxx.StdString; import com.alibaba.graphar.types.AdjListType; import com.alibaba.graphar.util.Result; +import java.util.Iterator; /** EdgesCollection is designed for reading a collection of edges. */ -public interface EdgesCollection extends CXXPointer { +public interface EdgesCollection extends CXXPointer, Iterable { /** * Construct the collection for a range of edges. * @@ -129,4 +130,24 @@ static EdgesCollection create( /** Get the number of edges in the collection. */ long size(); + + /** Implement Iterable interface to support for-each loop. */ + default Iterator iterator() { + return new Iterator() { + EdgeIter current = begin(); + EdgeIter end = end(); + + @Override + public boolean hasNext() { + return !current.isEnd(); + } + + @Override + public Edge next() { + Edge ret = current.get(); + current.inc(); + return ret; + } + }; + } } diff --git a/java/src/main/java/com/alibaba/graphar/vertices/VerticesCollection.java b/java/src/main/java/com/alibaba/graphar/vertices/VerticesCollection.java index dea2ec2c6..c9f35e9f6 100644 --- a/java/src/main/java/com/alibaba/graphar/vertices/VerticesCollection.java +++ b/java/src/main/java/com/alibaba/graphar/vertices/VerticesCollection.java @@ -27,12 +27,13 @@ import com.alibaba.fastffi.FFITypeAlias; import com.alibaba.graphar.graphinfo.VertexInfo; import com.alibaba.graphar.stdcxx.StdString; +import java.util.Iterator; /** VerticesCollection is designed for reading a collection of vertices. */ @FFIGen @FFITypeAlias(GAR_VERTICES_COLLECTION) @CXXHead(GAR_GRAPH_H) -public interface VerticesCollection extends CXXPointer { +public interface VerticesCollection extends CXXPointer, Iterable { /** The iterator pointing to the first vertex. */ @CXXValue @@ -48,6 +49,26 @@ public interface VerticesCollection extends CXXPointer { long size(); + /** Implement Iterable interface to support for-each loop. */ + default Iterator iterator() { + return new Iterator() { + VertexIter current = begin(); + VertexIter end = end(); + + @Override + public boolean hasNext() { + return !current.eq(end); + } + + @Override + public Vertex next() { + Vertex ret = current.get(); + current.inc(); + return ret; + } + }; + } + @FFIFactory interface Factory { /** diff --git a/java/src/test/java/com/alibaba/graphar/edges/EdgesCollectionTest.java b/java/src/test/java/com/alibaba/graphar/edges/EdgesCollectionTest.java index d600c27ed..07c6f4e58 100644 --- a/java/src/test/java/com/alibaba/graphar/edges/EdgesCollectionTest.java +++ b/java/src/test/java/com/alibaba/graphar/edges/EdgesCollectionTest.java @@ -41,18 +41,18 @@ public void test1() { AdjListType.ordered_by_source, 0, 1); - EdgeIter end = edges.end(); + EdgeIter it = edges.begin(); long count = 0L; - for (EdgeIter it = edges.begin(); !it.eq(end); it.inc()) { + for (Edge edge : edges) { // access data through iterator directly System.out.print("src=" + it.source() + ", dst=" + it.destination() + " "); // access data through edge - Edge edge = it.get(); Assert.assertEquals(edge.source(), it.source()); Assert.assertEquals(edge.destination(), it.destination()); StdString creationDate = StdString.create("creationDate"); System.out.println("creationDate=" + edge.property(creationDate, creationDate).value()); count++; + it.inc(); } System.out.println("edge_count=" + count); Assert.assertEquals(count, edges.size()); @@ -69,7 +69,7 @@ public void test1() { 4); EdgeIter end1 = edges1.end(); long count1 = 0; - for (EdgeIter it = edges1.begin(); !it.eq(end1); it.inc()) { + for (Edge edge : edges1) { count1++; } System.out.println("edge_count=" + count1); @@ -83,10 +83,8 @@ public void test1() { edgeLabel.toJavaString(), dstLabel.toJavaString(), AdjListType.ordered_by_source); - EdgeIter end2 = edges2.end(); long count2 = 0; - for (EdgeIter it = edges2.begin(); !it.eq(end2); it.inc()) { - Edge edge = it.get(); + for (Edge edge : edges2) { System.out.println("src=" + edge.source() + ", dst=" + edge.destination()); count2++; } @@ -103,9 +101,8 @@ public void test1() { AdjListType.unordered_by_source, 5, 5); - EdgeIter end3 = edges3.end(); long count3 = 0; - for (EdgeIter it = edges3.begin(); !it.eq(end3); it.inc()) { + for (Edge edge : edges3) { count3++; } System.out.println("edge_count=" + count3); diff --git a/java/src/test/java/com/alibaba/graphar/vertices/VerticesCollectionTest.java b/java/src/test/java/com/alibaba/graphar/vertices/VerticesCollectionTest.java index 2bc36ef13..9edeaece0 100644 --- a/java/src/test/java/com/alibaba/graphar/vertices/VerticesCollectionTest.java +++ b/java/src/test/java/com/alibaba/graphar/vertices/VerticesCollectionTest.java @@ -40,7 +40,8 @@ public void test1() { GrapharStaticFunctions.INSTANCE.constructVerticesCollection(graphInfo, label); Assert.assertFalse(maybeVerticesCollection.hasError()); VerticesCollection vertices = maybeVerticesCollection.value(); - for (VertexIter it = vertices.begin(); !it.eq(vertices.end()); it.inc()) { + VertexIter it = vertices.begin(); + for (Vertex vertex : vertices) { // access data through iterator directly VertexIterGen itGen = (VertexIterGen) it; System.out.println( @@ -50,7 +51,6 @@ public void test1() { + ", firstName=" + it.property(property, property).value().toJavaString()); // access data through vertex - Vertex vertex = it.get(); System.out.println( vertex.id() + ", id=" @@ -66,6 +66,7 @@ public void test1() { Assert.assertEquals( it.property(property, property).value().toJavaString(), vertex.property(property, property).value().toJavaString()); + it.inc(); } } }