From e68dcbb11d4616707c7781f603498078b26bb460 Mon Sep 17 00:00:00 2001 From: Ivan Mushketyk Date: Sun, 21 Aug 2016 14:17:24 +0100 Subject: [PATCH] [gelly] Add Vertex.create and Edge.create helper methods --- .../java/org/apache/flink/graph/Edge.java | 22 +++++++--- .../java/org/apache/flink/graph/Vertex.java | 23 ++++++---- .../java/org/apache/flink/graph/EdgeTest.java | 42 +++++++++++++++++++ .../org/apache/flink/graph/VertexTest.java | 41 ++++++++++++++++++ 4 files changed, 115 insertions(+), 13 deletions(-) create mode 100644 flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/EdgeTest.java create mode 100644 flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/VertexTest.java diff --git a/flink-libraries/flink-gelly/src/main/java/org/apache/flink/graph/Edge.java b/flink-libraries/flink-gelly/src/main/java/org/apache/flink/graph/Edge.java index d84badb511a92..b5de0027eb52e 100644 --- a/flink-libraries/flink-gelly/src/main/java/org/apache/flink/graph/Edge.java +++ b/flink-libraries/flink-gelly/src/main/java/org/apache/flink/graph/Edge.java @@ -19,6 +19,7 @@ package org.apache.flink.graph; import org.apache.flink.api.java.tuple.Tuple3; +import org.apache.flink.types.NullValue; /** * An Edge represents a link between two {@link Vertex vertices}, @@ -32,12 +33,21 @@ public class Edge extends Tuple3{ private static final long serialVersionUID = 1L; + + public static Edge create(K source, K target) { + return new Edge<>(source, target, NullValue.getInstance()); + } + + public static Edge create(K source, K target, V value) { + return new Edge<>(source, target, value); + } + public Edge(){} - public Edge(K src, K trg, V val) { - this.f0 = src; - this.f1 = trg; - this.f2 = val; + public Edge(K source, K target, V value) { + this.f0 = source; + this.f1 = target; + this.f2 = value; } /** @@ -49,8 +59,8 @@ public Edge reverse() { return new Edge(this.f1, this.f0, this.f2); } - public void setSource(K src) { - this.f0 = src; + public void setSource(K source) { + this.f0 = source; } public K getSource() { diff --git a/flink-libraries/flink-gelly/src/main/java/org/apache/flink/graph/Vertex.java b/flink-libraries/flink-gelly/src/main/java/org/apache/flink/graph/Vertex.java index c5eb97393ae94..5776496b01380 100644 --- a/flink-libraries/flink-gelly/src/main/java/org/apache/flink/graph/Vertex.java +++ b/flink-libraries/flink-gelly/src/main/java/org/apache/flink/graph/Vertex.java @@ -19,23 +19,32 @@ package org.apache.flink.graph; import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.types.NullValue; /** * Represents the graph's nodes. It carries an ID and a value. * For vertices with no value, use {@link org.apache.flink.types.NullValue} as the value type. * - * @param - * @param + * @param type of vertex id + * @param type of vertex value */ public class Vertex extends Tuple2 { private static final long serialVersionUID = 1L; + public static Vertex create(K id) { + return new Vertex<>(id, NullValue.getInstance()); + } + + public static Vertex create(K id, V value) { + return new Vertex<>(id, value); + } + public Vertex(){} - public Vertex(K k, V val) { - this.f0 = k; - this.f1 = val; + public Vertex(K id, V value) { + this.f0 = id; + this.f1 = value; } public K getId() { @@ -50,7 +59,7 @@ public void setId(K id) { this.f0 = id; } - public void setValue(V val) { - this.f1 = val; + public void setValue(V value) { + this.f1 = value; } } diff --git a/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/EdgeTest.java b/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/EdgeTest.java new file mode 100644 index 0000000000000..68866531bfdb3 --- /dev/null +++ b/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/EdgeTest.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.graph; + +import org.apache.flink.types.NullValue; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class EdgeTest { + @Test + public void testCreateEdgeWithNoValue() { + Edge edge = Edge.create(1, 2); + assertEquals(1, (long) edge.getSource()); + assertEquals(2, (long) edge.getTarget()); + assertEquals(NullValue.getInstance(), edge.getValue()); + } + + @Test + public void testCreateEdgeWithValue() { + Edge edge = Edge.create(1, 2, 3); + assertEquals(1, (long) edge.getSource()); + assertEquals(2, (long) edge.getTarget()); + assertEquals(3, (long) edge.getValue()); + } +} diff --git a/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/VertexTest.java b/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/VertexTest.java new file mode 100644 index 0000000000000..6f0d1f93fda93 --- /dev/null +++ b/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/VertexTest.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.graph; + +import org.apache.flink.types.NullValue; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class VertexTest { + + @Test + public void testCreateVertexWithNoValue() { + Vertex vertex = Vertex.create(1); + assertEquals(1, (long) vertex.getId()); + assertEquals(NullValue.getInstance(), vertex.getValue()); + } + + @Test + public void testCreateVertexWithValue() { + Vertex vertex = Vertex.create(1, 2); + assertEquals(1, (long) vertex.getId()); + assertEquals(2, (long) vertex.getValue()); + } +}