diff --git a/api/src/main/java/net/luckperms/api/query/DefaultQueryOptions.java b/api/src/main/java/net/luckperms/api/query/DefaultQueryOptions.java index d785749a1..09c2f5ccd 100644 --- a/api/src/main/java/net/luckperms/api/query/DefaultQueryOptions.java +++ b/api/src/main/java/net/luckperms/api/query/DefaultQueryOptions.java @@ -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 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() { diff --git a/api/src/main/java/net/luckperms/api/query/Flag.java b/api/src/main/java/net/luckperms/api/query/Flag.java index 2e40fdf09..fb4c49d56 100644 --- a/api/src/main/java/net/luckperms/api/query/Flag.java +++ b/api/src/main/java/net/luckperms/api/query/Flag.java @@ -27,6 +27,9 @@ package net.luckperms.api.query; /** * The flags which can be set for a query. + * + *

By default (in places like new instances of {@link QueryOptions.Builder} and + * {@link QueryOptions#defaultContextualOptions()}), all {@link Flag}s are set to true.

*/ public enum Flag { diff --git a/api/src/main/java/net/luckperms/api/query/QueryOptions.java b/api/src/main/java/net/luckperms/api/query/QueryOptions.java index 4d072766a..60c9bb675 100644 --- a/api/src/main/java/net/luckperms/api/query/QueryOptions.java +++ b/api/src/main/java/net/luckperms/api/query/QueryOptions.java @@ -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. * + *

By default, all {@link Flag}s are set to true.

+ * * @param flag the flag * @param value the value to set * @return this builder @@ -220,6 +222,8 @@ public interface QueryOptions { *

Note that this is a set operation, not append. Existing flags will * be overridden.

* + *

By default, all {@link Flag}s are set to true.

+ * * @param flags the flags * @return this builder */ diff --git a/common/src/main/java/me/lucko/luckperms/common/query/FlagUtils.java b/common/src/main/java/me/lucko/luckperms/common/query/FlagUtils.java index 93abeec8b..653885f9e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/query/FlagUtils.java +++ b/common/src/main/java/me/lucko/luckperms/common/query/FlagUtils.java @@ -33,13 +33,25 @@ import java.util.Set; final class FlagUtils { private FlagUtils() {} + private static final EnumSet 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 settings) { + static byte toByte(Set 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 settings) { byte b = 0; for (Flag setting : settings) { b |= (1 << setting.ordinal()); @@ -47,7 +59,7 @@ final class FlagUtils { return b; } - static Set createSetFromFlag(byte b) { + static Set toSet(byte b) { EnumSet settings = EnumSet.noneOf(Flag.class); for (Flag setting : Flag.values()) { if (read(b, setting)) { diff --git a/common/src/main/java/me/lucko/luckperms/common/query/QueryOptionsBuilderImpl.java b/common/src/main/java/me/lucko/luckperms/common/query/QueryOptionsBuilderImpl.java index 21cd29eb6..64d80afc1 100644 --- a/common/src/main/java/me/lucko/luckperms/common/query/QueryOptionsBuilderImpl.java +++ b/common/src/main/java/me/lucko/luckperms/common/query/QueryOptionsBuilderImpl.java @@ -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) { diff --git a/common/src/main/java/me/lucko/luckperms/common/query/QueryOptionsImpl.java b/common/src/main/java/me/lucko/luckperms/common/query/QueryOptionsImpl.java index 0310d04a5..8dbc5cd6d 100644 --- a/common/src/main/java/me/lucko/luckperms/common/query/QueryOptionsImpl.java +++ b/common/src/main/java/me/lucko/luckperms/common/query/QueryOptionsImpl.java @@ -86,7 +86,7 @@ public class QueryOptionsImpl implements QueryOptions { if (this.flagsSet != null) { return this.flagsSet; } - Set set = ImmutableSet.copyOf(FlagUtils.createSetFromFlag(this.flags)); + Set set = ImmutableSet.copyOf(FlagUtils.toSet(this.flags)); this.flagsSet = set; return set; }