Skip to content

Commit

Permalink
Consolidated binarySearch calls into one method, cleaned up generics
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed Jun 19, 2023
1 parent 86095c7 commit a8d102e
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions broker/src/main/java/io/moquette/broker/subscriptions/CNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,18 @@ List<INode> allChildren() {
}

INode childOf(Token token) {
int idx = Collections.binarySearch(children, token, (Object node, Object token1) -> ((INode) node).mainNode().token.compareTo((Token) token1));
if (idx < 0)
int idx = findIndexForToken(token);
if (idx < 0) {
return null;
}
return children.get(idx);
}

private int findIndexForToken(Token token) {
final INode tempTokenNode = new INode(new CNode(token));
return Collections.binarySearch(children, tempTokenNode, (INode node, INode tokenHolder) -> node.mainNode().token.compareTo(tokenHolder.mainNode().token));
}

private boolean equalsToken(Token token) {
return token != null && this.token != null && this.token.equals(token);
}
Expand All @@ -65,7 +71,7 @@ CNode copy() {
}

public void add(INode newINode) {
int idx = Collections.binarySearch(children, newINode.mainNode().token, (Object node, Object token1) -> ((INode) node).mainNode().token.compareTo((Token) token1));
int idx = findIndexForToken(newINode.mainNode().token);
if (idx < 0) {
children.add(-1 - idx, newINode);
} else {
Expand All @@ -74,7 +80,7 @@ public void add(INode newINode) {
}

public void remove(INode node) {
int idx = Collections.binarySearch(children, node.mainNode().token, (Object node1, Object token1) -> ((INode) node1).mainNode().token.compareTo((Token) token1));
int idx = findIndexForToken(node.mainNode().token);
this.children.remove(idx);
}

Expand Down

0 comments on commit a8d102e

Please sign in to comment.