From 40b7a95cfac2e6eaae06d45373ee062c9f32ac78 Mon Sep 17 00:00:00 2001 From: diaohancai <550630588@qq.com> Date: Mon, 13 Nov 2023 18:25:50 +0800 Subject: [PATCH] refactor: ListValue.getFirst() replaces ListValue.get(0) --- .../betweenness/BetweennessCentrality.java | 2 +- .../betweenness/BetweennessMessage.java | 4 ++-- .../community/cc/ClusteringCoefficient.java | 2 +- .../community/trianglecount/TriangleCount.java | 2 +- .../algorithm/path/rings/RingsDetection.java | 4 ++-- .../rings/filter/RingsDetectionWithFilter.java | 2 +- .../computer/algorithm/sampling/RandomWalk.java | 2 +- .../computer/core/graph/value/ListValue.java | 7 +++++++ .../core/graph/value/ListValueTest.java | 17 +++++++++++++++++ 9 files changed, 33 insertions(+), 9 deletions(-) diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/centrality/betweenness/BetweennessCentrality.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/centrality/betweenness/BetweennessCentrality.java index 9619744a4..5b5cc2d5a 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/centrality/betweenness/BetweennessCentrality.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/centrality/betweenness/BetweennessCentrality.java @@ -132,7 +132,7 @@ private void forward(ComputationContext context, Vertex vertex, BetweennessValue value = vertex.value(); IdSet arrivedVertices = value.arrivedVertices(); - Id source = sequence.get(0); + Id source = sequence.getFirst(); // The source vertex is arriving at first time if (!arrivedVertices.contains(source)) { arrivingVertices.add(source); diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/centrality/betweenness/BetweennessMessage.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/centrality/betweenness/BetweennessMessage.java index 6d2eacee9..1522f3557 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/centrality/betweenness/BetweennessMessage.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/centrality/betweenness/BetweennessMessage.java @@ -81,8 +81,8 @@ public int compareTo(Value value) { BetweennessMessage other = (BetweennessMessage) value; E.checkArgument(this.sequence.size() != 0, "Sequence can't be empty"); E.checkArgument(other.sequence.size() != 0, "Sequence can't be empty"); - Id selfSourceId = this.sequence.get(0); - Id otherSourceId = other.sequence.get(0); + Id selfSourceId = this.sequence.getFirst(); + Id otherSourceId = other.sequence.getFirst(); return selfSourceId.compareTo(otherSourceId); } } diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/community/cc/ClusteringCoefficient.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/community/cc/ClusteringCoefficient.java index 7af33aaee..5f54dfb74 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/community/cc/ClusteringCoefficient.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/community/cc/ClusteringCoefficient.java @@ -64,7 +64,7 @@ public void compute0(ComputationContext context, Vertex vertex) { selfId.add(vertex.id()); context.sendMessageToAllEdgesIf(vertex, selfId, (ids, targetId) -> { - return !ids.get(0).equals(targetId); + return !ids.getFirst().equals(targetId); }); vertex.value(new ClusteringCoefficientValue()); } diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/community/trianglecount/TriangleCount.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/community/trianglecount/TriangleCount.java index 6212aec6d..89aa9b841 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/community/trianglecount/TriangleCount.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/community/trianglecount/TriangleCount.java @@ -75,7 +75,7 @@ protected Integer triangleCount(ComputationContext context, Vertex vertex, while (messages.hasNext()) { IdList idList = messages.next(); assert idList.size() == 1; - Id inId = idList.get(0); + Id inId = idList.getFirst(); if (!outNeighbors.contains(inId)) { neighbors.add(inId); } diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/rings/RingsDetection.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/rings/RingsDetection.java index 1ef92bac2..ba5cbc74e 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/rings/RingsDetection.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/rings/RingsDetection.java @@ -70,7 +70,7 @@ public void compute(ComputationContext context, Vertex vertex, while (messages.hasNext()) { halt = false; IdList sequence = messages.next(); - if (id.equals(sequence.get(0))) { + if (id.equals(sequence.getFirst())) { // Use the smallest vertex record ring boolean isMin = true; for (int i = 1; i < sequence.size(); i++) { @@ -96,7 +96,7 @@ public void compute(ComputationContext context, Vertex vertex, } } // Field ringId is smallest vertex id in path - Id ringId = sequence.get(0); + Id ringId = sequence.getFirst(); if (!contains) { sequence.add(vertex.id()); for (Edge edge : vertex.edges()) { diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/rings/filter/RingsDetectionWithFilter.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/rings/filter/RingsDetectionWithFilter.java index 9f65535f4..81e58a5d1 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/rings/filter/RingsDetectionWithFilter.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/rings/filter/RingsDetectionWithFilter.java @@ -77,7 +77,7 @@ public void compute(ComputationContext context, Vertex vertex, halt = false; RingsDetectionMessage message = messages.next(); IdList path = message.path(); - if (vertexId.equals(path.get(0))) { + if (vertexId.equals(path.getFirst())) { // Use the smallest vertex record ring boolean isMin = true; for (int i = 0; i < path.size(); i++) { diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java index 33d738440..2fdf2dde2 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java @@ -177,7 +177,7 @@ private Edge randomSelectEdge(Edges edges) { */ private Id getSourceId(IdList path) { // the first id of path is the source id - return path.get(0); + return path.getFirst(); } /** diff --git a/computer-api/src/main/java/org/apache/hugegraph/computer/core/graph/value/ListValue.java b/computer-api/src/main/java/org/apache/hugegraph/computer/core/graph/value/ListValue.java index 4813ec19c..c9f5d05ed 100644 --- a/computer-api/src/main/java/org/apache/hugegraph/computer/core/graph/value/ListValue.java +++ b/computer-api/src/main/java/org/apache/hugegraph/computer/core/graph/value/ListValue.java @@ -90,6 +90,13 @@ public T get(int index) { return this.values.get(index); } + public T getFirst() { + if (this.values.size() == 0) { + throw new NoSuchElementException("The list is empty"); + } + return this.values.get(0); + } + public T getLast() { int index = this.values.size() - 1; if (index < 0) { diff --git a/computer-test/src/main/java/org/apache/hugegraph/computer/core/graph/value/ListValueTest.java b/computer-test/src/main/java/org/apache/hugegraph/computer/core/graph/value/ListValueTest.java index 1f44d897e..3b5310af5 100644 --- a/computer-test/src/main/java/org/apache/hugegraph/computer/core/graph/value/ListValueTest.java +++ b/computer-test/src/main/java/org/apache/hugegraph/computer/core/graph/value/ListValueTest.java @@ -134,6 +134,23 @@ public void testGet() { }); } + @Test + public void testGetFirst() { + ListValue value = new ListValue<>(ValueType.INT); + + Assert.assertThrows(NoSuchElementException.class, () -> { + value.getFirst(); + }, e -> { + Assert.assertContains("The list is empty", e.getMessage()); + }); + + value.add(new IntValue(100)); + Assert.assertEquals(100, value.getFirst().value()); + + value.add(new IntValue(200)); + Assert.assertEquals(100, value.getFirst().value()); + } + @Test public void testGetLast() { ListValue value1 = new ListValue<>(ValueType.INT);