/
Mechanism.java
169 lines (140 loc) · 4.55 KB
/
Mechanism.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package net.aufdemrand.denizencore.objects;
import net.aufdemrand.denizencore.tags.TagContext;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import net.aufdemrand.denizencore.utilities.debugging.dB;
public class Mechanism {
private boolean fulfilled;
private String raw_mechanism;
private Element value;
public TagContext context;
public boolean isProperty = false;
public Mechanism(Element mechanism, Element value) {
fulfilled = false;
raw_mechanism = CoreUtilities.toLowerCase(mechanism.asString());
this.value = value;
}
public Mechanism(Element mechanism, Element value, TagContext context) {
this(mechanism, value);
this.context = context;
}
public void fulfill() {
fulfilled = true;
}
public boolean fulfilled() {
return fulfilled;
}
public String getName() {
return raw_mechanism;
}
public Element getValue() {
if (value == null) {
return new Element("");
}
return value;
}
public boolean hasValue() {
return value != null;
}
public boolean matches(String string) {
if (string.equals(raw_mechanism)) {
fulfill();
return true;
}
return false;
}
public boolean requireBoolean() {
return requireBoolean("Invalid boolean. Must specify TRUE or FALSE.");
}
public boolean requireDouble() {
return requireDouble("Invalid decimal number specified.");
}
public boolean requireEnum(boolean allowInt, Enum<?>... values) {
return requireEnum(null, allowInt, values);
}
public boolean requireFloat() {
return requireFloat("Invalid decimal number specified.");
}
public boolean requireInteger() {
return requireInteger("Invalid integer number specified.");
}
public <T extends dObject> boolean requireObject(Class<T> type) {
return requireObject(null, type);
}
public boolean requireBoolean(String error) {
if (hasValue() && value.isBoolean()) {
return true;
}
echoError(error);
return false;
}
public boolean requireDouble(String error) {
if (hasValue() && value.isDouble()) {
return true;
}
echoError(error);
return false;
}
public boolean requireEnum(String error, boolean allowInt, Enum<?>... values) {
if (hasValue() && allowInt && value.isInt() && value.asInt() < values.length) {
return true;
}
if (hasValue() && value.isString()) {
String raw_value = value.asString().toUpperCase();
for (Enum<?> check_value : values) {
if (raw_value.equals(check_value.name())) {
return true;
}
}
}
if (error == null) {
echoError("Invalid " + values[0].getDeclaringClass().getSimpleName() + "."
+ " Must specify a valid name" + (allowInt ? " or number" : "") + ".");
}
else {
echoError(error);
}
return false;
}
public boolean requireFloat(String error) {
if (hasValue() && value.isFloat()) {
return true;
}
echoError(error);
return false;
}
public boolean requireInteger(String error) {
if (hasValue() && value.isInt()) {
return true;
}
echoError(error);
return false;
}
public <T extends dObject> boolean requireObject(String error, Class<T> type) {
if (hasValue() && value.matchesType(type)) {
return true;
}
if (error == null) {
// TODO: Remove getSimpleName(), or simplify somehow.
echoError("Invalid " + type.getSimpleName() + " specified.");
}
else {
echoError(error);
}
return false;
}
public boolean shouldDebug() {
return context == null || context.debug;
}
public void echoError(String error) {
// TODO: Consider special cases of whether object properties with debug off should even show errors
dB.echoError(error);
}
public void reportInvalid() {
echoError("Invalid mechanism specified: " + raw_mechanism);
}
public void autoReport() {
if (!fulfilled()) {
reportInvalid();
}
}
}