Skip to content

Commit 893b3cc

Browse files
committed
#448 Add more OptionalProperty initializer methods
1 parent d0e9c8f commit 893b3cc

2 files changed

Lines changed: 29 additions & 11 deletions

File tree

src/main/java/ch/jalu/configme/properties/PropertyInitializer.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -317,48 +317,62 @@ public static <E> ArrayPropertyBuilder<E, InlineArrayProperty<E>> inlineArrayPro
317317
// --------------
318318
// Optional flavors
319319
// --------------
320+
public static <T> @NotNull OptionalProperty<T> optionalProperty(@NotNull String path, PropertyType<T> type) {
321+
return new OptionalProperty<>(path, type);
322+
}
323+
320324
public static @NotNull OptionalProperty<Boolean> optionalBooleanProperty(@NotNull String path) {
321-
return new OptionalProperty<>(path, BooleanType.BOOLEAN);
325+
return optionalProperty(path, BooleanType.BOOLEAN);
322326
}
323327

324328
public static @NotNull OptionalProperty<Short> optionalShortProperty(@NotNull String path) {
325-
return new OptionalProperty<>(path, NumberType.SHORT);
329+
return optionalProperty(path, NumberType.SHORT);
326330
}
327331

328332
public static @NotNull OptionalProperty<Integer> optionalIntegerProperty(@NotNull String path) {
329-
return new OptionalProperty<>(path, NumberType.INTEGER);
333+
return optionalProperty(path, NumberType.INTEGER);
330334
}
331335

332336
public static @NotNull OptionalProperty<Long> optionalLongProperty(@NotNull String path) {
333-
return new OptionalProperty<>(path, NumberType.LONG);
337+
return optionalProperty(path, NumberType.LONG);
334338
}
335339

336340
public static @NotNull OptionalProperty<Float> optionalFloatProperty(@NotNull String path) {
337-
return new OptionalProperty<>(path, NumberType.FLOAT);
341+
return optionalProperty(path, NumberType.FLOAT);
338342
}
339343

340344
public static @NotNull OptionalProperty<Double> optionalDoubleProperty(@NotNull String path) {
341-
return new OptionalProperty<>(path, NumberType.DOUBLE);
345+
return optionalProperty(path, NumberType.DOUBLE);
342346
}
343347

344348
public static @NotNull OptionalProperty<String> optionalStringProperty(@NotNull String path) {
345-
return new OptionalProperty<>(path, StringType.STRING);
349+
return optionalProperty(path, StringType.STRING);
346350
}
347351

348352
public static <E extends Enum<E>> @NotNull OptionalProperty<E> optionalEnumProperty(@NotNull Class<E> clazz,
349353
@NotNull String path) {
350-
return new OptionalProperty<>(path, new EnumPropertyType<>(clazz));
354+
return optionalProperty(path, new EnumPropertyType<>(clazz));
351355
}
352356

353357
public static @NotNull OptionalProperty<Pattern> optionalRegexProperty(@NotNull String path) {
354-
return new OptionalProperty<>(path, RegexType.REGEX);
358+
return optionalProperty(path, RegexType.REGEX);
355359
}
356360

357361
public static @NotNull OptionalProperty<List<String>> optionalListProperty(@NotNull String path) {
358-
return new OptionalProperty<>(path, new ListPropertyType<>(StringType.STRING));
362+
return optionalListProperty(path, StringType.STRING);
363+
}
364+
365+
public static <T> @NotNull OptionalProperty<List<T>> optionalListProperty(@NotNull String path,
366+
@NotNull PropertyType<T> type) {
367+
return new OptionalProperty<>(path, new ListPropertyType<>(type));
359368
}
360369

361370
public static @NotNull OptionalProperty<Set<String>> optionalSetProperty(@NotNull String path) {
362-
return new OptionalProperty<>(path, new SetPropertyType<>(StringType.STRING));
371+
return optionalSetProperty(path, StringType.STRING);
372+
}
373+
374+
public static <T> @NotNull OptionalProperty<Set<T>> optionalSetProperty(@NotNull String path,
375+
@NotNull PropertyType<T> type) {
376+
return new OptionalProperty<>(path, new SetPropertyType<>(type));
363377
}
364378
}

src/test/java/ch/jalu/configme/properties/PropertyInitializerTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import static ch.jalu.configme.properties.PropertyInitializer.optionalIntegerProperty;
3636
import static ch.jalu.configme.properties.PropertyInitializer.optionalListProperty;
3737
import static ch.jalu.configme.properties.PropertyInitializer.optionalLongProperty;
38+
import static ch.jalu.configme.properties.PropertyInitializer.optionalProperty;
3839
import static ch.jalu.configme.properties.PropertyInitializer.optionalRegexProperty;
3940
import static ch.jalu.configme.properties.PropertyInitializer.optionalSetProperty;
4041
import static ch.jalu.configme.properties.PropertyInitializer.optionalShortProperty;
@@ -71,6 +72,7 @@ void shouldInstantiateProperties() {
7172
assertThat(newLowercaseStringSetProperty("path", Arrays.asList("5", "7")), instanceOf(LowercaseStringSetProperty.class));
7273
assertThat(newBeanProperty(WorldGroupConfig.class, "worlds", new WorldGroupConfig()), instanceOf(BeanProperty.class));
7374

75+
assertThat(optionalProperty("path", NumberType.LONG), instanceOf(OptionalProperty.class));
7476
assertThat(optionalBooleanProperty("path"), instanceOf(OptionalProperty.class));
7577
assertThat(optionalShortProperty("path"), instanceOf(OptionalProperty.class));
7678
assertThat(optionalIntegerProperty("path"), instanceOf(OptionalProperty.class));
@@ -81,7 +83,9 @@ void shouldInstantiateProperties() {
8183
assertThat(optionalEnumProperty(TestEnum.class, "path"), instanceOf(OptionalProperty.class));
8284
assertThat(optionalRegexProperty("path"), instanceOf(OptionalProperty.class));
8385
assertThat(optionalListProperty("path"), instanceOf(OptionalProperty.class));
86+
assertThat(optionalListProperty("path", BooleanType.BOOLEAN), instanceOf(OptionalProperty.class));
8487
assertThat(optionalSetProperty("path"), instanceOf(OptionalProperty.class));
88+
assertThat(optionalSetProperty("path", NumberType.INTEGER), instanceOf(OptionalProperty.class));
8589
}
8690

8791
@Test

0 commit comments

Comments
 (0)