|
| 1 | +package org.comroid.api; |
| 2 | + |
| 3 | +import org.comroid.util.Bitmask; |
| 4 | +import org.jetbrains.annotations.NotNull; |
| 5 | + |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.HashSet; |
| 8 | +import java.util.Set; |
| 9 | + |
| 10 | +/** |
| 11 | + * Helper interface for enum classes that store an integer bitmask. |
| 12 | + * <p> |
| 13 | + * The default generated bitmasks are dependent on the order of constants. |
| 14 | + * |
| 15 | + * @param <S> The implementing Enum type |
| 16 | + * @see #getValue() for further information |
| 17 | + * @see Named Default Enum implementation |
| 18 | + */ |
| 19 | +public interface BitmaskAttribute<S extends BitmaskAttribute<S>> extends IntegerAttribute, SelfDeclared<S>, Named { |
| 20 | + /** |
| 21 | + * Computes a default integer value for this bitmask, depending on enum order. |
| 22 | + * If implemented by an enum class, this method provides unique default bitmasks for every enum constant. |
| 23 | + * |
| 24 | + * @return The integer value of this Bitmask constant. |
| 25 | + * @see IntegerAttribute#getValue() Returns Enums ordinal value if possible |
| 26 | + */ |
| 27 | + @Override |
| 28 | + @NotNull |
| 29 | + default Integer getValue() { |
| 30 | + return 1 << IntegerAttribute.super.getValue(); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Creates a set of all mask attributes from an integer value and an enum class. |
| 35 | + * |
| 36 | + * @param mask The integer value to scan |
| 37 | + * @param viaEnum The enum to use all constants from. |
| 38 | + * @param <T> The enum type. |
| 39 | + * @return A set of all Bitmask attributes set in the integer value |
| 40 | + */ |
| 41 | + static <T extends java.lang.Enum<? extends T> & BitmaskAttribute<T>> Set<T> valueOf(int mask, Class<T> viaEnum) { |
| 42 | + if (!viaEnum.isEnum()) |
| 43 | + throw new IllegalArgumentException("Only enums allowed as parameter 'viaEnum'"); |
| 44 | + |
| 45 | + return valueOf(mask, viaEnum.getEnumConstants()); |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + /** |
| 50 | + * Creates a set of all mask attributes from an integer value and an enum class. |
| 51 | + * |
| 52 | + * @param mask The integer value to scan |
| 53 | + * @param values All possible mask attributes |
| 54 | + * @param <T> The enum type. |
| 55 | + * @return A set of all Bitmask attributes set in the integer value |
| 56 | + */ |
| 57 | + static <T extends BitmaskAttribute<T>> Set<T> valueOf(int mask, T[] values) { |
| 58 | + HashSet<T> yields = new HashSet<>(); |
| 59 | + |
| 60 | + for (T constant : values) { |
| 61 | + if (constant.isFlagSet(mask)) |
| 62 | + yields.add(constant); |
| 63 | + } |
| 64 | + |
| 65 | + return Collections.unmodifiableSet(yields); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Creates an integer value containing all provided Bitmask attributes. |
| 70 | + * |
| 71 | + * @param values All values to combine |
| 72 | + * @return The result integer value |
| 73 | + */ |
| 74 | + static int toMask(BitmaskAttribute<?>[] values) { |
| 75 | + int x = 0; |
| 76 | + for (BitmaskAttribute<?> each : values) |
| 77 | + x = each.apply(x, true); |
| 78 | + return x; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Checks whether this Bitmask attribute contains another attribute. |
| 83 | + * |
| 84 | + * @param other The other attribute. |
| 85 | + * @return Whether the other attribute is contained in this attribute |
| 86 | + */ |
| 87 | + default boolean hasFlag(BitmaskAttribute<S> other) { |
| 88 | + return Bitmask.isFlagSet(getValue(), other.getValue()); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Checks whether this attribute is set within an integer mask. |
| 93 | + * |
| 94 | + * @param inMask The mask to check. |
| 95 | + * @return Whether this attribute is contained in the mask |
| 96 | + */ |
| 97 | + default boolean isFlagSet(int inMask) { |
| 98 | + return Bitmask.isFlagSet(inMask, getValue()); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Applies the {@code newState} of this attribute to the given mask, and returns the result. |
| 103 | + * |
| 104 | + * @param toMask The mask to apply this attribute to |
| 105 | + * @param newState The desired state of this attribute within the mask |
| 106 | + * @return The new mask |
| 107 | + */ |
| 108 | + default int apply(int toMask, boolean newState) { |
| 109 | + return Bitmask.modifyFlag(toMask, getValue(), newState); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * {@linkplain Object#equals(Object) Equals-implementation} to accept instances of BitmaskAttribute |
| 114 | + * |
| 115 | + * @param other The attribute to check against. |
| 116 | + * @return Whether the attribute values are equal |
| 117 | + */ |
| 118 | + default boolean equals(BitmaskAttribute<?> other) { |
| 119 | + return getValue() == (int) other.getValue(); |
| 120 | + } |
| 121 | +} |
0 commit comments