Skip to content

Commit

Permalink
fix(server): clean up the code (#2456)
Browse files Browse the repository at this point in the history
- replace `size() == 0` with `isEmpty()`
- remove unnecessary unboxing
- remove unnecessary boxing
- replace for loop with enhanced for loop
- replace lambda with method reference
  • Loading branch information
msgui authored Feb 22, 2024
1 parent 5cb9aad commit e79f1a5
Show file tree
Hide file tree
Showing 46 changed files with 101 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void filter(ContainerRequestContext context) {

public static boolean isWhiteAPI(ContainerRequestContext context) {
List<PathSegment> segments = context.getUriInfo().getPathSegments();
E.checkArgument(segments.size() > 0, "Invalid request uri '%s'",
E.checkArgument(!segments.isEmpty(), "Invalid request uri '%s'",
context.getUriInfo().getPath());
String rootPath = segments.get(0).getPath();
return WHITE_API_LIST.contains(rootPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private HttpServer configHttpServer(URI uri, ResourceConfig rc) {
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ private static Map<String, Object> parseReplica(HugeConfig conf) {

private static int convertFactor(String factor) {
try {
return Integer.valueOf(factor);
return Integer.parseInt(factor);
} catch (NumberFormatException e) {
throw new BackendException(
"Expect int factor value for SimpleStrategy, " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ public void delete(CassandraSessionPool.Session session,

// Let super class do delete if not deleting edge by label
List<Object> idParts = this.idColumnValue(entry.id());
if (idParts.size() > 1 || entry.columns().size() > 0) {
if (idParts.size() > 1 || !entry.columns().isEmpty()) {
super.delete(session, entry);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public HugeUser findUser(String name) {
}

List<HugeUser> users = this.users.query(P.NAME, name, 2L);
if (users.size() > 0) {
if (!users.isEmpty()) {
assert users.size() == 1;
user = users.get(0);
this.usersCache.update(username, user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ protected Iterator<HugeEdge> queryEdgesFromBackend(Query query) {
edges.add(rs.next());
}

if (edges.size() == 0) {
if (edges.isEmpty()) {
this.edgesCache.update(cacheKey, Collections.emptyList());
} else if (edges.size() <= MAX_CACHE_EDGES_PER_QUERY) {
this.edgesCache.update(cacheKey, edges);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public char prefix() {
}

public static IdType valueOfPrefix(String id) {
E.checkArgument(id != null && id.length() > 0,
E.checkArgument(id != null && !id.isEmpty(),
"Invalid id '%s'", id);
switch (id.charAt(0)) {
case 'L':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ public enum ConditionType {

public enum RelationType implements BiPredicate<Object, Object> {

EQ("==", (v1, v2) -> {
return equals(v1, v2);
}),
EQ("==", RelationType::equals),

GT(">", (v1, v2) -> {
return compare(v1, v2) > 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,12 @@ public <T> T condition(Object key) {
if (valuesEQ.isEmpty() && valuesIN.isEmpty()) {
return null;
}
if (valuesEQ.size() == 1 && valuesIN.size() == 0) {
if (valuesEQ.size() == 1 && valuesIN.isEmpty()) {
@SuppressWarnings("unchecked")
T value = (T) valuesEQ.get(0);
return value;
}
if (valuesEQ.size() == 0 && valuesIN.size() == 1) {
if (valuesEQ.isEmpty() && valuesIN.size() == 1) {
@SuppressWarnings("unchecked")
T value = (T) valuesIN.get(0);
return value;
Expand All @@ -273,7 +273,7 @@ public <T> T condition(Object key) {
}
}

if (intersectValues.size() == 0) {
if (intersectValues.isEmpty()) {
return null;
}
E.checkState(intersectValues.size() == 1,
Expand Down Expand Up @@ -516,7 +516,7 @@ public boolean hasNeqCondition() {

public boolean matchUserpropKeys(List<Id> keys) {
Set<Id> conditionKeys = this.userpropKeys();
return keys.size() > 0 && conditionKeys.containsAll(keys);
return !keys.isEmpty() && conditionKeys.containsAll(keys);
}

@Override
Expand Down Expand Up @@ -808,7 +808,7 @@ public boolean checkRangeIndex(HugeElement element, Condition cond) {
*/
boolean hasRightValue = removeFieldValue(fieldValues,
property.value());
if (fieldValues.size() > 0) {
if (!fieldValues.isEmpty()) {
this.addLeftIndex(element.id(), propId, fieldValues);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private QueryResults(Iterator<R> results) {
}

public void setQuery(Query query) {
if (this.queries.size() > 0) {
if (!this.queries.isEmpty()) {
this.queries.clear();
}
this.addQuery(query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ public HugeEdge readEdge(HugeGraph graph, BackendEntry bytesEntry) {
@Override
public BackendEntry writeIndex(HugeIndex index) {
BinaryBackendEntry entry;
if (index.fieldValues() == null && index.elementIds().size() == 0) {
if (index.fieldValues() == null && index.elementIds().isEmpty()) {
/*
* When field-values is null and elementIds size is 0, it is
* meaningful for deletion of index data by index label.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public BackendEntry writeIndex(HugeIndex index) {
* When field-values is null and elementIds size is 0, it is
* meaningful for deletion of index data in secondary/range index.
*/
if (index.fieldValues() == null && index.elementIds().size() == 0) {
if (index.fieldValues() == null && index.elementIds().isEmpty()) {
entry.column(HugeKeys.INDEX_LABEL_ID, index.indexLabel().longId());
} else {
entry.column(HugeKeys.FIELD_VALUES, index.fieldValues());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public HugeEdge readEdge(HugeGraph graph, BackendEntry backendEntry) {
@Override
public BackendEntry writeIndex(HugeIndex index) {
TextBackendEntry entry = newBackendEntry(index.type(), index.id());
if (index.fieldValues() == null && index.elementIds().size() == 0) {
if (index.fieldValues() == null && index.elementIds().isEmpty()) {
/*
* When field-values is null and elementIds size is 0, it is
* meaningful for deletion of index data in secondary/range index.
Expand Down Expand Up @@ -497,7 +497,7 @@ private Query writeQueryEdgePrefixCondition(ConditionQuery cq) {
}
}

if (condParts.size() > 0) {
if (!condParts.isEmpty()) {
// Conditions to id
String id = EdgeId.concat(condParts.toArray(new String[0]));
return new IdPrefixQuery(cq, IdGenerator.of(id));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ private Iterator<BackendEntry> queryByRange(ConditionQuery query) {

protected Map<Id, BackendEntry> queryById(Collection<Id> ids,
Map<Id, BackendEntry> entries) {
assert ids.size() > 0;
assert !ids.isEmpty();
Map<Id, BackendEntry> rs = InsertionOrderUtil.newMap();

for (Id id : ids) {
Expand Down Expand Up @@ -262,7 +262,7 @@ protected Map<Id, BackendEntry> queryByIdRange(Id start,
protected Map<Id, BackendEntry> queryByFilter(
Collection<Condition> conditions,
Map<Id, BackendEntry> entries) {
assert conditions.size() > 0;
assert !conditions.isEmpty();

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ protected Map<Id, BackendEntry> queryByIdRange(
private Map<Id, BackendEntry> queryEdgeById(
Collection<Id> ids, boolean prefix,
Map<Id, BackendEntry> entries) {
assert ids.size() > 0;
assert !ids.isEmpty();
Map<Id, BackendEntry> rs = InsertionOrderUtil.newMap();

for (Id id : ids) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,7 @@ public Iterator<HugeEdge> query(Query query) {
ConditionQuery cq = cqs.get(0);
return this.query(cq);
}
return new FlatMapperIterator<>(cqs.iterator(), cq -> {
return this.query(cq);
});
return new FlatMapperIterator<>(cqs.iterator(), this::query);
}

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

private void addVertex(Id vertex) {
Id lastId = IdGenerator.ZERO;
if (this.vertices.size() > 0) {
if (!this.vertices.isEmpty()) {
lastId = this.vertices.get(this.vertices.size() - 1);
}
LOG.info("scan from hbase source {} lastId value: {} compare {} size {}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1704,7 +1704,7 @@ public String toString() {
}

private static Query parent(Collection<Query> queries) {
if (queries.size() > 0) {
if (!queries.isEmpty()) {
// Chose the first one as origin query (any one is OK)
return queries.iterator().next();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,23 +180,23 @@ public boolean hasUpdate() {
public boolean hasUpdate(HugeType type, Action action) {
if (type.isVertex()) {
if (action == Action.DELETE) {
if (this.removedVertices.size() > 0) {
if (!this.removedVertices.isEmpty()) {
return true;
}
} else {
if (this.addedVertices.size() > 0 ||
this.updatedVertices.size() > 0) {
if (!this.addedVertices.isEmpty() ||
!this.updatedVertices.isEmpty()) {
return true;
}
}
} else if (type.isEdge()) {
if (action == Action.DELETE) {
if (this.removedEdges.size() > 0) {
if (!this.removedEdges.isEmpty()) {
return true;
}
} else {
if (this.addedEdges.size() > 0 ||
this.updatedEdges.size() > 0) {
if (!this.addedEdges.isEmpty() ||
!this.updatedEdges.isEmpty()) {
return true;
}
}
Expand Down Expand Up @@ -300,16 +300,16 @@ protected final boolean removingEdgeOwner(HugeEdge edge) {
@Override
protected BackendMutation prepareCommit() {
// Serialize and add updates into super.deletions
if (this.removedVertices.size() > 0 || this.removedEdges.size() > 0) {
if (!this.removedVertices.isEmpty() || !this.removedEdges.isEmpty()) {
this.prepareDeletions(this.removedVertices, this.removedEdges);
}

if (this.addedProps.size() > 0 || this.removedProps.size() > 0) {
if (!this.addedProps.isEmpty() || !this.removedProps.isEmpty()) {
this.prepareUpdates(this.addedProps, this.removedProps);
}

// Serialize and add updates into super.additions
if (this.addedVertices.size() > 0 || this.addedEdges.size() > 0) {
if (!this.addedVertices.isEmpty() || !this.addedEdges.isEmpty()) {
this.prepareAdditions(this.addedVertices, this.addedEdges);
}

Expand Down Expand Up @@ -690,7 +690,7 @@ public void removeVertex(HugeVertex vertex) {
// Override vertices in local `addedVertices`
this.addedVertices.remove(vertex.id());
// Force load vertex to ensure all properties are loaded (refer to #2181)
if (vertex.schemaLabel().indexLabels().size() > 0) {
if (!vertex.schemaLabel().indexLabels().isEmpty()) {
vertex.forceLoad();
}
// Collect the removed vertex
Expand All @@ -710,7 +710,7 @@ public Iterator<Vertex> queryAdjacentVertices(Iterator<Edge> edges) {
for (Edge edge : batchEdges) {
vertexIds.add(((HugeEdge) edge).otherVertex().id());
}
assert vertexIds.size() > 0;
assert !vertexIds.isEmpty();
return this.queryAdjacentVertices(vertexIds.toArray());
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ private boolean allMembersExist(Collection<Pair<Community, Set<Id>>> comms,
for (Pair<Community, Set<Id>> comm : comms) {
all.removeAll(comm.getRight());
}
if (all.size() > 0) {
if (!all.isEmpty()) {
LOG.warn("Lost members of last pass: {}", all);
}
return all.isEmpty();
Expand Down Expand Up @@ -649,7 +649,7 @@ public Collection<Object> showCommunity(String community) {
final String C_PASS0 = labelOfPassN(0);
Collection<Object> comms = Collections.singletonList(community);
boolean reachPass0 = false;
while (comms.size() > 0 && !reachPass0) {
while (!comms.isEmpty() && !reachPass0) {
Iterator<Vertex> subComms = this.vertices(comms.iterator());
comms = new HashSet<>();
while (subComms.hasNext()) {
Expand Down Expand Up @@ -703,7 +703,7 @@ public long clearPass(int pass) {
if (pass < 0) {
// drop edges of all pass
List<String> els = this.cpassEdgeLabels();
if (els.size() > 0) {
if (!els.isEmpty()) {
String first = els.remove(0);
te = te.hasLabel(first, els.toArray(new String[0]));
this.drop(te);
Expand All @@ -727,7 +727,7 @@ public long clearPass(int pass) {
if (pass < 0) {
// drop vertices of all pass
List<String> vls = this.cpassVertexLabels();
if (vls.size() > 0) {
if (!vls.isEmpty()) {
String first = vls.remove(0);
tv = tv.hasLabel(first, vls.toArray(new String[0]));
this.drop(tv);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private String voteCommunityOfVertex(Vertex vertex, String edgeLabel,
}

// isolated vertex
if (labels.size() == 0) {
if (labels.isEmpty()) {
return this.labelOfVertex(vertex);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private long initRankMap() {

private void contributeToAdjacentVertices(Id sourceVertexId,
List<Id> adjacentVertices) {
if (adjacentVertices.size() == 0) {
if (adjacentVertices.isEmpty()) {
return;
}
DoublePair sourcePair = this.vertexRankMap.get(sourceVertexId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public boolean checkLinkEqual(Id sourceLabel, Id targetLabel) {
}

public boolean existSortKeys() {
return this.sortKeys.size() > 0;
return !this.sortKeys.isEmpty();
}

public List<Id> sortKeys() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ private void checkNullableKeys(Action action) {

private void checkIdStrategy() {
IdStrategy strategy = this.idStrategy;
boolean hasPrimaryKey = this.primaryKeys.size() > 0;
boolean hasPrimaryKey = !this.primaryKeys.isEmpty();
switch (strategy) {
case DEFAULT:
if (hasPrimaryKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public boolean hasProperty(Id key) {
}

public boolean hasProperties() {
return this.properties.size() > 0;
return !this.properties.isEmpty();
}

public int sizeOfProperties() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ protected List<Object> primaryValues() {
}

public boolean existsEdges() {
return this.edges.size() > 0;
return !this.edges.isEmpty();
}

public Collection<HugeEdge> getEdges() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private Ranks contributeNewLayer(List<Adjacencies> adjacencies,

private List<Map<Id, Double>> topRanks(List<Ranks> ranks,
List<Step> steps) {
assert ranks.size() > 0;
assert !ranks.isEmpty();
List<Map<Id, Double>> results = newList(ranks.size());
// The first layer is root node so skip i=0
results.add(ranks.get(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ protected ConditionQuery constructEdgesQuery(
withEdgeCond = false;
} else {
// Partial sysprop conditions are in sort-keys
assert query.userpropKeys().size() > 0;
assert !query.userpropKeys().isEmpty();
}
}

Expand Down
Loading

0 comments on commit e79f1a5

Please sign in to comment.