This is an automated email from the ASF dual-hosted git repository.
jermy pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/incubator-hugegraph-computer.git
The following commit(s) were added to refs/heads/master by this push:
new 0c447be2 refactor: ListValue.getFirst() replaces ListValue.get(0)
(#282)
0c447be2 is described below
commit 0c447be2c61a521fbe6cd5fc5f1273218ba268d3
Author: diaohancai <[email protected]>
AuthorDate: Mon Nov 13 19:44:56 2023 +0800
refactor: ListValue.getFirst() replaces ListValue.get(0) (#282)
---
.../centrality/betweenness/BetweennessCentrality.java | 2 +-
.../centrality/betweenness/BetweennessMessage.java | 4 ++--
.../algorithm/community/cc/ClusteringCoefficient.java | 2 +-
.../community/trianglecount/TriangleCount.java | 2 +-
.../computer/algorithm/path/rings/RingsDetection.java | 4 ++--
.../path/rings/filter/RingsDetectionWithFilter.java | 2 +-
.../computer/algorithm/sampling/RandomWalk.java | 2 +-
.../hugegraph/computer/core/graph/value/ListValue.java | 7 +++++++
.../computer/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 9619744a..5b5cc2d5 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 @@ public class BetweennessCentrality implements
Computation<BetweennessMessage> {
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 6d2eacee..1522f355 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 class BetweennessMessage implements
CustomizeValue<BetweennessMessage> {
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 7af33aae..5f54dfb7 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 class ClusteringCoefficient extends TriangleCount {
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 6212aec6..89aa9b84 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 @@ public class TriangleCount implements Computation<IdList> {
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 1ef92bac..ba5cbc74 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 class RingsDetection implements Computation<IdList> {
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 class RingsDetection implements Computation<IdList> {
}
}
// 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 9f65535f..81e58a5d 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 class RingsDetectionWithFilter
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 33d73844..2fdf2dde 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 @@ public class RandomWalk implements
Computation<RandomWalkMessage> {
*/
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 4813ec19..c9f5d05e 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 class ListValue<T extends Tvalue<?>> implements
Tvalue<List<Object>> {
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 1f44d897..3b5310af 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 class ListValueTest extends UnitTestBase {
});
}
+ @Test
+ public void testGetFirst() {
+ ListValue<IntValue> 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<IntValue> value1 = new ListValue<>(ValueType.INT);