Add copy constructor and remove APIs for Commands builder

It helps to change allowed commands from session.

PiperOrigin-RevId: 373302990
This commit is contained in:
jaewan 2021-05-12 06:55:42 +01:00 committed by Oliver Woodman
parent b2b6e0ecb5
commit 43da862780
2 changed files with 89 additions and 0 deletions

View File

@ -597,6 +597,11 @@ public interface Player {
flagsBuilder = new ExoFlags.Builder(); flagsBuilder = new ExoFlags.Builder();
} }
private Builder(Commands commands) {
flagsBuilder = new ExoFlags.Builder();
flagsBuilder.addAll(commands.flags);
}
/** /**
* Adds a {@link Command}. * Adds a {@link Command}.
* *
@ -646,6 +651,43 @@ public interface Player {
return this; return this;
} }
/**
* Removes a {@link Command}.
*
* @param command A {@link Command}.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder remove(@Command int command) {
flagsBuilder.remove(command);
return this;
}
/**
* Removes a {@link Command} if the provided condition is true. Does nothing otherwise.
*
* @param command A {@link Command}.
* @param condition A condition.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder removeIf(@Command int command, boolean condition) {
flagsBuilder.removeIf(command, condition);
return this;
}
/**
* Removes {@link Command commands}.
*
* @param commands The {@link Command commands} to remove.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder removeAll(@Command int... commands) {
flagsBuilder.removeAll(commands);
return this;
}
/** /**
* Builds a {@link Commands} instance. * Builds a {@link Commands} instance.
* *
@ -665,6 +707,11 @@ public interface Player {
this.flags = flags; this.flags = flags;
} }
/** Returns a {@link Builder} initialized with the values of this instance. */
public Builder buildUpon() {
return new Builder(this);
}
/** Returns whether the set of commands contains the specified {@link Command}. */ /** Returns whether the set of commands contains the specified {@link Command}. */
public boolean contains(@Command int command) { public boolean contains(@Command int command) {
return flags.contains(command); return flags.contains(command);

View File

@ -99,6 +99,48 @@ public final class ExoFlags {
return this; return this;
} }
/**
* Removes a flag.
*
* @param flag A flag.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder remove(int flag) {
checkState(!buildCalled);
flags.delete(flag);
return this;
}
/**
* Removes a flag if the provided condition is true. Does nothing otherwise.
*
* @param flag A flag.
* @param condition A condition.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder removeIf(int flag, boolean condition) {
if (condition) {
return remove(flag);
}
return this;
}
/**
* Removes flags.
*
* @param flags The flags to remove.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder removeAll(int... flags) {
for (int flag : flags) {
remove(flag);
}
return this;
}
/** /**
* Builds an {@link ExoFlags} instance. * Builds an {@link ExoFlags} instance.
* *