Use thee default set of flags by default in QueryOptions.Builder, etc

This commit is contained in:
Luck committed 2019-12-13 11:28:46 +00:00
1 parent d8ac199858
commit 06f7900318
6 files changed
+29 -15

No files matched your search

@@ -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;
}