Skip to content

Commit 0c447be

Browse files
authored
refactor: ListValue.getFirst() replaces ListValue.get(0) (#282)
1 parent 85e6399 commit 0c447be

File tree

9 files changed

+33
-9
lines changed

9 files changed

+33
-9
lines changed

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/centrality/betweenness/BetweennessCentrality.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private void forward(ComputationContext context, Vertex vertex,
132132

133133
BetweennessValue value = vertex.value();
134134
IdSet arrivedVertices = value.arrivedVertices();
135-
Id source = sequence.get(0);
135+
Id source = sequence.getFirst();
136136
// The source vertex is arriving at first time
137137
if (!arrivedVertices.contains(source)) {
138138
arrivingVertices.add(source);

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/centrality/betweenness/BetweennessMessage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ public int compareTo(Value value) {
8181
BetweennessMessage other = (BetweennessMessage) value;
8282
E.checkArgument(this.sequence.size() != 0, "Sequence can't be empty");
8383
E.checkArgument(other.sequence.size() != 0, "Sequence can't be empty");
84-
Id selfSourceId = this.sequence.get(0);
85-
Id otherSourceId = other.sequence.get(0);
84+
Id selfSourceId = this.sequence.getFirst();
85+
Id otherSourceId = other.sequence.getFirst();
8686
return selfSourceId.compareTo(otherSourceId);
8787
}
8888
}

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/community/cc/ClusteringCoefficient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void compute0(ComputationContext context, Vertex vertex) {
6464
selfId.add(vertex.id());
6565

6666
context.sendMessageToAllEdgesIf(vertex, selfId, (ids, targetId) -> {
67-
return !ids.get(0).equals(targetId);
67+
return !ids.getFirst().equals(targetId);
6868
});
6969
vertex.value(new ClusteringCoefficientValue());
7070
}

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/community/trianglecount/TriangleCount.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected Integer triangleCount(ComputationContext context, Vertex vertex,
7575
while (messages.hasNext()) {
7676
IdList idList = messages.next();
7777
assert idList.size() == 1;
78-
Id inId = idList.get(0);
78+
Id inId = idList.getFirst();
7979
if (!outNeighbors.contains(inId)) {
8080
neighbors.add(inId);
8181
}

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/rings/RingsDetection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void compute(ComputationContext context, Vertex vertex,
7070
while (messages.hasNext()) {
7171
halt = false;
7272
IdList sequence = messages.next();
73-
if (id.equals(sequence.get(0))) {
73+
if (id.equals(sequence.getFirst())) {
7474
// Use the smallest vertex record ring
7575
boolean isMin = true;
7676
for (int i = 1; i < sequence.size(); i++) {
@@ -96,7 +96,7 @@ public void compute(ComputationContext context, Vertex vertex,
9696
}
9797
}
9898
// Field ringId is smallest vertex id in path
99-
Id ringId = sequence.get(0);
99+
Id ringId = sequence.getFirst();
100100
if (!contains) {
101101
sequence.add(vertex.id());
102102
for (Edge edge : vertex.edges()) {

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/rings/filter/RingsDetectionWithFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void compute(ComputationContext context, Vertex vertex,
7777
halt = false;
7878
RingsDetectionMessage message = messages.next();
7979
IdList path = message.path();
80-
if (vertexId.equals(path.get(0))) {
80+
if (vertexId.equals(path.getFirst())) {
8181
// Use the smallest vertex record ring
8282
boolean isMin = true;
8383
for (int i = 0; i < path.size(); i++) {

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ private Edge randomSelectEdge(Edges edges) {
177177
*/
178178
private Id getSourceId(IdList path) {
179179
// the first id of path is the source id
180-
return path.get(0);
180+
return path.getFirst();
181181
}
182182

183183
/**

computer-api/src/main/java/org/apache/hugegraph/computer/core/graph/value/ListValue.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ public T get(int index) {
9090
return this.values.get(index);
9191
}
9292

93+
public T getFirst() {
94+
if (this.values.size() == 0) {
95+
throw new NoSuchElementException("The list is empty");
96+
}
97+
return this.values.get(0);
98+
}
99+
93100
public T getLast() {
94101
int index = this.values.size() - 1;
95102
if (index < 0) {

computer-test/src/main/java/org/apache/hugegraph/computer/core/graph/value/ListValueTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,23 @@ public void testGet() {
134134
});
135135
}
136136

137+
@Test
138+
public void testGetFirst() {
139+
ListValue<IntValue> value = new ListValue<>(ValueType.INT);
140+
141+
Assert.assertThrows(NoSuchElementException.class, () -> {
142+
value.getFirst();
143+
}, e -> {
144+
Assert.assertContains("The list is empty", e.getMessage());
145+
});
146+
147+
value.add(new IntValue(100));
148+
Assert.assertEquals(100, value.getFirst().value());
149+
150+
value.add(new IntValue(200));
151+
Assert.assertEquals(100, value.getFirst().value());
152+
}
153+
137154
@Test
138155
public void testGetLast() {
139156
ListValue<IntValue> value1 = new ListValue<>(ValueType.INT);

0 commit comments

Comments
 (0)