|
3 | 3 | import java.lang.annotation.Retention;
|
4 | 4 | import java.lang.annotation.RetentionPolicy;
|
5 | 5 | import java.lang.annotation.Target;
|
| 6 | +import java.util.EnumSet; |
6 | 7 |
|
7 | 8 | /**
|
8 |
| - * Used by {@link Property Properties} to determine what are valid values for the {@link Property}. |
9 |
| - * Frequently used in an array containing two entries, indicating minimum and maximum bounds. |
| 9 | + * Used for comparisons by {@link Property Properties} to determine what are valid values for the {@link Property}. |
| 10 | + * <p> |
| 11 | + * Fully written out, it uses the array {@link #types()} to store the types being tracked, |
| 12 | + * and the corresponding element to store what the value being compared against is. |
| 13 | + * <p> |
| 14 | + * However, to improve the ease of use, some information can be assumed by default and are thus optional. |
| 15 | + * <p> |
| 16 | + * If {@link #types()} is empty, elements that do not match the default value will be checked. |
| 17 | + * This means that in most cases {@link #types()} does not need to be declared. |
| 18 | + * <br> |
| 19 | + * The only exception is if any of {@link #gt()}, {@link #gte()}, {@link #eq()}, {@link #lte()}, {@link #lt()}, |
| 20 | + * {@link #not()}, or {@link #unique()} elements match the default value, |
| 21 | + * which is {@link Integer#MIN_VALUE} for the {@link #gt()}, {@link #gte()}, {@link #eq()}, {@link #lte()}, and {@link #lt()} elements and |
| 22 | + * an empty string ({@code ""}) for both {@link #not()} and {@link #unique()} elements. |
| 23 | + * In this situation, {@link #types()} will need to contain all relevant {@link Type Types}. |
10 | 24 | *
|
11 |
| - * @see Property#valid() |
| 25 | + * @see Property#comp() |
12 | 26 | */
|
13 | 27 | @Retention(RetentionPolicy.RUNTIME)
|
14 | 28 | @Target({ /* No targets allowed */})
|
15 | 29 | public @interface Comp {
|
16 | 30 |
|
17 | 31 | /**
|
18 | 32 | * @return the method of comparison
|
| 33 | + * @deprecated use {@link #types()} instead |
19 | 34 | */
|
| 35 | + @Deprecated |
20 | 36 | Type type() default Type.EQ;
|
21 | 37 |
|
22 | 38 | /**
|
23 | 39 | * @return the value that {@link #type()} compares with
|
| 40 | + * @deprecated use {@link #lt()}, {@link #lte()}, {@link #gte()}, {@link #gt()}, {@link #eq()}, {@link #not()}, or {@link #unique()} instead |
24 | 41 | */
|
25 |
| - String value(); |
| 42 | + @Deprecated |
| 43 | + String value() default ""; |
| 44 | + |
| 45 | + /** |
| 46 | + * @return an array of types to compare with. In most cases, this can be assumed. |
| 47 | + */ |
| 48 | + Type[] types() default {}; |
| 49 | + |
| 50 | + /** |
| 51 | + * The value represent less than. |
| 52 | + * Enabled either via being set to a non-default value, or by adding {@link Comp.Type#LT} to the {@link #types()} element. |
| 53 | + * |
| 54 | + * @return if enabled, the value used to represent less than |
| 55 | + */ |
| 56 | + int lt() default Integer.MIN_VALUE; |
| 57 | + |
| 58 | + /** |
| 59 | + * The value represent less than or equal to. |
| 60 | + * Enabled either via being set to a non-default value, or by adding {@link Comp.Type#LTE} to the {@link #types()} element. |
| 61 | + * |
| 62 | + * @return if enabled, the value used to represent less than or equal to |
| 63 | + */ |
| 64 | + int lte() default Integer.MIN_VALUE; |
| 65 | + |
| 66 | + /** |
| 67 | + * The value represent greater than or equal to. |
| 68 | + * Enabled either via being set to a non-default value, or by adding {@link Comp.Type#GTE} to the {@link #types()} element. |
| 69 | + * |
| 70 | + * @return if enabled, the value used to represent greater than or equal to |
| 71 | + */ |
| 72 | + int gte() default Integer.MIN_VALUE; |
| 73 | + |
| 74 | + /** |
| 75 | + * The value represent greater than. |
| 76 | + * Enabled either via being set to a non-default value, or by adding {@link Comp.Type#GT} to the {@link #types()} element. |
| 77 | + * |
| 78 | + * @return if enabled, the value used to represent greater than |
| 79 | + */ |
| 80 | + int gt() default Integer.MIN_VALUE; |
| 81 | + |
| 82 | + /** |
| 83 | + * The value represent equal to. |
| 84 | + * Enabled either via being set to a non-default value, or by adding {@link Comp.Type#EQ} to the {@link #types()} element. |
| 85 | + * |
| 86 | + * @return if enabled, the value used to represent equal to |
| 87 | + */ |
| 88 | + int eq() default Integer.MIN_VALUE; |
| 89 | + |
| 90 | + /** |
| 91 | + * The value represent not equal to. |
| 92 | + * Enabled either via being set to a non-default value, or by adding {@link Comp.Type#NOT} to the {@link #types()} element. |
| 93 | + * |
| 94 | + * @return if enabled, the value used to represent not equal to |
| 95 | + */ |
| 96 | + String not() default ""; |
| 97 | + |
| 98 | + /** |
| 99 | + * @return if {@link #types()} contains {@link Comp.Type#UNI}, the lang key used to represent the unique description |
| 100 | + */ |
| 101 | + String unique() default ""; |
26 | 102 |
|
27 | 103 | /**
|
28 | 104 | * Used to determine the type of comparison. Contains a symbol representation and a localization key.
|
|
63 | 139 | * <th>!=</th>
|
64 | 140 | * <th>groovyscript.wiki.not</th>
|
65 | 141 | * </tr>
|
| 142 | + * <tr> |
| 143 | + * <th>UNI</th> |
| 144 | + * <th>!?</th> |
| 145 | + * <th>groovyscript.wiki.unique</th> |
| 146 | + * </tr> |
66 | 147 | * </table>
|
67 | 148 | */
|
68 | 149 | enum Type {
|
69 |
| - GT(">", "greater_than"), |
70 |
| - GTE(">=", "greater_than_or_equal_to"), |
71 |
| - EQ("==", "equal_to"), |
72 |
| - LTE("<=", "less_than_or_equal_to"), |
73 |
| - LT("<", "less_than"), |
74 |
| - NOT("!=", "not"); |
75 |
| - |
76 |
| - private static final String baseLocalizationPath = "groovyscript.wiki."; |
| 150 | + GT(">", "groovyscript.wiki.greater_than"), |
| 151 | + GTE(">=", "groovyscript.wiki.greater_than_or_equal_to"), |
| 152 | + EQ("==", "groovyscript.wiki.equal_to"), |
| 153 | + LTE("<=", "groovyscript.wiki.less_than_or_equal_to"), |
| 154 | + LT("<", "groovyscript.wiki.less_than"), |
| 155 | + NOT("!=", "groovyscript.wiki.not"), |
| 156 | + UNI("!?", "groovyscript.wiki.unique"); |
77 | 157 |
|
78 | 158 | private final String symbol;
|
79 | 159 | private final String key;
|
80 | 160 |
|
81 | 161 | Type(String symbol, String key) {
|
82 | 162 | this.symbol = symbol;
|
83 |
| - this.key = baseLocalizationPath + key; |
| 163 | + this.key = key; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Creates an EnumSet based on the given {@link Comp}. |
| 168 | + * If {@link Comp#types()} has any elements, the types contained will be used. |
| 169 | + * Otherwise, any non-default values for the individual elements will be used. |
| 170 | + * |
| 171 | + * @param comp the {@link Comp} instance to be parsed |
| 172 | + * @return a set containing the types that are used in the Comp |
| 173 | + */ |
| 174 | + public static EnumSet<Type> getUsedTypes(Comp comp) { |
| 175 | + if (comp.types().length > 0) return EnumSet.of(comp.types()[0], comp.types()); |
| 176 | + var usedTypes = EnumSet.noneOf(Comp.Type.class); |
| 177 | + if (comp.gt() != Integer.MIN_VALUE) usedTypes.add(Comp.Type.GT); |
| 178 | + if (comp.gte() != Integer.MIN_VALUE) usedTypes.add(Comp.Type.GTE); |
| 179 | + if (comp.eq() != Integer.MIN_VALUE) usedTypes.add(Comp.Type.EQ); |
| 180 | + if (comp.lte() != Integer.MIN_VALUE) usedTypes.add(Comp.Type.LTE); |
| 181 | + if (comp.lt() != Integer.MIN_VALUE) usedTypes.add(Comp.Type.LT); |
| 182 | + if (!comp.not().isEmpty()) usedTypes.add(Comp.Type.NOT); |
| 183 | + if (!comp.unique().isEmpty()) usedTypes.add(Comp.Type.UNI); |
| 184 | + return usedTypes; |
84 | 185 | }
|
85 | 186 |
|
86 | 187 | public String getSymbol() {
|
|
0 commit comments