| 1 | package net.minecraft.world.flag;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.HashCommon;
|
| 4 | import java.util.Arrays;
|
| 5 | import java.util.Collection;
|
| 6 | import org.jspecify.annotations.Nullable;
|
| 7 |
|
| 8 | public final class FeatureFlagSet {
|
| 9 | private static final FeatureFlagSet EMPTY = new FeatureFlagSet(null, 0L);
|
| 10 | public static final int MAX_CONTAINER_SIZE = 64;
|
| 11 | private final @Nullable FeatureFlagUniverse universe;
|
| 12 | private final long mask;
|
| 13 |
|
| 14 | private FeatureFlagSet(final @Nullable FeatureFlagUniverse universe, final long mask) {
|
| 15 | this.universe = universe;
|
| 16 | this.mask = mask;
|
| 17 | }
|
| 18 |
|
| 19 | static FeatureFlagSet create(final FeatureFlagUniverse universe, final Collection<FeatureFlag> flags) {
|
| 20 | if (flags.isEmpty()) {
|
| 21 | return EMPTY;
|
| 22 | }
|
| 23 |
|
| 24 | long mask = computeMask(universe, 0L, flags);
|
| 25 | return new FeatureFlagSet(universe, mask);
|
| 26 | }
|
| 27 |
|
| 28 | public static FeatureFlagSet of() {
|
| 29 | return EMPTY;
|
| 30 | }
|
| 31 |
|
| 32 | public static FeatureFlagSet of(final FeatureFlag flag) {
|
| 33 | return new FeatureFlagSet(flag.universe, flag.mask);
|
| 34 | }
|
| 35 |
|
| 36 | public static FeatureFlagSet of(final FeatureFlag flag, final FeatureFlag... flags) {
|
| 37 | long mask = flags.length == 0 ? flag.mask : computeMask(flag.universe, flag.mask, Arrays.asList(flags));
|
| 38 | return new FeatureFlagSet(flag.universe, mask);
|
| 39 | }
|
| 40 |
|
| 41 | private static long computeMask(final FeatureFlagUniverse universe, long mask, final Iterable<FeatureFlag> flags) {
|
| 42 | for (FeatureFlag f : flags) {
|
| 43 | if (universe != f.universe) {
|
| 44 | throw new IllegalStateException("Mismatched feature universe, expected '" + universe + "', but got '" + f.universe + "'");
|
| 45 | }
|
| 46 |
|
| 47 | mask |= f.mask;
|
| 48 | }
|
| 49 |
|
| 50 | return mask;
|
| 51 | }
|
| 52 |
|
| 53 | public boolean contains(final FeatureFlag flag) {
|
| 54 | return this.universe != flag.universe ? false : (this.mask & flag.mask) != 0L;
|
| 55 | }
|
| 56 |
|
| 57 | public boolean isEmpty() {
|
| 58 | return this.equals(EMPTY);
|
| 59 | }
|
| 60 |
|
| 61 | public boolean isSubsetOf(final FeatureFlagSet set) {
|
| 62 | if (this.universe == null) {
|
| 63 | return true;
|
| 64 | } else {
|
| 65 | return this.universe != set.universe ? false : (this.mask & ~set.mask) == 0L;
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | public boolean intersects(final FeatureFlagSet set) {
|
| 70 | return this.universe != null && set.universe != null && this.universe == set.universe ? (this.mask & set.mask) != 0L : false;
|
| 71 | }
|
| 72 |
|
| 73 | public FeatureFlagSet join(final FeatureFlagSet other) {
|
| 74 | if (this.universe == null) {
|
| 75 | return other;
|
| 76 | } else if (other.universe == null) {
|
| 77 | return this;
|
| 78 | } else if (this.universe != other.universe) {
|
| 79 | throw new IllegalArgumentException("Mismatched set elements: '" + this.universe + "' != '" + other.universe + "'");
|
| 80 | } else {
|
| 81 | return new FeatureFlagSet(this.universe, this.mask | other.mask);
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | public FeatureFlagSet subtract(final FeatureFlagSet other) {
|
| 86 | if (this.universe == null || other.universe == null) {
|
| 87 | return this;
|
| 88 | }
|
| 89 |
|
| 90 | if (this.universe != other.universe) {
|
| 91 | throw new IllegalArgumentException("Mismatched set elements: '" + this.universe + "' != '" + other.universe + "'");
|
| 92 | }
|
| 93 |
|
| 94 | long newMask = this.mask & ~other.mask;
|
| 95 | return newMask == 0L ? EMPTY : new FeatureFlagSet(this.universe, newMask);
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public boolean equals(final Object o) {
|
| 100 | return this == o ? true : o instanceof FeatureFlagSet that && this.universe == that.universe && this.mask == that.mask;
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | public int hashCode() {
|
| 105 | return (int)HashCommon.mix(this.mask);
|
| 106 | }
|
| 107 | }
|
| 108 |
|