Skip to content

Commit e79f1a5

Browse files
authored
fix(server): clean up the code (#2456)
- replace `size() == 0` with `isEmpty()` - remove unnecessary unboxing - remove unnecessary boxing - replace for loop with enhanced for loop - replace lambda with method reference
1 parent 5cb9aad commit e79f1a5

File tree

46 files changed

+101
-134
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+101
-134
lines changed

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/LoadDetectFilter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void filter(ContainerRequestContext context) {
9595

9696
public static boolean isWhiteAPI(ContainerRequestContext context) {
9797
List<PathSegment> segments = context.getUriInfo().getPathSegments();
98-
E.checkArgument(segments.size() > 0, "Invalid request uri '%s'",
98+
E.checkArgument(!segments.isEmpty(), "Invalid request uri '%s'",
9999
context.getUriInfo().getPath());
100100
String rootPath = segments.get(0).getPath();
101101
return WHITE_API_LIST.contains(rootPath);

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/server/RestServer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private HttpServer configHttpServer(URI uri, ResourceConfig rc) {
9797
}
9898

9999
Collection<NetworkListener> listeners = server.getListeners();
100-
E.checkState(listeners.size() > 0,
100+
E.checkState(!listeners.isEmpty(),
101101
"Http Server should have some listeners, but now is none");
102102
NetworkListener listener = listeners.iterator().next();
103103

hugegraph-server/hugegraph-cassandra/src/main/java/org/apache/hugegraph/backend/store/cassandra/CassandraStore.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ private static Map<String, Object> parseReplica(HugeConfig conf) {
538538

539539
private static int convertFactor(String factor) {
540540
try {
541-
return Integer.valueOf(factor);
541+
return Integer.parseInt(factor);
542542
} catch (NumberFormatException e) {
543543
throw new BackendException(
544544
"Expect int factor value for SimpleStrategy, " +

hugegraph-server/hugegraph-cassandra/src/main/java/org/apache/hugegraph/backend/store/cassandra/CassandraTables.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ public void delete(CassandraSessionPool.Session session,
447447

448448
// Let super class do delete if not deleting edge by label
449449
List<Object> idParts = this.idColumnValue(entry.id());
450-
if (idParts.size() > 1 || entry.columns().size() > 0) {
450+
if (idParts.size() > 1 || !entry.columns().isEmpty()) {
451451
super.delete(session, entry);
452452
return;
453453
}

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/auth/StandardAuthManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public HugeUser findUser(String name) {
180180
}
181181

182182
List<HugeUser> users = this.users.query(P.NAME, name, 2L);
183-
if (users.size() > 0) {
183+
if (!users.isEmpty()) {
184184
assert users.size() == 1;
185185
user = users.get(0);
186186
this.usersCache.update(username, user);

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/cache/CachedGraphTransaction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ protected Iterator<HugeEdge> queryEdgesFromBackend(Query query) {
350350
edges.add(rs.next());
351351
}
352352

353-
if (edges.size() == 0) {
353+
if (edges.isEmpty()) {
354354
this.edgesCache.update(cacheKey, Collections.emptyList());
355355
} else if (edges.size() <= MAX_CACHE_EDGES_PER_QUERY) {
356356
this.edgesCache.update(cacheKey, edges);

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/id/Id.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public char prefix() {
6767
}
6868

6969
public static IdType valueOfPrefix(String id) {
70-
E.checkArgument(id != null && id.length() > 0,
70+
E.checkArgument(id != null && !id.isEmpty(),
7171
"Invalid id '%s'", id);
7272
switch (id.charAt(0)) {
7373
case 'L':

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/Condition.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ public enum ConditionType {
5252

5353
public enum RelationType implements BiPredicate<Object, Object> {
5454

55-
EQ("==", (v1, v2) -> {
56-
return equals(v1, v2);
57-
}),
55+
EQ("==", RelationType::equals),
5856

5957
GT(">", (v1, v2) -> {
6058
return compare(v1, v2) > 0;

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/ConditionQuery.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,12 @@ public <T> T condition(Object key) {
241241
if (valuesEQ.isEmpty() && valuesIN.isEmpty()) {
242242
return null;
243243
}
244-
if (valuesEQ.size() == 1 && valuesIN.size() == 0) {
244+
if (valuesEQ.size() == 1 && valuesIN.isEmpty()) {
245245
@SuppressWarnings("unchecked")
246246
T value = (T) valuesEQ.get(0);
247247
return value;
248248
}
249-
if (valuesEQ.size() == 0 && valuesIN.size() == 1) {
249+
if (valuesEQ.isEmpty() && valuesIN.size() == 1) {
250250
@SuppressWarnings("unchecked")
251251
T value = (T) valuesIN.get(0);
252252
return value;
@@ -273,7 +273,7 @@ public <T> T condition(Object key) {
273273
}
274274
}
275275

276-
if (intersectValues.size() == 0) {
276+
if (intersectValues.isEmpty()) {
277277
return null;
278278
}
279279
E.checkState(intersectValues.size() == 1,
@@ -516,7 +516,7 @@ public boolean hasNeqCondition() {
516516

517517
public boolean matchUserpropKeys(List<Id> keys) {
518518
Set<Id> conditionKeys = this.userpropKeys();
519-
return keys.size() > 0 && conditionKeys.containsAll(keys);
519+
return !keys.isEmpty() && conditionKeys.containsAll(keys);
520520
}
521521

522522
@Override
@@ -808,7 +808,7 @@ public boolean checkRangeIndex(HugeElement element, Condition cond) {
808808
*/
809809
boolean hasRightValue = removeFieldValue(fieldValues,
810810
property.value());
811-
if (fieldValues.size() > 0) {
811+
if (!fieldValues.isEmpty()) {
812812
this.addLeftIndex(element.id(), propId, fieldValues);
813813
}
814814

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/QueryResults.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private QueryResults(Iterator<R> results) {
5959
}
6060

6161
public void setQuery(Query query) {
62-
if (this.queries.size() > 0) {
62+
if (!this.queries.isEmpty()) {
6363
this.queries.clear();
6464
}
6565
this.addQuery(query);

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/serializer/BinarySerializer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ public HugeEdge readEdge(HugeGraph graph, BackendEntry bytesEntry) {
527527
@Override
528528
public BackendEntry writeIndex(HugeIndex index) {
529529
BinaryBackendEntry entry;
530-
if (index.fieldValues() == null && index.elementIds().size() == 0) {
530+
if (index.fieldValues() == null && index.elementIds().isEmpty()) {
531531
/*
532532
* When field-values is null and elementIds size is 0, it is
533533
* meaningful for deletion of index data by index label.

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/serializer/TableSerializer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public BackendEntry writeIndex(HugeIndex index) {
308308
* When field-values is null and elementIds size is 0, it is
309309
* meaningful for deletion of index data in secondary/range index.
310310
*/
311-
if (index.fieldValues() == null && index.elementIds().size() == 0) {
311+
if (index.fieldValues() == null && index.elementIds().isEmpty()) {
312312
entry.column(HugeKeys.INDEX_LABEL_ID, index.indexLabel().longId());
313313
} else {
314314
entry.column(HugeKeys.FIELD_VALUES, index.fieldValues());

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/serializer/TextSerializer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ public HugeEdge readEdge(HugeGraph graph, BackendEntry backendEntry) {
355355
@Override
356356
public BackendEntry writeIndex(HugeIndex index) {
357357
TextBackendEntry entry = newBackendEntry(index.type(), index.id());
358-
if (index.fieldValues() == null && index.elementIds().size() == 0) {
358+
if (index.fieldValues() == null && index.elementIds().isEmpty()) {
359359
/*
360360
* When field-values is null and elementIds size is 0, it is
361361
* meaningful for deletion of index data in secondary/range index.
@@ -497,7 +497,7 @@ private Query writeQueryEdgePrefixCondition(ConditionQuery cq) {
497497
}
498498
}
499499

500-
if (condParts.size() > 0) {
500+
if (!condParts.isEmpty()) {
501501
// Conditions to id
502502
String id = EdgeId.concat(condParts.toArray(new String[0]));
503503
return new IdPrefixQuery(cq, IdGenerator.of(id));

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/store/memory/InMemoryDBTable.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ private Iterator<BackendEntry> queryByRange(ConditionQuery query) {
232232

233233
protected Map<Id, BackendEntry> queryById(Collection<Id> ids,
234234
Map<Id, BackendEntry> entries) {
235-
assert ids.size() > 0;
235+
assert !ids.isEmpty();
236236
Map<Id, BackendEntry> rs = InsertionOrderUtil.newMap();
237237

238238
for (Id id : ids) {
@@ -262,7 +262,7 @@ protected Map<Id, BackendEntry> queryByIdRange(Id start,
262262
protected Map<Id, BackendEntry> queryByFilter(
263263
Collection<Condition> conditions,
264264
Map<Id, BackendEntry> entries) {
265-
assert conditions.size() > 0;
265+
assert !conditions.isEmpty();
266266

267267
Map<Id, BackendEntry> rs = new HashMap<>();
268268

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/store/memory/InMemoryDBTables.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ protected Map<Id, BackendEntry> queryByIdRange(
181181
private Map<Id, BackendEntry> queryEdgeById(
182182
Collection<Id> ids, boolean prefix,
183183
Map<Id, BackendEntry> entries) {
184-
assert ids.size() > 0;
184+
assert !ids.isEmpty();
185185
Map<Id, BackendEntry> rs = InsertionOrderUtil.newMap();
186186

187187
for (Id id : ids) {

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/store/ram/RamTable.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,7 @@ public Iterator<HugeEdge> query(Query query) {
306306
ConditionQuery cq = cqs.get(0);
307307
return this.query(cq);
308308
}
309-
return new FlatMapperIterator<>(cqs.iterator(), cq -> {
310-
return this.query(cq);
311-
});
309+
return new FlatMapperIterator<>(cqs.iterator(), this::query);
312310
}
313311

314312
private Iterator<HugeEdge> query(ConditionQuery query) {
@@ -540,7 +538,7 @@ protected long load(Iterator<Vertex> vertices) {
540538

541539
private void addVertex(Id vertex) {
542540
Id lastId = IdGenerator.ZERO;
543-
if (this.vertices.size() > 0) {
541+
if (!this.vertices.isEmpty()) {
544542
lastId = this.vertices.get(this.vertices.size() - 1);
545543
}
546544
LOG.info("scan from hbase source {} lastId value: {} compare {} size {}",

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/tx/GraphIndexTransaction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1704,7 +1704,7 @@ public String toString() {
17041704
}
17051705

17061706
private static Query parent(Collection<Query> queries) {
1707-
if (queries.size() > 0) {
1707+
if (!queries.isEmpty()) {
17081708
// Chose the first one as origin query (any one is OK)
17091709
return queries.iterator().next();
17101710
}

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/tx/GraphTransaction.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -180,23 +180,23 @@ public boolean hasUpdate() {
180180
public boolean hasUpdate(HugeType type, Action action) {
181181
if (type.isVertex()) {
182182
if (action == Action.DELETE) {
183-
if (this.removedVertices.size() > 0) {
183+
if (!this.removedVertices.isEmpty()) {
184184
return true;
185185
}
186186
} else {
187-
if (this.addedVertices.size() > 0 ||
188-
this.updatedVertices.size() > 0) {
187+
if (!this.addedVertices.isEmpty() ||
188+
!this.updatedVertices.isEmpty()) {
189189
return true;
190190
}
191191
}
192192
} else if (type.isEdge()) {
193193
if (action == Action.DELETE) {
194-
if (this.removedEdges.size() > 0) {
194+
if (!this.removedEdges.isEmpty()) {
195195
return true;
196196
}
197197
} else {
198-
if (this.addedEdges.size() > 0 ||
199-
this.updatedEdges.size() > 0) {
198+
if (!this.addedEdges.isEmpty() ||
199+
!this.updatedEdges.isEmpty()) {
200200
return true;
201201
}
202202
}
@@ -300,16 +300,16 @@ protected final boolean removingEdgeOwner(HugeEdge edge) {
300300
@Override
301301
protected BackendMutation prepareCommit() {
302302
// Serialize and add updates into super.deletions
303-
if (this.removedVertices.size() > 0 || this.removedEdges.size() > 0) {
303+
if (!this.removedVertices.isEmpty() || !this.removedEdges.isEmpty()) {
304304
this.prepareDeletions(this.removedVertices, this.removedEdges);
305305
}
306306

307-
if (this.addedProps.size() > 0 || this.removedProps.size() > 0) {
307+
if (!this.addedProps.isEmpty() || !this.removedProps.isEmpty()) {
308308
this.prepareUpdates(this.addedProps, this.removedProps);
309309
}
310310

311311
// Serialize and add updates into super.additions
312-
if (this.addedVertices.size() > 0 || this.addedEdges.size() > 0) {
312+
if (!this.addedVertices.isEmpty() || !this.addedEdges.isEmpty()) {
313313
this.prepareAdditions(this.addedVertices, this.addedEdges);
314314
}
315315

@@ -690,7 +690,7 @@ public void removeVertex(HugeVertex vertex) {
690690
// Override vertices in local `addedVertices`
691691
this.addedVertices.remove(vertex.id());
692692
// Force load vertex to ensure all properties are loaded (refer to #2181)
693-
if (vertex.schemaLabel().indexLabels().size() > 0) {
693+
if (!vertex.schemaLabel().indexLabels().isEmpty()) {
694694
vertex.forceLoad();
695695
}
696696
// Collect the removed vertex
@@ -710,7 +710,7 @@ public Iterator<Vertex> queryAdjacentVertices(Iterator<Edge> edges) {
710710
for (Edge edge : batchEdges) {
711711
vertexIds.add(((HugeEdge) edge).otherVertex().id());
712712
}
713-
assert vertexIds.size() > 0;
713+
assert !vertexIds.isEmpty();
714714
return this.queryAdjacentVertices(vertexIds.toArray());
715715
});
716716
}

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/comm/LouvainTraverser.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ private boolean allMembersExist(Collection<Pair<Community, Set<Id>>> comms,
536536
for (Pair<Community, Set<Id>> comm : comms) {
537537
all.removeAll(comm.getRight());
538538
}
539-
if (all.size() > 0) {
539+
if (!all.isEmpty()) {
540540
LOG.warn("Lost members of last pass: {}", all);
541541
}
542542
return all.isEmpty();
@@ -649,7 +649,7 @@ public Collection<Object> showCommunity(String community) {
649649
final String C_PASS0 = labelOfPassN(0);
650650
Collection<Object> comms = Collections.singletonList(community);
651651
boolean reachPass0 = false;
652-
while (comms.size() > 0 && !reachPass0) {
652+
while (!comms.isEmpty() && !reachPass0) {
653653
Iterator<Vertex> subComms = this.vertices(comms.iterator());
654654
comms = new HashSet<>();
655655
while (subComms.hasNext()) {
@@ -703,7 +703,7 @@ public long clearPass(int pass) {
703703
if (pass < 0) {
704704
// drop edges of all pass
705705
List<String> els = this.cpassEdgeLabels();
706-
if (els.size() > 0) {
706+
if (!els.isEmpty()) {
707707
String first = els.remove(0);
708708
te = te.hasLabel(first, els.toArray(new String[0]));
709709
this.drop(te);
@@ -727,7 +727,7 @@ public long clearPass(int pass) {
727727
if (pass < 0) {
728728
// drop vertices of all pass
729729
List<String> vls = this.cpassVertexLabels();
730-
if (vls.size() > 0) {
730+
if (!vls.isEmpty()) {
731731
String first = vls.remove(0);
732732
tv = tv.hasLabel(first, vls.toArray(new String[0]));
733733
this.drop(tv);

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/comm/LpaAlgorithm.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ private String voteCommunityOfVertex(Vertex vertex, String edgeLabel,
203203
}
204204

205205
// isolated vertex
206-
if (labels.size() == 0) {
206+
if (labels.isEmpty()) {
207207
return this.labelOfVertex(vertex);
208208
}
209209

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/rank/PageRankAlgorithm.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private long initRankMap() {
196196

197197
private void contributeToAdjacentVertices(Id sourceVertexId,
198198
List<Id> adjacentVertices) {
199-
if (adjacentVertices.size() == 0) {
199+
if (adjacentVertices.isEmpty()) {
200200
return;
201201
}
202202
DoublePair sourcePair = this.vertexRankMap.get(sourceVertexId);

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/EdgeLabel.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public boolean checkLinkEqual(Id sourceLabel, Id targetLabel) {
105105
}
106106

107107
public boolean existSortKeys() {
108-
return this.sortKeys.size() > 0;
108+
return !this.sortKeys.isEmpty();
109109
}
110110

111111
public List<Id> sortKeys() {

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/builder/VertexLabelBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ private void checkNullableKeys(Action action) {
476476

477477
private void checkIdStrategy() {
478478
IdStrategy strategy = this.idStrategy;
479-
boolean hasPrimaryKey = this.primaryKeys.size() > 0;
479+
boolean hasPrimaryKey = !this.primaryKeys.isEmpty();
480480
switch (strategy) {
481481
case DEFAULT:
482482
if (hasPrimaryKey) {

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/structure/HugeElement.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public boolean hasProperty(Id key) {
252252
}
253253

254254
public boolean hasProperties() {
255-
return this.properties.size() > 0;
255+
return !this.properties.isEmpty();
256256
}
257257

258258
public int sizeOfProperties() {

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/structure/HugeVertex.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ protected List<Object> primaryValues() {
224224
}
225225

226226
public boolean existsEdges() {
227-
return this.edges.size() > 0;
227+
return !this.edges.isEmpty();
228228
}
229229

230230
public Collection<HugeEdge> getEdges() {

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/NeighborRankTraverser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ private Ranks contributeNewLayer(List<Adjacencies> adjacencies,
214214

215215
private List<Map<Id, Double>> topRanks(List<Ranks> ranks,
216216
List<Step> steps) {
217-
assert ranks.size() > 0;
217+
assert !ranks.isEmpty();
218218
List<Map<Id, Double>> results = newList(ranks.size());
219219
// The first layer is root node so skip i=0
220220
results.add(ranks.get(0));

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/optimize/HugeVertexStep.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ protected ConditionQuery constructEdgesQuery(
151151
withEdgeCond = false;
152152
} else {
153153
// Partial sysprop conditions are in sort-keys
154-
assert query.userpropKeys().size() > 0;
154+
assert !query.userpropKeys().isEmpty();
155155
}
156156
}
157157

0 commit comments

Comments
 (0)