Skip to content

Commit ef8c8e6

Browse files
committed
Add methods to combine a RestrictionChainElement with a Restriction class directly
1 parent 0a14959 commit ef8c8e6

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/main/java/net/kautler/command/api/restriction/RestrictionChainElement.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import net.kautler.command.restriction.RestrictionLookup;
2020

2121
import java.util.StringJoiner;
22+
import java.util.stream.Stream;
2223

2324
import static java.lang.String.format;
2425
import static java.util.Objects.requireNonNull;
@@ -35,7 +36,7 @@ public class RestrictionChainElement {
3536
/**
3637
* Constructs a new restriction chain element.
3738
*/
38-
private RestrictionChainElement() {
39+
RestrictionChainElement() {
3940
this.restriction = null;
4041
}
4142

@@ -75,6 +76,17 @@ public RestrictionChainElement and(RestrictionChainElement other) {
7576
return new AndCombination(this, other);
7677
}
7778

79+
/**
80+
* Returns a restriction chain element that combines this restriction chain element with the given restriction class
81+
* using boolean short-circuit "and" logic.
82+
*
83+
* @param other the restriction class to be combined with this restriction chain element
84+
* @return a restriction chain element that represents the boolean combination
85+
*/
86+
public RestrictionChainElement and(Class<? extends Restriction<?>> other) {
87+
return new AndCombination(this, new RestrictionChainElement(other));
88+
}
89+
7890
/**
7991
* Returns a restriction chain element that combines this restriction chain element with the given one using boolean
8092
* short-circuit "or" logic.
@@ -86,6 +98,17 @@ public RestrictionChainElement or(RestrictionChainElement other) {
8698
return new OrCombination(this, other);
8799
}
88100

101+
/**
102+
* Returns a restriction chain element that combines this restriction chain element with the given restriction class
103+
* using boolean short-circuit "or" logic.
104+
*
105+
* @param other the restriction class to be combined with this restriction chain element
106+
* @return a restriction chain element that represents the boolean combination
107+
*/
108+
public RestrictionChainElement or(Class<? extends Restriction<?>> other) {
109+
return new OrCombination(this, new RestrictionChainElement(other));
110+
}
111+
89112
/**
90113
* Returns a restriction chain element that negates this restriction chain element.
91114
*
@@ -130,7 +153,8 @@ private AndCombination(RestrictionChainElement left, RestrictionChainElement rig
130153

131154
@Override
132155
public <M> boolean isCommandAllowed(M message, RestrictionLookup<? super M> availableRestrictions) {
133-
return left.isCommandAllowed(message, availableRestrictions) && right.isCommandAllowed(message, availableRestrictions);
156+
return Stream.of(left, right)
157+
.allMatch(chainElement -> chainElement.isCommandAllowed(message, availableRestrictions));
134158
}
135159

136160
@Override
@@ -170,7 +194,8 @@ private OrCombination(RestrictionChainElement left, RestrictionChainElement righ
170194

171195
@Override
172196
public <M> boolean isCommandAllowed(M message, RestrictionLookup<? super M> availableRestrictions) {
173-
return left.isCommandAllowed(message, availableRestrictions) || right.isCommandAllowed(message, availableRestrictions);
197+
return Stream.of(left, right)
198+
.anyMatch(chainElement -> chainElement.isCommandAllowed(message, availableRestrictions));
174199
}
175200

176201
@Override

0 commit comments

Comments
 (0)