Skip to content

Commit

Permalink
Converted returned null and null checks to Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed Jun 19, 2023
1 parent a8d102e commit c24d92b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ List<INode> allChildren() {
return new ArrayList<>(this.children);
}

INode childOf(Token token) {
Optional<INode> childOf(Token token) {
int idx = findIndexForToken(token);
if (idx < 0) {
return null;
return Optional.empty();
}
return children.get(idx);
return Optional.of(children.get(idx));
}

private int findIndexForToken(Token token) {
Expand Down
42 changes: 22 additions & 20 deletions broker/src/main/java/io/moquette/broker/subscriptions/CTrie.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ Optional<CNode> lookup(Topic topic) {
INode inode = this.root;
Token token = topic.headToken();
while (!topic.isEmpty()) {
INode child = inode.mainNode().childOf(token);
if (child == null) {
Optional<INode> child = inode.mainNode().childOf(token);
if (child.isEmpty()) {
break;
}
topic = topic.exceptHeadToken();
inode = inode.mainNode().childOf(token);
inode = child.get();
token = topic.headToken();
}
if (inode == null || !topic.isEmpty()) {
Expand Down Expand Up @@ -85,20 +85,20 @@ private Set<Subscription> recursiveMatch(Topic topic, INode inode) {

// We should only consider the maximum three children children of
// type #, + or exact match
INode subInode = cnode.childOf(Token.MULTI);
if (subInode != null) {
subscriptions.addAll(recursiveMatch(remainingTopic, subInode));
Optional<INode> subInode = cnode.childOf(Token.MULTI);
if (subInode.isPresent()) {
subscriptions.addAll(recursiveMatch(remainingTopic, subInode.get()));
}
subInode = cnode.childOf(Token.SINGLE);
if (subInode != null) {
subscriptions.addAll(recursiveMatch(remainingTopic, subInode));
if (subInode.isPresent()) {
subscriptions.addAll(recursiveMatch(remainingTopic, subInode.get()));
}
if (remainingTopic.isEmpty()) {
subscriptions.addAll(cnode.subscriptions);
} else {
subInode = cnode.childOf(remainingTopic.headToken());
if (subInode != null) {
subscriptions.addAll(recursiveMatch(remainingTopic, subInode));
if (subInode.isPresent()) {
subscriptions.addAll(recursiveMatch(remainingTopic, subInode.get()));
}
}
return subscriptions;
Expand All @@ -115,10 +115,10 @@ private Action insert(Topic topic, final INode inode, Subscription newSubscripti
final Token token = topic.headToken();
final CNode cnode = inode.mainNode();
if (!topic.isEmpty()) {
INode nextInode = cnode.childOf(token);
if (nextInode != null) {
Optional<INode> nextInode = cnode.childOf(token);
if (nextInode.isPresent()) {
Topic remainingTopic = topic.exceptHeadToken();
return insert(remainingTopic, nextInode, newSubscription);
return insert(remainingTopic, nextInode.get(), newSubscription);
}
}
if (topic.isEmpty()) {
Expand All @@ -130,21 +130,23 @@ private Action insert(Topic topic, final INode inode, Subscription newSubscripti

private Action insertSubscription(INode inode, CNode cnode, Subscription newSubscription) {
final CNode updatedCnode;
if (cnode instanceof TNode)
if (cnode instanceof TNode) {
updatedCnode = new CNode(cnode.getToken());
else
} else {
updatedCnode = cnode.copy();
}
updatedCnode.addSubscription(newSubscription);
return inode.compareAndSet(cnode, updatedCnode) ? Action.OK : Action.REPEAT;
}

private Action createNodeAndInsertSubscription(Topic topic, INode inode, CNode cnode, Subscription newSubscription) {
final INode newInode = createPathRec(topic, newSubscription);
final CNode updatedCnode;
if (cnode instanceof TNode)
if (cnode instanceof TNode) {
updatedCnode = new CNode(cnode.getToken());
else
} else {
updatedCnode = cnode.copy();
}
updatedCnode.add(newInode);

return inode.compareAndSet(cnode, updatedCnode) ? Action.OK : Action.REPEAT;
Expand Down Expand Up @@ -180,10 +182,10 @@ private Action remove(String clientId, Topic topic, INode inode, INode iParent)
Token token = topic.headToken();
final CNode cnode = inode.mainNode();
if (!topic.isEmpty()) {
INode nextInode = cnode.childOf(token);
if (nextInode != null) {
Optional<INode> nextInode = cnode.childOf(token);
if (nextInode.isPresent()) {
Topic remainingTopic = topic.exceptHeadToken();
return remove(clientId, remainingTopic, nextInode, inode);
return remove(clientId, remainingTopic, nextInode.get(), inode);
}
}
if (cnode instanceof TNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
*/
package io.moquette.broker.subscriptions;

import java.util.Optional;

class TNode extends CNode {

public TNode(Token token) {
super(token);
}

@Override
INode childOf(Token token) {
Optional<INode> childOf(Token token) {
throw new IllegalStateException("Can't be invoked on TNode");
}

Expand Down

0 comments on commit c24d92b

Please sign in to comment.