Skip to content

Commit 1c6ee20

Browse files
Add additional Validators and accept Validators of super types (#9)
* Add validators for BigInteger and BigDecimal * Add validators for number comparison * Add more validators for strings * Accept validators of super types * Add annotations
1 parent 33333e4 commit 1c6ee20

17 files changed

+351
-35
lines changed

core/src/main/java/io/github/almightysatan/jaskl/ConfigEntry.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public interface ConfigEntry<T> {
8080
* @throws ValidationException if the default value fails validation
8181
*/
8282
@SafeVarargs
83-
static <T> @NotNull ConfigEntry<T> of(@NotNull Config config, @NotNull String path, @Nullable String description, T defaultValue, Type<T> type, @NotNull Validator<T>... validators) throws InvalidTypeException, ValidationException {
83+
static <T> @NotNull ConfigEntry<T> of(@NotNull Config config, @NotNull String path, @Nullable String description, T defaultValue, Type<T> type, @NotNull Validator<? super T>... validators) throws InvalidTypeException, ValidationException {
8484
WritableConfigEntryImpl<T> entry = new WritableConfigEntryImpl<>(Type.validated(type, validators), path, description, defaultValue);
8585
entry.register(config);
8686
return entry;
@@ -100,7 +100,7 @@ public interface ConfigEntry<T> {
100100
* @throws ValidationException if the default value fails validation
101101
*/
102102
@SafeVarargs
103-
static <T> @NotNull ConfigEntry<T> of(@NotNull Config config, @NotNull String path, T defaultValue, Type<T> type, @NotNull Validator<T>... validators) throws InvalidTypeException, ValidationException {
103+
static <T> @NotNull ConfigEntry<T> of(@NotNull Config config, @NotNull String path, T defaultValue, Type<T> type, @NotNull Validator<? super T>... validators) throws InvalidTypeException, ValidationException {
104104
return of(config, path, null, defaultValue, type, validators);
105105
}
106106
}

core/src/main/java/io/github/almightysatan/jaskl/Type.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public interface Type<T> {
3636

3737
@NotNull Object toWritable(@NotNull T value, @NotNull Function<@NotNull Object, @NotNull Object> keyPreprocessor) throws InvalidTypeException;
3838

39-
static <T> @NotNull Type<T> validated(@NotNull Type<T> type, @NotNull Validator<T> validator) {
39+
static <T> @NotNull Type<T> validated(@NotNull Type<T> type, @NotNull Validator<? super T> validator) {
4040
Objects.requireNonNull(type);
4141
Objects.requireNonNull(validator);
4242
return new Type<T>() {
@@ -61,7 +61,7 @@ public interface Type<T> {
6161
};
6262
}
6363

64-
static <T> @NotNull Type<T> validated(@NotNull Type<T> type, @NotNull Validator<T>[] validators) {
64+
static <T> @NotNull Type<T> validated(@NotNull Type<T> type, @NotNull Validator<? super T>[] validators) {
6565
Objects.requireNonNull(type);
6666
Objects.requireNonNull(validators);
6767
return validated(type, Validator.of(validators));

core/src/main/java/io/github/almightysatan/jaskl/Validator.java

+93-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import org.jetbrains.annotations.NotNull;
2424

25+
import java.math.BigDecimal;
26+
import java.math.BigInteger;
2527
import java.util.List;
2628
import java.util.Map;
2729
import java.util.Objects;
@@ -42,12 +44,12 @@ public interface Validator<T> {
4244
}
4345

4446
@SafeVarargs
45-
static <T> @NotNull Validator<T> of(@NotNull Validator<T>... validators) {
47+
static <T> @NotNull Validator<T> of(@NotNull Validator<? super T>... validators) {
4648
Objects.requireNonNull(validators);
4749
if (validators.length == 0)
4850
return nop();
4951
return value -> {
50-
for (Validator<T> validator : validators)
52+
for (Validator<? super T> validator : validators)
5153
validator.validate(value);
5254
};
5355
}
@@ -62,6 +64,14 @@ public interface Validator<T> {
6264

6365
Validator<Double> DOUBLE_NEGATIVE = value -> { if (value >= 0) throw new ValidationException("should be negative"); };
6466

67+
static @NotNull Validator<Double> doubleGreater(double other) { return value -> { if (value > other) throw new ValidationException("should be greater than " + other); }; }
68+
69+
static @NotNull Validator<Double> doubleGreaterOrEqual(double other) { return value -> { if (value >= other) throw new ValidationException("should be greater than or equal to " + other); }; }
70+
71+
static @NotNull Validator<Double> doubleLess(double other) { return value -> { if (value < other) throw new ValidationException("should be less than " + other); }; }
72+
73+
static @NotNull Validator<Double> doubleLessOrEqual(double other) { return value -> { if (value <= other) throw new ValidationException("should be less than or equal to " + other); }; }
74+
6575
Validator<Float> FLOAT_NOT_ZERO = value -> { if (value == 0) throw new ValidationException("should not be 0"); };
6676

6777
Validator<Float> FLOAT_NOT_POSITIVE = value -> { if (value > 0) throw new ValidationException("should not be positive"); };
@@ -72,6 +82,14 @@ public interface Validator<T> {
7282

7383
Validator<Float> FLOAT_NEGATIVE = value -> { if (value >= 0) throw new ValidationException("should be negative"); };
7484

85+
static @NotNull Validator<Float> floatGreater(float other) { return value -> { if (value > other) throw new ValidationException("should be greater than " + other); }; }
86+
87+
static @NotNull Validator<Float> floatGreaterOrEqual(float other) { return value -> { if (value >= other) throw new ValidationException("should be greater than or equal to " + other); }; }
88+
89+
static @NotNull Validator<Float> floatLess(float other) { return value -> { if (value < other) throw new ValidationException("should be less than " + other); }; }
90+
91+
static @NotNull Validator<Float> floatLessOrEqual(float other) { return value -> { if (value <= other) throw new ValidationException("should be less than or equal to " + other); }; }
92+
7593
Validator<Integer> INTEGER_NOT_ZERO = value -> { if (value == 0) throw new ValidationException("should not be 0"); };
7694

7795
Validator<Integer> INTEGER_NOT_POSITIVE = value -> { if (value > 0) throw new ValidationException("should not be positive"); };
@@ -82,6 +100,14 @@ public interface Validator<T> {
82100

83101
Validator<Integer> INTEGER_NEGATIVE = value -> { if (value >= 0) throw new ValidationException("should be negative"); };
84102

103+
static @NotNull Validator<Integer> integerGreater(int other) { return value -> { if (value > other) throw new ValidationException("should be greater than " + other); }; }
104+
105+
static @NotNull Validator<Integer> integerGreaterOrEqual(int other) { return value -> { if (value >= other) throw new ValidationException("should be greater than or equal to " + other); }; }
106+
107+
static @NotNull Validator<Integer> integerLess(int other) { return value -> { if (value < other) throw new ValidationException("should be less than " + other); }; }
108+
109+
static @NotNull Validator<Integer> integerLessOrEqual(int other) { return value -> { if (value <= other) throw new ValidationException("should be less than or equal to " + other); }; }
110+
85111
Validator<Long> LONG_NOT_ZERO = value -> { if (value == 0) throw new ValidationException("should not be 0"); };
86112

87113
Validator<Long> LONG_NOT_POSITIVE = value -> { if (value > 0) throw new ValidationException("should not be positive"); };
@@ -92,8 +118,70 @@ public interface Validator<T> {
92118

93119
Validator<Long> LONG_NEGATIVE = value -> { if (value >= 0) throw new ValidationException("should be negative"); };
94120

121+
static @NotNull Validator<Long> longGreater(long other) { return value -> { if (value > other) throw new ValidationException("should be greater than " + other); }; }
122+
123+
static @NotNull Validator<Long> longGreaterOrEqual(long other) { return value -> { if (value >= other) throw new ValidationException("should be greater than or equal to " + other); }; }
124+
125+
static @NotNull Validator<Long> longLess(long other) { return value -> { if (value < other) throw new ValidationException("should be less than " + other); }; }
126+
127+
static @NotNull Validator<Long> longLessOrEqual(long other) { return value -> { if (value <= other) throw new ValidationException("should be less than or equal to " + other); }; }
128+
129+
Validator<BigInteger> BIG_INTEGER_NOT_ZERO = value -> { if (value.signum() == 0) throw new ValidationException("should not be 0"); };
130+
131+
Validator<BigInteger> BIG_INTEGER_NOT_POSITIVE = value -> { if (value.signum() == 1) throw new ValidationException("should not be positive"); };
132+
133+
Validator<BigInteger> BIG_INTEGER_NOT_NEGATIVE = value -> { if (value.signum() == -1) throw new ValidationException("should not be negative"); };
134+
135+
Validator<BigInteger> BIG_INTEGER_POSITIVE = value -> { if (value.signum() != 1) throw new ValidationException("should be positive"); };
136+
137+
Validator<BigInteger> BIG_INTEGER_NEGATIVE = value -> { if (value.signum() != -1) throw new ValidationException("should be negative"); };
138+
139+
static @NotNull Validator<BigInteger> bigIntegerGreater(BigInteger other) { return value -> { if (value.compareTo(other) < 1) throw new ValidationException("should be greater than " + other); }; }
140+
141+
static @NotNull Validator<BigInteger> bigIntegerGreaterOrEqual(BigInteger other) { return value -> { if (value.compareTo(other) < 0) throw new ValidationException("should be greater than or equal to " + other); }; }
142+
143+
static @NotNull Validator<BigInteger> bigIntegerLess(BigInteger other) { return value -> { if (value.compareTo(other) > -1) throw new ValidationException("should be less than " + other); }; }
144+
145+
static @NotNull Validator<BigInteger> bigIntegerLessOrEqual(BigInteger other) { return value -> { if (value.compareTo(other) > 0) throw new ValidationException("should be less than or equal to " + other); }; }
146+
147+
Validator<BigDecimal> BIG_DECIMAL_NOT_ZERO = value -> { if (value.signum() == 0) throw new ValidationException("should not be 0"); };
148+
149+
Validator<BigDecimal> BIG_DECIMAL_NOT_POSITIVE = value -> { if (value.signum() == 1) throw new ValidationException("should not be positive"); };
150+
151+
Validator<BigDecimal> BIG_DECIMAL_NOT_NEGATIVE = value -> { if (value.signum() == -1) throw new ValidationException("should not be negative"); };
152+
153+
Validator<BigDecimal> BIG_DECIMAL_POSITIVE = value -> { if (value.signum() != 1) throw new ValidationException("should be positive"); };
154+
155+
Validator<BigDecimal> BIG_DECIMAL_NEGATIVE = value -> { if (value.signum() != -1) throw new ValidationException("should be negative"); };
156+
157+
static @NotNull Validator<BigDecimal> bigDecimalGreater(BigDecimal other) { return value -> { if (value.compareTo(other) < 1) throw new ValidationException("should be greater than " + other); }; }
158+
159+
static @NotNull Validator<BigDecimal> bigDecimalGreaterOrEqual(BigDecimal other) { return value -> { if (value.compareTo(other) < 0) throw new ValidationException("should be greater than or equal to " + other); }; }
160+
161+
static @NotNull Validator<BigDecimal> bigDecimalLess(BigDecimal other) { return value -> { if (value.compareTo(other) > -1) throw new ValidationException("should be less than " + other); }; }
162+
163+
static @NotNull Validator<BigDecimal> bigDecimalLessOrEqual(BigDecimal other) { return value -> { if (value.compareTo(other) > 0) throw new ValidationException("should be less than or equal to " + other); }; }
164+
95165
Validator<String> STRING_NOT_EMPTY = value -> { if (value.isEmpty()) throw new ValidationException("should not be empty"); };
96166

167+
Validator<String> STRING_ALPHABETIC = value -> {
168+
for (char c : value.toCharArray())
169+
if ((c < 0x41 || c > 0x5A) && (c < 0x61 || c > 0x7A))
170+
throw new ValidationException("should only contain alphabetic characters");
171+
};
172+
173+
Validator<String> STRING_NUMERIC = value -> {
174+
for (char c : value.toCharArray())
175+
if (c < 0x30 || c > 0x39)
176+
throw new ValidationException("should only contain numbers");
177+
};
178+
179+
Validator<String> STRING_ALPHANUMERIC = value -> {
180+
for (char c : value.toCharArray())
181+
if ((c < 0x41 || c > 0x5A) && (c < 0x61 || c > 0x7A) && (c < 0x30 || c > 0x39))
182+
throw new ValidationException("should only contain alphanumeric characters");
183+
};
184+
97185
static @NotNull Validator<String> stringMinLength(int size) { return value -> { if (value.length() < size) throw new ValidationException("should be at least " + size + " characters long"); };}
98186

99187
static @NotNull Validator<String> stringMaxLength(int size) { return value -> { if (value.length() > size) throw new ValidationException("should not be longer than " + size + " characters"); };}
@@ -104,15 +192,15 @@ public interface Validator<T> {
104192

105193
static <T> @NotNull Validator<List<T>> listMaxSize(int size) { return value -> { if (value.size() > size) throw new ValidationException("should not have more than " + size + " entries"); };}
106194

107-
static <T> @NotNull Validator<List<T>> listForEach(Validator<T> validator) { return value -> value.forEach(validator::validate);}
195+
static <T> @NotNull Validator<List<T>> listForEach(Validator<? super T> validator) { return value -> value.forEach(validator::validate);}
108196

109197
static <K, V> @NotNull Validator<Map<K, V>> mapNotEmpty() { return value -> { if (value.isEmpty()) throw new ValidationException("should not be empty"); };}
110198

111199
static <K, V> @NotNull Validator<Map<K, V>> mapMinSize(int size) { return value -> { if (value.size() < size) throw new ValidationException("should have at least " + size + " entries"); };}
112200

113201
static <K, V> @NotNull Validator<Map<K, V>> mapMaxSize(int size) { return value -> { if (value.size() > size) throw new ValidationException("should not have more than " + size + " entries"); };}
114202

115-
static <K, V> @NotNull Validator<Map<K, V>> mapForEachKey(Validator<K> validator) { return value -> value.keySet().forEach(validator::validate);}
203+
static <K, V> @NotNull Validator<Map<K, V>> mapForEachKey(Validator<? super K> validator) { return value -> value.keySet().forEach(validator::validate);}
116204

117-
static <K, V> @NotNull Validator<Map<K, V>> mapForEachValue(Validator<V> validator) { return value -> value.values().forEach(validator::validate);}
205+
static <K, V> @NotNull Validator<Map<K, V>> mapForEachValue(Validator<? super V> validator) { return value -> value.values().forEach(validator::validate);}
118206
}

0 commit comments

Comments
 (0)