mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-09-09 12:38:01 +02:00
Use thee default set of flags by default in QueryOptions.Builder, etc
This commit is contained in:
parent
d8ac199858
commit
06f7900318
@ -25,10 +25,6 @@
|
||||
|
||||
package net.luckperms.api.query;
|
||||
|
||||
import net.luckperms.api.context.ImmutableContextSet;
|
||||
|
||||
import java.util.EnumSet;
|
||||
|
||||
/**
|
||||
* Some default {@link QueryOptions} instances.
|
||||
*/
|
||||
@ -38,7 +34,6 @@ final class DefaultQueryOptions {
|
||||
}
|
||||
|
||||
private static boolean setup = false;
|
||||
private static final EnumSet<Flag> DEFAULT_FLAGS = EnumSet.allOf(Flag.class);
|
||||
private static QueryOptions contextual;
|
||||
private static QueryOptions nonContextual;
|
||||
|
||||
@ -47,8 +42,8 @@ final class DefaultQueryOptions {
|
||||
return;
|
||||
}
|
||||
setup = true;
|
||||
contextual = QueryOptions.contextual(ImmutableContextSet.empty(), DEFAULT_FLAGS);
|
||||
nonContextual = QueryOptions.nonContextual(DEFAULT_FLAGS);
|
||||
contextual = QueryOptions.builder(QueryMode.CONTEXTUAL).build();
|
||||
nonContextual = QueryOptions.builder(QueryMode.NON_CONTEXTUAL).build();
|
||||
}
|
||||
|
||||
static QueryOptions contextual() {
|
||||
|
@ -27,6 +27,9 @@ package net.luckperms.api.query;
|
||||
|
||||
/**
|
||||
* The flags which can be set for a query.
|
||||
*
|
||||
* <p>By default (in places like new instances of {@link QueryOptions.Builder} and
|
||||
* {@link QueryOptions#defaultContextualOptions()}), all {@link Flag}s are set to true.</p>
|
||||
*/
|
||||
public enum Flag {
|
||||
|
||||
|
@ -71,7 +71,7 @@ public interface QueryOptions {
|
||||
* @return the query options
|
||||
*/
|
||||
static @NonNull QueryOptions contextual(@NonNull ContextSet context) {
|
||||
return defaultContextualOptions().toBuilder().context(context).build();
|
||||
return builder(QueryMode.CONTEXTUAL).context(context).build();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -208,6 +208,8 @@ public interface QueryOptions {
|
||||
/**
|
||||
* Sets the value of the given flag.
|
||||
*
|
||||
* <p>By default, all {@link Flag}s are set to true.</p>
|
||||
*
|
||||
* @param flag the flag
|
||||
* @param value the value to set
|
||||
* @return this builder
|
||||
@ -220,6 +222,8 @@ public interface QueryOptions {
|
||||
* <p>Note that this is a set operation, not append. Existing flags will
|
||||
* be overridden.</p>
|
||||
*
|
||||
* <p>By default, all {@link Flag}s are set to true.</p>
|
||||
*
|
||||
* @param flags the flags
|
||||
* @return this builder
|
||||
*/
|
||||
|
@ -33,13 +33,25 @@ import java.util.Set;
|
||||
final class FlagUtils {
|
||||
private FlagUtils() {}
|
||||
|
||||
private static final EnumSet<Flag> DEFAULT_FLAGS_SET = EnumSet.allOf(Flag.class);
|
||||
private static final int DEFAULT_FLAGS_SIZE = DEFAULT_FLAGS_SET.size();
|
||||
static final byte DEFAULT_FLAGS = encodeAsByte(DEFAULT_FLAGS_SET);
|
||||
|
||||
/* bitwise utility methods */
|
||||
|
||||
static boolean read(byte b, Flag setting) {
|
||||
return ((b >> setting.ordinal()) & 1) == 1;
|
||||
}
|
||||
|
||||
static byte createFlag(Set<Flag> settings) {
|
||||
static byte toByte(Set<Flag> settings) {
|
||||
// fast path for the default set of flags.
|
||||
if (settings.size() == DEFAULT_FLAGS_SIZE) {
|
||||
return DEFAULT_FLAGS;
|
||||
}
|
||||
return encodeAsByte(settings);
|
||||
}
|
||||
|
||||
private static byte encodeAsByte(Set<Flag> settings) {
|
||||
byte b = 0;
|
||||
for (Flag setting : settings) {
|
||||
b |= (1 << setting.ordinal());
|
||||
@ -47,7 +59,7 @@ final class FlagUtils {
|
||||
return b;
|
||||
}
|
||||
|
||||
static Set<Flag> createSetFromFlag(byte b) {
|
||||
static Set<Flag> toSet(byte b) {
|
||||
EnumSet<Flag> settings = EnumSet.noneOf(Flag.class);
|
||||
for (Flag setting : Flag.values()) {
|
||||
if (read(b, setting)) {
|
||||
|
@ -54,7 +54,7 @@ public class QueryOptionsBuilderImpl implements QueryOptions.Builder {
|
||||
public QueryOptionsBuilderImpl(QueryMode mode) {
|
||||
this.mode = mode;
|
||||
this.context = mode == QueryMode.CONTEXTUAL ? ImmutableContextSetImpl.EMPTY : null;
|
||||
this.flags = 0;
|
||||
this.flags = FlagUtils.DEFAULT_FLAGS;
|
||||
this.flagsSet = null;
|
||||
this.options = null;
|
||||
this.copyOptions = false;
|
||||
@ -94,13 +94,13 @@ public class QueryOptionsBuilderImpl implements QueryOptions.Builder {
|
||||
public QueryOptions.@NonNull Builder flag(@NonNull Flag flag, boolean value) {
|
||||
Objects.requireNonNull(flag, "flag");
|
||||
|
||||
// already set
|
||||
// check if already set
|
||||
if (this.flagsSet == null && FlagUtils.read(this.flags, flag) == value) {
|
||||
return this;
|
||||
}
|
||||
|
||||
if (this.flagsSet == null) {
|
||||
this.flagsSet = FlagUtils.createSetFromFlag(this.flags);
|
||||
this.flagsSet = FlagUtils.toSet(this.flags);
|
||||
}
|
||||
if (value) {
|
||||
this.flagsSet.add(flag);
|
||||
@ -146,7 +146,7 @@ public class QueryOptionsBuilderImpl implements QueryOptions.Builder {
|
||||
|
||||
@Override
|
||||
public @NonNull QueryOptions build() {
|
||||
byte flags = this.flagsSet != null ? FlagUtils.createFlag(this.flagsSet) : this.flags;
|
||||
byte flags = this.flagsSet != null ? FlagUtils.toByte(this.flagsSet) : this.flags;
|
||||
|
||||
if (this.options == null) {
|
||||
if (this.mode == QueryMode.NON_CONTEXTUAL) {
|
||||
|
@ -86,7 +86,7 @@ public class QueryOptionsImpl implements QueryOptions {
|
||||
if (this.flagsSet != null) {
|
||||
return this.flagsSet;
|
||||
}
|
||||
Set<Flag> set = ImmutableSet.copyOf(FlagUtils.createSetFromFlag(this.flags));
|
||||
Set<Flag> set = ImmutableSet.copyOf(FlagUtils.toSet(this.flags));
|
||||
this.flagsSet = set;
|
||||
return set;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user