|
1 | 1 | package org.violetmoon.zeta.module;
|
2 | 2 |
|
| 3 | +import org.apache.commons.lang3.text.WordUtils; |
| 4 | +import org.violetmoon.zeta.util.ZetaSide; |
| 5 | + |
3 | 6 | import java.util.Locale;
|
4 | 7 | import java.util.Set;
|
5 | 8 | import java.util.function.Function;
|
6 | 9 |
|
7 |
| -import org.apache.commons.lang3.text.WordUtils; |
8 |
| -import org.violetmoon.zeta.util.ZetaSide; |
9 |
| - |
10 | 10 | /**
|
11 | 11 | * performs some common data-munging of the data straight off a ZetaLoadModule annotation
|
12 | 12 | */
|
13 | 13 | public record TentativeModule(
|
14 |
| - Class<? extends ZetaModule> clazz, |
15 |
| - Class<? extends ZetaModule> keyClass, |
| 14 | + Class<? extends ZetaModule> clazz, |
| 15 | + Class<? extends ZetaModule> keyClass, |
16 | 16 |
|
17 |
| - ZetaCategory category, |
18 |
| - String displayName, |
19 |
| - String lowercaseName, |
20 |
| - String description, |
21 |
| - Set<String> antiOverlap, |
22 |
| - boolean enabledByDefault, |
| 17 | + ZetaCategory category, |
| 18 | + String displayName, |
| 19 | + String lowercaseName, |
| 20 | + String description, |
| 21 | + Set<String> antiOverlap, |
| 22 | + boolean enabledByDefault, |
23 | 23 |
|
24 |
| - boolean clientReplacement, |
25 |
| - int loadPhase |
| 24 | + boolean clientReplacement, |
| 25 | + int loadPhase |
26 | 26 | ) {
|
27 |
| - @SuppressWarnings("unchecked") |
28 |
| - public static TentativeModule from(ZetaLoadModuleAnnotationData data, Function<String, ZetaCategory> categoryResolver) { |
29 |
| - Class<?> clazzUnchecked = data.clazz(); |
30 |
| - if(!ZetaModule.class.isAssignableFrom(clazzUnchecked)) |
31 |
| - throw new RuntimeException("Class " + clazzUnchecked.getName() + " does not extend ZetaModule"); |
32 |
| - Class<? extends ZetaModule> clazz = (Class<? extends ZetaModule>) clazzUnchecked; |
| 27 | + @SuppressWarnings("unchecked") |
| 28 | + public static TentativeModule from(ZetaLoadModuleAnnotationData data, Function<String, ZetaCategory> categoryResolver) { |
| 29 | + Class<?> clazzUnchecked = data.clazz(); |
| 30 | + if (!ZetaModule.class.isAssignableFrom(clazzUnchecked)) |
| 31 | + throw new RuntimeException("Class " + clazzUnchecked.getName() + " does not extend ZetaModule"); |
| 32 | + Class<? extends ZetaModule> clazz = (Class<? extends ZetaModule>) clazzUnchecked; |
33 | 33 |
|
34 |
| - String displayName; |
35 |
| - if(data.name().isEmpty()) |
36 |
| - displayName = WordUtils.capitalizeFully(clazz.getSimpleName().replaceAll("Module$", "").replaceAll("(?<=.)([A-Z])", " $1")); |
37 |
| - else |
38 |
| - displayName = data.name(); |
39 |
| - String lowercaseName = displayName.toLowerCase(Locale.ROOT).replace(" ", "_"); |
| 34 | + String displayName; |
| 35 | + if (data.name().isEmpty()) |
| 36 | + displayName = WordUtils.capitalizeFully(clazz.getSimpleName().replaceAll("Module$", "").replaceAll("(?<=.)([A-Z])", " $1")); |
| 37 | + else |
| 38 | + displayName = data.name(); |
| 39 | + String lowercaseName = displayName.toLowerCase(Locale.ROOT).replace(" ", "_"); |
40 | 40 |
|
41 |
| - boolean clientReplacement = data.clientReplacement(); |
| 41 | + boolean clientReplacement = data.clientReplacement(); |
42 | 42 |
|
43 |
| - Class<? extends ZetaModule> keyClass; |
44 |
| - if(clientReplacement) { |
45 |
| - Class<?> sup = clazz.getSuperclass(); |
46 |
| - if(ZetaModule.class.isAssignableFrom(sup) && ZetaModule.class != sup) |
47 |
| - keyClass = (Class<? extends ZetaModule>) clazz.getSuperclass(); |
48 |
| - else |
49 |
| - throw new RuntimeException("Client extension module " + clazz.getName() + " should `extend` the module it's an extension of"); |
50 |
| - } else { |
51 |
| - keyClass = clazz; |
| 43 | + Class<? extends ZetaModule> keyClass; |
| 44 | + if (clientReplacement) { |
| 45 | + Class<?> sup = clazz.getSuperclass(); |
| 46 | + if (ZetaModule.class.isAssignableFrom(sup) && ZetaModule.class != sup) |
| 47 | + keyClass = (Class<? extends ZetaModule>) clazz.getSuperclass(); |
| 48 | + else |
| 49 | + throw new RuntimeException("Client extension module " + clazz.getName() + " should `extend` the module it's an extension of"); |
| 50 | + } else { |
| 51 | + keyClass = clazz; |
52 | 52 |
|
53 |
| - //Only client replacement modules are allowed to leave off a category annotation (since it uses the same category as the module it's replacing) |
54 |
| - if(data.category() == null || data.category().isEmpty()) |
55 |
| - throw new RuntimeException("Module " + clazz.getName() + " should specify a category"); |
56 |
| - } |
| 53 | + //Only client replacement modules are allowed to leave off a category annotation (since it uses the same category as the module it's replacing) |
| 54 | + if (data.category() == null || data.category().isEmpty()) |
| 55 | + throw new RuntimeException("Module " + clazz.getName() + " should specify a category"); |
| 56 | + } |
57 | 57 |
|
58 |
| - return new TentativeModule( |
59 |
| - clazz, |
60 |
| - keyClass, |
61 |
| - categoryResolver.apply(data.category()), |
62 |
| - displayName, |
63 |
| - lowercaseName, |
64 |
| - data.description(), |
65 |
| - Set.of(data.antiOverlap()), |
66 |
| - data.enabledByDefault(), |
67 |
| - clientReplacement, |
68 |
| - data.loadPhase() |
69 |
| - ); |
70 |
| - } |
| 58 | + return new TentativeModule( |
| 59 | + clazz, |
| 60 | + keyClass, |
| 61 | + categoryResolver.apply(data.category()), |
| 62 | + displayName, |
| 63 | + lowercaseName, |
| 64 | + data.description(), |
| 65 | + Set.of(data.antiOverlap()), |
| 66 | + data.enabledByDefault(), |
| 67 | + clientReplacement, |
| 68 | + data.loadPhase() |
| 69 | + ); |
| 70 | + } |
71 | 71 |
|
72 |
| - public TentativeModule replaceWith(TentativeModule replacement) { |
73 |
| - return new TentativeModule( |
74 |
| - replacement.clazz, |
75 |
| - this.keyClass, |
76 |
| - this.category, |
77 |
| - this.displayName, |
78 |
| - this.lowercaseName, |
79 |
| - this.description, |
80 |
| - this.antiOverlap, |
81 |
| - this.enabledByDefault, |
82 |
| - false, |
83 |
| - replacement.loadPhase |
84 |
| - ); |
85 |
| - } |
| 72 | + public TentativeModule replaceWith(TentativeModule replacement) { |
| 73 | + return new TentativeModule( |
| 74 | + replacement.clazz, |
| 75 | + this.keyClass, |
| 76 | + this.category, |
| 77 | + this.displayName, |
| 78 | + this.lowercaseName, |
| 79 | + this.description, |
| 80 | + this.antiOverlap, |
| 81 | + this.enabledByDefault, |
| 82 | + false, |
| 83 | + replacement.loadPhase |
| 84 | + ); |
| 85 | + } |
86 | 86 |
|
87 |
| - public boolean appliesTo(ZetaSide side) { |
88 |
| - return switch(side) { |
89 |
| - case CLIENT -> true; |
90 |
| - case SERVER -> !clientReplacement; |
91 |
| - }; |
92 |
| - } |
| 87 | + public boolean appliesTo(ZetaSide side) { |
| 88 | + return switch (side) { |
| 89 | + case CLIENT -> true; |
| 90 | + case SERVER -> !clientReplacement; |
| 91 | + }; |
| 92 | + } |
93 | 93 | }
|
0 commit comments