Make ConfigLib Java 8 compatible

dev v2.0.1
Exlll 6 years ago
parent f4795ca21a
commit 71a6052e29

@ -1,3 +1,5 @@
language: java language: java
jdk: jdk:
oraclejdk10 - oraclejdk8
- oraclejdk9
- oraclejdk10

@ -1,5 +1,5 @@
name: ConfigLib name: ConfigLib
author: Exlll author: Exlll
version: 2.0.0 version: 2.0.1
main: de.exlll.configlib.ConfigLib main: de.exlll.configlib.ConfigLib

@ -1,5 +1,5 @@
name: ConfigLib name: ConfigLib
author: Exlll author: Exlll
version: 2.0.0 version: 2.0.1
main: de.exlll.configlib.ConfigLib main: de.exlll.configlib.ConfigLib

@ -41,7 +41,7 @@ public final class Comments {
private static List<String> getComments(AnnotatedElement element) { private static List<String> getComments(AnnotatedElement element) {
Comment comment = element.getAnnotation(Comment.class); Comment comment = element.getAnnotation(Comment.class);
return (comment != null) return (comment != null)
? List.of(comment.value()) ? Arrays.asList(comment.value())
: Collections.emptyList(); : Collections.emptyList();
} }

@ -7,14 +7,11 @@ import de.exlll.configlib.annotation.NoConvert;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.Arrays; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Set;
enum Reflect { enum Reflect {
; ;
private static final Set<Class<?>> SIMPLE_TYPES = Set.of( private static final Set<Class<?>> SIMPLE_TYPES = new HashSet<>(Arrays.asList(
Boolean.class, Boolean.class,
Byte.class, Byte.class,
Character.class, Character.class,
@ -24,7 +21,7 @@ enum Reflect {
Float.class, Float.class,
Double.class, Double.class,
String.class String.class
); ));
static boolean isSimpleType(Class<?> cls) { static boolean isSimpleType(Class<?> cls) {
return cls.isPrimitive() || SIMPLE_TYPES.contains(cls); return cls.isPrimitive() || SIMPLE_TYPES.contains(cls);

@ -31,7 +31,7 @@ final class YamlComments {
Map.Entry<String, List<String>> entry Map.Entry<String, List<String>> entry
) { ) {
String fieldComments = commentListToString(entry.getValue()); String fieldComments = commentListToString(entry.getValue());
return Map.entry(entry.getKey(), fieldComments); return new MapEntry<>(entry.getKey(), fieldComments);
} }
private String commentListToString(List<String> comments) { private String commentListToString(List<String> comments) {
@ -43,4 +43,32 @@ final class YamlComments {
private String toCommentLine(String comment) { private String toCommentLine(String comment) {
return comment.isEmpty() ? "" : "# " + comment; return comment.isEmpty() ? "" : "# " + comment;
} }
private static final class MapEntry<K, V> implements Map.Entry<K, V> {
private final K key;
private V value;
public MapEntry(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
@Override
public V setValue(V value) {
V old = this.value;
this.value = value;
return old;
}
}
} }

@ -8,6 +8,7 @@ import org.yaml.snakeyaml.Yaml;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -103,7 +104,7 @@ final class YamlSource implements ConfigurationSource<YamlConfiguration> {
private void addFieldComments() { private void addFieldComments() {
if (comments.hasFieldComments()) { if (comments.hasFieldComments()) {
List<String> dumpLines = List.of(dump.split("\n")); List<String> dumpLines = Arrays.asList(dump.split("\n"));
addDumpLines(dumpLines); addDumpLines(dumpLines);
} else { } else {
builder.append(dump); builder.append(dump);

@ -3,9 +3,8 @@ package de.exlll.configlib;
import de.exlll.configlib.annotation.Comment; import de.exlll.configlib.annotation.Comment;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.List; import static de.exlll.configlib.util.CollectionFactory.listOf;
import java.util.Map; import static de.exlll.configlib.util.CollectionFactory.mapOf;
import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@ -27,11 +26,11 @@ public class CommentsTest {
assertThat(comments.getFieldComments().entrySet(), empty()); assertThat(comments.getFieldComments().entrySet(), empty());
comments = Comments.ofClass(B.class); comments = Comments.ofClass(B.class);
assertThat(comments.getClassComments(), is(List.of("B"))); assertThat(comments.getClassComments(), is(listOf("B")));
assertThat(comments.getFieldComments().entrySet(), empty()); assertThat(comments.getFieldComments().entrySet(), empty());
comments = Comments.ofClass(C.class); comments = Comments.ofClass(C.class);
assertThat(comments.getClassComments(), is(List.of("C", "D"))); assertThat(comments.getClassComments(), is(listOf("C", "D")));
assertThat(comments.getFieldComments().entrySet(), empty()); assertThat(comments.getFieldComments().entrySet(), empty());
} }
@ -47,9 +46,9 @@ public class CommentsTest {
Comments comments = Comments.ofClass(A.class); Comments comments = Comments.ofClass(A.class);
assertThat(comments.getClassComments(), empty()); assertThat(comments.getClassComments(), empty());
assertThat(comments.getFieldComments(), is(Map.of( assertThat(comments.getFieldComments(), is(mapOf(
"b", List.of("b"), "b", listOf("b"),
"c", List.of("c", "d") "c", listOf("c", "d")
))); )));
} }
} }

@ -12,6 +12,9 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import static de.exlll.configlib.FieldMapperHelpers.*; import static de.exlll.configlib.FieldMapperHelpers.*;
import static de.exlll.configlib.util.CollectionFactory.listOf;
import static de.exlll.configlib.util.CollectionFactory.mapOf;
import static de.exlll.configlib.util.CollectionFactory.setOf;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@ -39,7 +42,7 @@ public class FieldMapperConverterTest {
implements Converter<Point2D, List<Integer>> { implements Converter<Point2D, List<Integer>> {
@Override @Override
public List<Integer> convertTo(Point2D element, ConversionInfo info) { public List<Integer> convertTo(Point2D element, ConversionInfo info) {
return List.of(element.x, element.y); return listOf(element.x, element.y);
} }
@Override @Override
@ -57,7 +60,7 @@ public class FieldMapperConverterTest {
public Map<String, String> convertTo(Point2D element, ConversionInfo info) { public Map<String, String> convertTo(Point2D element, ConversionInfo info) {
int x = element.x; int x = element.x;
int y = element.y; int y = element.y;
return Map.of("p", x + ":" + y); return mapOf("p", x + ":" + y);
} }
@Override @Override
@ -153,7 +156,7 @@ public class FieldMapperConverterTest {
@Convert(NullConverter.class) @Convert(NullConverter.class)
String s = "string"; String s = "string";
} }
Map<String, Object> map = Map.of("s", "value"); Map<String, Object> map = mapOf("s", "value");
A a = instanceFromMap(new A(), map); A a = instanceFromMap(new A(), map);
assertThat(a.s, is("string")); assertThat(a.s, is("string"));
} }
@ -190,7 +193,7 @@ public class FieldMapperConverterTest {
@Convert(IntToStringConverter.class) @Convert(IntToStringConverter.class)
int i = 1; int i = 1;
} }
A i = instanceFromMap(new A(), Map.of("i", "10")); A i = instanceFromMap(new A(), mapOf("i", "10"));
assertThat(i.i, is(10)); assertThat(i.i, is(10));
} }
@ -203,8 +206,8 @@ public class FieldMapperConverterTest {
Point2D p2 = new Point2D(); Point2D p2 = new Point2D();
} }
Map<String, Object> map = instanceToMap(new A()); Map<String, Object> map = instanceToMap(new A());
assertThat(map.get("p1"), is(List.of(1, 2))); assertThat(map.get("p1"), is(listOf(1, 2)));
assertThat(map.get("p2"), is(Map.of("p", "1:2"))); assertThat(map.get("p2"), is(mapOf("p", "1:2")));
} }
@Test @Test
@ -215,9 +218,9 @@ public class FieldMapperConverterTest {
@Convert(PointToMapConverter.class) @Convert(PointToMapConverter.class)
Point2D p2 = new Point2D(); Point2D p2 = new Point2D();
} }
Map<String, Object> map = Map.of( Map<String, Object> map = mapOf(
"p1", List.of(10, 11), "p1", listOf(10, 11),
"p2", Map.of("p", "11:12") "p2", mapOf("p", "11:12")
); );
A i = instanceFromMap(new A(), map); A i = instanceFromMap(new A(), map);
assertThat(i.p1.x, is(10)); assertThat(i.p1.x, is(10));
@ -277,7 +280,7 @@ public class FieldMapperConverterTest {
@Convert(TestSubClassConverter.class) @Convert(TestSubClassConverter.class)
TestSubClass a = new TestSubClass(); TestSubClass a = new TestSubClass();
} }
Map<String, Object> map = Map.of( Map<String, Object> map = mapOf(
"a", 1 "a", 1
); );
String msg = "The value for field 'a' with type 'TestSubClass' " + String msg = "The value for field 'a' with type 'TestSubClass' " +
@ -294,7 +297,7 @@ public class FieldMapperConverterTest {
class D { class D {
char d; char d;
} }
Map<String, Object> map = Map.of( Map<String, Object> map = mapOf(
"c", "", "d", "12" "c", "", "d", "12"
); );
String msg = "The value for field 'c' with type 'char' " + String msg = "The value for field 'c' with type 'char' " +
@ -313,7 +316,7 @@ public class FieldMapperConverterTest {
class B { class B {
String b = "string"; String b = "string";
} }
Map<String, Object> map = Map.of( Map<String, Object> map = mapOf(
"b", 2 "b", 2
); );
String msg = "The value for field 'b' with type 'String' " + String msg = "The value for field 'b' with type 'String' " +
@ -327,7 +330,7 @@ public class FieldMapperConverterTest {
class A { class A {
LocalTestEnum e = LocalTestEnum.T; LocalTestEnum e = LocalTestEnum.T;
} }
Map<String, Object> map = Map.of( Map<String, Object> map = mapOf(
"e", "V" "e", "V"
); );
String msg = "The value for field 'e' with type 'LocalTestEnum' " + String msg = "The value for field 'e' with type 'LocalTestEnum' " +
@ -340,10 +343,10 @@ public class FieldMapperConverterTest {
void instanceFromMapCatchesClassCastExceptionOfUnknownEnumConstantsInLists() { void instanceFromMapCatchesClassCastExceptionOfUnknownEnumConstantsInLists() {
class A { class A {
@ElementType(LocalTestEnum.class) @ElementType(LocalTestEnum.class)
List<List<LocalTestEnum>> l = List.of(); List<List<LocalTestEnum>> l = listOf();
} }
Map<String, Object> map = Map.of( Map<String, Object> map = mapOf(
"l", List.of(List.of("Q", "V")) "l", listOf(listOf("Q", "V"))
); );
ConfigurationException ex = assertIfmThrowsCfgException(new A(), map); ConfigurationException ex = assertIfmThrowsCfgException(new A(), map);
Throwable cause = ex.getCause(); Throwable cause = ex.getCause();
@ -357,10 +360,10 @@ public class FieldMapperConverterTest {
void instanceFromMapCatchesClassCastExceptionOfUnknownEnumConstantsInSets() { void instanceFromMapCatchesClassCastExceptionOfUnknownEnumConstantsInSets() {
class A { class A {
@ElementType(LocalTestEnum.class) @ElementType(LocalTestEnum.class)
Set<List<LocalTestEnum>> s = Set.of(); Set<List<LocalTestEnum>> s = setOf();
} }
Map<String, Object> map = Map.of( Map<String, Object> map = mapOf(
"s", Set.of(List.of("Q", "V")) "s", setOf(listOf("Q", "V"))
); );
ConfigurationException ex = assertIfmThrowsCfgException(new A(), map); ConfigurationException ex = assertIfmThrowsCfgException(new A(), map);
Throwable cause = ex.getCause(); Throwable cause = ex.getCause();
@ -374,10 +377,10 @@ public class FieldMapperConverterTest {
void instanceFromMapCatchesClassCastExceptionOfUnknownEnumConstantsInMaps() { void instanceFromMapCatchesClassCastExceptionOfUnknownEnumConstantsInMaps() {
class A { class A {
@ElementType(LocalTestEnum.class) @ElementType(LocalTestEnum.class)
Map<Integer, List<LocalTestEnum>> m = Map.of(); Map<Integer, List<LocalTestEnum>> m = mapOf();
} }
Map<String, Object> map = Map.of( Map<String, Object> map = mapOf(
"m", Map.of(1, List.of("Q", "V")) "m", mapOf(1, listOf("Q", "V"))
); );
ConfigurationException ex = assertIfmThrowsCfgException(new A(), map); ConfigurationException ex = assertIfmThrowsCfgException(new A(), map);
Throwable cause = ex.getCause(); Throwable cause = ex.getCause();

@ -23,6 +23,7 @@ import java.util.function.Predicate;
import static de.exlll.configlib.Converters.ENUM_CONVERTER; import static de.exlll.configlib.Converters.ENUM_CONVERTER;
import static de.exlll.configlib.Converters.SIMPLE_TYPE_CONVERTER; import static de.exlll.configlib.Converters.SIMPLE_TYPE_CONVERTER;
import static de.exlll.configlib.FieldMapperHelpers.*; import static de.exlll.configlib.FieldMapperHelpers.*;
import static de.exlll.configlib.util.CollectionFactory.*;
import static java.util.stream.Collectors.*; import static java.util.stream.Collectors.*;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
@ -108,7 +109,7 @@ class FieldMapperTest {
@Test @Test
void instanceFromMapDoesNotSetFinalStaticOrTransientFields() { void instanceFromMapDoesNotSetFinalStaticOrTransientFields() {
Map<String, Object> map = Map.of( Map<String, Object> map = mapOf(
"staticFinalInt", 10, "staticFinalInt", 10,
"staticInt", 10, "staticInt", 10,
"finalInt", 10, "finalInt", 10,
@ -131,7 +132,7 @@ class FieldMapperTest {
class A { class A {
LocalTestEnum t = LocalTestEnum.T; LocalTestEnum t = LocalTestEnum.T;
} }
Map<String, Object> map = Map.of( Map<String, Object> map = mapOf(
"t", "R" "t", "R"
); );
@ -150,7 +151,7 @@ class FieldMapperTest {
class A { class A {
TestSubClass c = new TestSubClass(); TestSubClass c = new TestSubClass();
} }
Map<String, Object> map = Map.of("c", Map.of("primInt", 20)); Map<String, Object> map = mapOf("c", mapOf("primInt", 20));
A a = new A(); A a = new A();
assertThat(a.c.getPrimInt(), is(0)); assertThat(a.c.getPrimInt(), is(0));
@ -177,8 +178,8 @@ class FieldMapperTest {
class B { class B {
LocalTestAbstractClass l = new LocalTestAbstractClassImpl(); LocalTestAbstractClass l = new LocalTestAbstractClassImpl();
} }
instanceFromMap(new A(), Map.of("l", Map.of())); instanceFromMap(new A(), mapOf("l", mapOf()));
instanceFromMap(new B(), Map.of("l", Map.of())); instanceFromMap(new B(), mapOf("l", mapOf()));
} }
@ -255,39 +256,39 @@ class FieldMapperTest {
void instanceToMapConvertsEnumsContainersToStringContainers() { void instanceToMapConvertsEnumsContainersToStringContainers() {
class A { class A {
@ElementType(LocalTestEnum.class) @ElementType(LocalTestEnum.class)
List<LocalTestEnum> el = List.of(LocalTestEnum.S, LocalTestEnum.T); List<LocalTestEnum> el = listOf(LocalTestEnum.S, LocalTestEnum.T);
@ElementType(LocalTestEnum.class) @ElementType(LocalTestEnum.class)
Set<LocalTestEnum> es = Set.of(LocalTestEnum.S, LocalTestEnum.T); Set<LocalTestEnum> es = setOf(LocalTestEnum.S, LocalTestEnum.T);
@ElementType(LocalTestEnum.class) @ElementType(LocalTestEnum.class)
Map<String, LocalTestEnum> em = Map.of( Map<String, LocalTestEnum> em = mapOf(
"1", LocalTestEnum.S, "2", LocalTestEnum.T "1", LocalTestEnum.S, "2", LocalTestEnum.T
); );
} }
Map<String, Object> map = instanceToMap(new A()); Map<String, Object> map = instanceToMap(new A());
assertThat(map.get("el"), is(List.of("S", "T"))); assertThat(map.get("el"), is(listOf("S", "T")));
assertThat(map.get("es"), is(Set.of("S", "T"))); assertThat(map.get("es"), is(setOf("S", "T")));
assertThat(map.get("em"), is(Map.of("1", "S", "2", "T"))); assertThat(map.get("em"), is(mapOf("1", "S", "2", "T")));
} }
@Test @Test
void instanceFromMapConvertsStringContainersToEnumContainers() { void instanceFromMapConvertsStringContainersToEnumContainers() {
class A { class A {
@ElementType(LocalTestEnum.class) @ElementType(LocalTestEnum.class)
List<LocalTestEnum> el = List.of(); List<LocalTestEnum> el = listOf();
@ElementType(LocalTestEnum.class) @ElementType(LocalTestEnum.class)
Set<LocalTestEnum> es = Set.of(); Set<LocalTestEnum> es = setOf();
@ElementType(LocalTestEnum.class) @ElementType(LocalTestEnum.class)
Map<String, LocalTestEnum> em = Map.of(); Map<String, LocalTestEnum> em = mapOf();
} }
Map<String, Object> map = Map.of( Map<String, Object> map = mapOf(
"el", List.of("S", "T"), "el", listOf("S", "T"),
"es", Set.of("S", "T"), "es", setOf("S", "T"),
"em", Map.of("1", "S", "2", "T") "em", mapOf("1", "S", "2", "T")
); );
A a = instanceFromMap(new A(), map); A a = instanceFromMap(new A(), map);
assertThat(a.el, is(List.of(LocalTestEnum.S, LocalTestEnum.T))); assertThat(a.el, is(listOf(LocalTestEnum.S, LocalTestEnum.T)));
assertThat(a.es, is(Set.of(LocalTestEnum.S, LocalTestEnum.T))); assertThat(a.es, is(setOf(LocalTestEnum.S, LocalTestEnum.T)));
assertThat(a.em, is(Map.of( assertThat(a.em, is(mapOf(
"1", LocalTestEnum.S, "2", LocalTestEnum.T "1", LocalTestEnum.S, "2", LocalTestEnum.T
))); )));
} }
@ -303,7 +304,7 @@ class FieldMapperTest {
.collect(toSet()); .collect(toSet());
Map<String, Map<String, Object>> subClassMap = t.getSubClassMap() Map<String, Map<String, Object>> subClassMap = t.getSubClassMap()
.entrySet().stream() .entrySet().stream()
.map(e -> Map.entry(e.getKey(), e.getValue().asMap())) .map(e -> mapEntry(e.getKey(), e.getValue().asMap()))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue)); .collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
assertAll( assertAll(
() -> assertThat(map.get("subClassSet"), is(subClassSet)), () -> assertThat(map.get("subClassSet"), is(subClassSet)),
@ -339,7 +340,7 @@ class FieldMapperTest {
); );
Map<Integer, Map<String, Map<String, Object>>> m = t.getSubClassMapsMap() Map<Integer, Map<String, Map<String, Object>>> m = t.getSubClassMapsMap()
.entrySet().stream() .entrySet().stream()
.map(e -> Map.entry(e.getKey(), f.apply(e.getValue()))) .map(e -> mapEntry(e.getKey(), f.apply(e.getValue())))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue)); .collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
assertThat(map.get("subClassListsList"), is(lists)); assertThat(map.get("subClassListsList"), is(lists));
@ -515,7 +516,7 @@ class FieldMapperTest {
} }
ExcludedClass cls = new ExcludedClass(); ExcludedClass cls = new ExcludedClass();
Map<String, Object> map = Map.of("ex", cls); Map<String, Object> map = mapOf("ex", cls);
A a = instanceFromMap(new A(), map); A a = instanceFromMap(new A(), map);
assertThat(a.ex, sameInstance(cls)); assertThat(a.ex, sameInstance(cls));
} }

@ -9,11 +9,12 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import static de.exlll.configlib.util.CollectionFactory.setOf;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
class ReflectTest { class ReflectTest {
private static final Set<Class<?>> ALL_SIMPLE_TYPES = Set.of( private static final Set<Class<?>> ALL_SIMPLE_TYPES = setOf(
boolean.class, Boolean.class, boolean.class, Boolean.class,
byte.class, Byte.class, byte.class, Byte.class,
char.class, Character.class, char.class, Character.class,

@ -12,6 +12,9 @@ import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import static de.exlll.configlib.FieldMapperHelpers.*; import static de.exlll.configlib.FieldMapperHelpers.*;
import static de.exlll.configlib.util.CollectionFactory.listOf;
import static de.exlll.configlib.util.CollectionFactory.mapOf;
import static de.exlll.configlib.util.CollectionFactory.setOf;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
@ -23,7 +26,7 @@ public class ValidatorTest {
TestSubClass c = new TestSubClass(); TestSubClass c = new TestSubClass();
} }
Map<String, Object> map = Map.of( Map<String, Object> map = mapOf(
"c", "s" "c", "s"
); );
String msg = "Initializing field 'c' requires a Map<String, Object> " + String msg = "Initializing field 'c' requires a Map<String, Object> " +
@ -37,7 +40,7 @@ public class ValidatorTest {
class A { class A {
LocalTestEnum t = LocalTestEnum.T; LocalTestEnum t = LocalTestEnum.T;
} }
Map<String, Object> map = Map.of( Map<String, Object> map = mapOf(
"t", 1 "t", 1
); );
String msg = "Initializing enum 't' requires a string but '1' is of " + String msg = "Initializing enum 't' requires a string but '1' is of " +
@ -51,8 +54,8 @@ public class ValidatorTest {
TestSubClass c = new TestSubClass(); TestSubClass c = new TestSubClass();
} }
Map<String, Object> map = Map.of( Map<String, Object> map = mapOf(
"c", Map.of(1, 200, "string", "s") "c", mapOf(1, 200, "string", "s")
); );
String msg = "Initializing field 'c' requires a Map<String, Object> " + String msg = "Initializing field 'c' requires a Map<String, Object> " +
"but the given map contains non-string keys." + "but the given map contains non-string keys." +
@ -68,7 +71,7 @@ public class ValidatorTest {
} }
Map<String, Object> m1 = new HashMap<>(); Map<String, Object> m1 = new HashMap<>();
m1.put(null, "null"); m1.put(null, "null");
Map<String, Object> m2 = Map.of("c", m1); Map<String, Object> m2 = mapOf("c", m1);
String msg = "Initializing field 'c' requires a Map<String, Object> " + String msg = "Initializing field 'c' requires a Map<String, Object> " +
"but the given map contains non-string keys." + "but the given map contains non-string keys." +
"\nAll entries: {null=null}"; "\nAll entries: {null=null}";
@ -80,7 +83,7 @@ public class ValidatorTest {
class A { class A {
Sub3 s = new Sub3(1); Sub3 s = new Sub3(1);
} }
Map<String, Object> map = Map.of("s", Map.of()); Map<String, Object> map = mapOf("s", mapOf());
String msg = "Type 'Sub3' of field 's' doesn't have a no-args constructor."; String msg = "Type 'Sub3' of field 's' doesn't have a no-args constructor.";
assertIfmCfgExceptionMessage(new A(), map, msg); assertIfmCfgExceptionMessage(new A(), map, msg);
} }
@ -90,7 +93,7 @@ public class ValidatorTest {
class A { class A {
Sub1 s = new Sub1(); Sub1 s = new Sub1();
} }
Map<String, Object> map = Map.of("s", Map.of()); Map<String, Object> map = mapOf("s", mapOf());
String msg = "Type 'Sub1' of field 's' is not annotated " + String msg = "Type 'Sub1' of field 's' is not annotated " +
"as a configuration element."; "as a configuration element.";
assertIfmCfgExceptionMessage(new A(), map, msg); assertIfmCfgExceptionMessage(new A(), map, msg);
@ -107,17 +110,17 @@ public class ValidatorTest {
class C { class C {
ConcurrentHashMap<?, ?> m = new ConcurrentHashMap<>(); ConcurrentHashMap<?, ?> m = new ConcurrentHashMap<>();
} }
Map<String, Object> m = Map.of("l", List.of("s")); Map<String, Object> m = mapOf("l", listOf("s"));
String msg = "Can not set field 'l' with type 'CopyOnWriteArrayList' " + String msg = "Can not set field 'l' with type 'CopyOnWriteArrayList' " +
"to 'List1'."; "to 'List1'.";
assertIfmCfgExceptionMessage(new A(), m, msg); assertIfmCfgExceptionMessage(new A(), m, msg);
m = Map.of("s", Set.of("s")); m = mapOf("s", setOf("s"));
msg = "Can not set field 's' with type 'ConcurrentSkipListSet' " + msg = "Can not set field 's' with type 'ConcurrentSkipListSet' " +
"to 'Set1'."; "to 'Set1'.";
assertIfmCfgExceptionMessage(new B(), m, msg); assertIfmCfgExceptionMessage(new B(), m, msg);
m = Map.of("m", Map.of(1, "s")); m = mapOf("m", mapOf(1, "s"));
msg = "Can not set field 'm' with type 'ConcurrentHashMap' " + msg = "Can not set field 'm' with type 'ConcurrentHashMap' " +
"to 'Map1'."; "to 'Map1'.";
assertIfmCfgExceptionMessage(new C(), m, msg); assertIfmCfgExceptionMessage(new C(), m, msg);
@ -139,7 +142,7 @@ public class ValidatorTest {
class A { class A {
String string; String string;
} }
Map<String, Object> map = Map.of("string", "s"); Map<String, Object> map = mapOf("string", "s");
String msg = "The value of field 'string' is null.\n" + String msg = "The value of field 'string' is null.\n" +
"Please assign a non-null default value or remove this field."; "Please assign a non-null default value or remove this field.";
assertIfmCfgExceptionMessage(new A(), map, msg); assertIfmCfgExceptionMessage(new A(), map, msg);
@ -148,13 +151,13 @@ public class ValidatorTest {
@Test @Test
void instanceToMapRequiresListsWithoutElementTypeToContainSimpleTypes() { void instanceToMapRequiresListsWithoutElementTypeToContainSimpleTypes() {
class A { class A {
List<TestSubClass> l = new ArrayList<>(List.of( List<TestSubClass> l = new ArrayList<>(listOf(
TestSubClass.of(1, "1") TestSubClass.of(1, "1")
)); ));
} }
class B { class B {
List<Set<Map<Integer, TestSubClass>>> l = new ArrayList<>(List.of( List<Set<Map<Integer, TestSubClass>>> l = new ArrayList<>(listOf(
Set.of(Map.of(1, TestSubClass.of(1, "1"))) setOf(mapOf(1, TestSubClass.of(1, "1")))
)); ));
} }
@ -174,13 +177,13 @@ public class ValidatorTest {
@Test @Test
void instanceToMapRequiresSetsWithoutElementTypeToContainSimpleTypes() { void instanceToMapRequiresSetsWithoutElementTypeToContainSimpleTypes() {
class A { class A {
Set<TestSubClass> s = new HashSet<>(Set.of( Set<TestSubClass> s = new HashSet<>(setOf(
TestSubClass.of(1, "1") TestSubClass.of(1, "1")
)); ));
} }
class B { class B {
Set<List<Map<Integer, TestSubClass>>> s = new HashSet<>(Set.of( Set<List<Map<Integer, TestSubClass>>> s = new HashSet<>(setOf(
List.of(Map.of(1, TestSubClass.of(1, "1"))) listOf(mapOf(1, TestSubClass.of(1, "1")))
)); ));
} }
@ -200,13 +203,13 @@ public class ValidatorTest {
@Test @Test
void instanceToMapRequiresMapsWithoutElementTypeToContainSimpleTypes() { void instanceToMapRequiresMapsWithoutElementTypeToContainSimpleTypes() {
class A { class A {
Map<Integer, TestSubClass> m = new HashMap<>(Map.of( Map<Integer, TestSubClass> m = new HashMap<>(mapOf(
1, TestSubClass.of(1, "1") 1, TestSubClass.of(1, "1")
)); ));
} }
class B { class B {
Map<Integer, Set<List<TestSubClass>>> m = new HashMap<>(Map.of( Map<Integer, Set<List<TestSubClass>>> m = new HashMap<>(mapOf(
1, Set.of(List.of(TestSubClass.of(1, "1"))) 1, setOf(listOf(TestSubClass.of(1, "1")))
)); ));
} }
@ -395,7 +398,7 @@ public class ValidatorTest {
Sub2 s = new Sub2(); Sub2 s = new Sub2();
} }
Map<String, Object> map = Map.of("s", Collections.emptyMap()); Map<String, Object> map = mapOf("s", Collections.emptyMap());
assertThat(instanceToMap(new B()), is(map)); assertThat(instanceToMap(new B()), is(map));
@ -426,7 +429,7 @@ public class ValidatorTest {
@ElementType(LocalTestEnum.class) @ElementType(LocalTestEnum.class)
List<LocalTestEnum> l = new ArrayList<>(); List<LocalTestEnum> l = new ArrayList<>();
} }
Map<String, Object> m = Map.of("l", List.of()); Map<String, Object> m = mapOf("l", listOf());
String msg = "The element type of field 'l' must be a concrete class " + String msg = "The element type of field 'l' must be a concrete class " +
"but type 'LocalTestInterface' is an interface."; "but type 'LocalTestInterface' is an interface.";
@ -494,17 +497,17 @@ public class ValidatorTest {
String msg = "Field 's' is annotated with the ElementType annotation but " + String msg = "Field 's' is annotated with the ElementType annotation but " +
"is not a List, Set or Map."; "is not a List, Set or Map.";
assertItmCfgExceptionMessage(new A(), msg); assertItmCfgExceptionMessage(new A(), msg);
assertIfmCfgExceptionMessage(new A(), Map.of("s", ""), msg); assertIfmCfgExceptionMessage(new A(), mapOf("s", ""), msg);
} }
@Test @Test
void instanceFromMapsRequiresElementTypeToBeEnumType() { void instanceFromMapsRequiresElementTypeToBeEnumType() {
class A { class A {
@ElementType(TestSubClass.class) @ElementType(TestSubClass.class)
List<List<TestSubClass>> l = List.of(); List<List<TestSubClass>> l = listOf();
} }
Map<String, Object> map = Map.of( Map<String, Object> map = mapOf(
"l", List.of(List.of("Q", "V")) "l", listOf(listOf("Q", "V"))
); );
ConfigurationException ex = assertIfmThrowsCfgException(new A(), map); ConfigurationException ex = assertIfmThrowsCfgException(new A(), map);
Throwable cause = ex.getCause(); Throwable cause = ex.getCause();
@ -517,10 +520,10 @@ public class ValidatorTest {
void instanceFromMapElementConverterRequiresObjectsOfTypeMapStringObject() { void instanceFromMapElementConverterRequiresObjectsOfTypeMapStringObject() {
class A { class A {
@ElementType(TestSubClass.class) @ElementType(TestSubClass.class)
List<List<TestSubClass>> l = List.of(); List<List<TestSubClass>> l = listOf();
} }
Map<String, Object> map = Map.of( Map<String, Object> map = mapOf(
"l", List.of(List.of(1, 2)) "l", listOf(listOf(1, 2))
); );
ConfigurationException ex = assertIfmThrowsCfgException(new A(), map); ConfigurationException ex = assertIfmThrowsCfgException(new A(), map);
Throwable cause = ex.getCause(); Throwable cause = ex.getCause();

@ -10,6 +10,8 @@ import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.*; import java.util.*;
import static de.exlll.configlib.util.CollectionFactory.listOf;
import static de.exlll.configlib.util.CollectionFactory.mapOf;
import static java.util.stream.Collectors.toCollection; import static java.util.stream.Collectors.toCollection;
@SuppressWarnings("FieldCanBeLocal") @SuppressWarnings("FieldCanBeLocal")
@ -44,13 +46,13 @@ public final class TestClass extends YamlConfiguration {
TEST_VALUES.subClass = TestSubClass.TEST_VALUES; TEST_VALUES.subClass = TestSubClass.TEST_VALUES;
/* containers of simple types */ /* containers of simple types */
TEST_VALUES.ints = linkedHashSetOf(1, 2, 3); TEST_VALUES.ints = linkedHashSetOf(1, 2, 3);
TEST_VALUES.strings = List.of("a", "b", "c"); TEST_VALUES.strings = listOf("a", "b", "c");
TEST_VALUES.doubleByBool = linkedHashMap(true, 1.0, false, 2.0); TEST_VALUES.doubleByBool = linkedHashMap(true, 1.0, false, 2.0);
/* containers of other types */ /* containers of other types */
TEST_VALUES.subClassSet = linkedHashSetOf( TEST_VALUES.subClassSet = linkedHashSetOf(
TestSubClass.of(1, "1"), TestSubClass.of(2, "2") TestSubClass.of(1, "1"), TestSubClass.of(2, "2")
); );
TEST_VALUES.subClassList = List.of( TEST_VALUES.subClassList = listOf(
TestSubClass.of(1, "1"), TestSubClass.of(2, "2") TestSubClass.of(1, "1"), TestSubClass.of(2, "2")
); );
TEST_VALUES.subClassMap = linkedHashMap( TEST_VALUES.subClassMap = linkedHashMap(
@ -58,28 +60,28 @@ public final class TestClass extends YamlConfiguration {
"2", TestSubClass.of(2, "2") "2", TestSubClass.of(2, "2")
); );
/* nested containers of simple types */ /* nested containers of simple types */
TEST_VALUES.listsList = List.of( TEST_VALUES.listsList = listOf(
List.of(1, 2), List.of(3, 4) listOf(1, 2), listOf(3, 4)
); );
TEST_VALUES.setsSet = linkedHashSetOf( TEST_VALUES.setsSet = linkedHashSetOf(
linkedHashSetOf("a", "b"), linkedHashSetOf("c", "d") linkedHashSetOf("a", "b"), linkedHashSetOf("c", "d")
); );
TEST_VALUES.mapsMap = linkedHashMap( TEST_VALUES.mapsMap = linkedHashMap(
1, Map.of("1", 1), 2, Map.of("2", 2) 1, mapOf("1", 1), 2, mapOf("2", 2)
); );
/* nested containers of custom types */ /* nested containers of custom types */
TEST_VALUES.subClassListsList = List.of( TEST_VALUES.subClassListsList = listOf(
List.of(TestSubClass.of(1, "1"), TestSubClass.of(2, "2")) listOf(TestSubClass.of(1, "1"), TestSubClass.of(2, "2"))
); );
TEST_VALUES.subClassSetsSet = linkedHashSetOf(linkedHashSetOf( TEST_VALUES.subClassSetsSet = linkedHashSetOf(linkedHashSetOf(
TestSubClass.of(1, "1"), TestSubClass.of(2, "2") TestSubClass.of(1, "1"), TestSubClass.of(2, "2")
)); ));
TEST_VALUES.subClassMapsMap = linkedHashMap( TEST_VALUES.subClassMapsMap = linkedHashMap(
1, Map.of("1", TestSubClass.of(1, "2")), 1, mapOf("1", TestSubClass.of(1, "2")),
2, Map.of("2", TestSubClass.of(2, "2")) 2, mapOf("2", TestSubClass.of(2, "2"))
); );
TEST_VALUES.e1 = TestEnum.NON_DEFAULT; TEST_VALUES.e1 = TestEnum.NON_DEFAULT;
TEST_VALUES.enums = List.of(TestEnum.DEFAULT, TestEnum.NON_DEFAULT); TEST_VALUES.enums = listOf(TestEnum.DEFAULT, TestEnum.NON_DEFAULT);
TEST_VALUES.converterSubClass = TestSubClass.of(2, "2"); TEST_VALUES.converterSubClass = TestSubClass.of(2, "2");
TEST_VALUES.excludedClass = TestExcludedClass.TEST_VALUES; TEST_VALUES.excludedClass = TestExcludedClass.TEST_VALUES;
} }

@ -4,6 +4,8 @@ import de.exlll.configlib.annotation.ConfigurationElement;
import java.util.Map; import java.util.Map;
import static de.exlll.configlib.util.CollectionFactory.mapOf;
@SuppressWarnings("FieldCanBeLocal") @SuppressWarnings("FieldCanBeLocal")
@ConfigurationElement @ConfigurationElement
public final class TestSubClass { public final class TestSubClass {
@ -27,7 +29,7 @@ public final class TestSubClass {
} }
public Map<String, Object> asMap() { public Map<String, Object> asMap() {
return Map.of("primInt", primInt, "string", string); return mapOf("primInt", primInt, "string", string);
} }
public int getFinalInt() { public int getFinalInt() {

@ -13,8 +13,8 @@ import java.io.IOException;
import java.nio.file.FileSystem; import java.nio.file.FileSystem;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.List;
import static de.exlll.configlib.util.CollectionFactory.listOf;
import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.joining;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
@ -129,8 +129,8 @@ class YamlConfigurationTest {
} }
} }
YamlProperties properties = YamlProperties.builder() YamlProperties properties = YamlProperties.builder()
.setPrependedComments(List.of("AB", "", "CD")) .setPrependedComments(listOf("AB", "", "CD"))
.setAppendedComments(List.of("AB", "", "CD")) .setAppendedComments(listOf("AB", "", "CD"))
.build(); .build();
new A(properties).save(); new A(properties).save();
assertThat(readConfig(testPath), is(PRE_AND_APPENDED_COMMENTS_YML)); assertThat(readConfig(testPath), is(PRE_AND_APPENDED_COMMENTS_YML));

@ -11,8 +11,8 @@ import java.io.IOException;
import java.nio.file.FileSystem; import java.nio.file.FileSystem;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Map;
import static de.exlll.configlib.util.CollectionFactory.mapOf;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@ -36,7 +36,7 @@ class YamlSourceTest {
YamlSource source = new YamlSource(configPath, YamlProperties.DEFAULT); YamlSource source = new YamlSource(configPath, YamlProperties.DEFAULT);
Path parentDir = configPath.getParent(); Path parentDir = configPath.getParent();
assertThat(Files.exists(parentDir), is(false)); assertThat(Files.exists(parentDir), is(false));
source.saveConfiguration(new TestClass(configPath), Map.of()); source.saveConfiguration(new TestClass(configPath), mapOf());
assertThat(Files.exists(parentDir), is(true)); assertThat(Files.exists(parentDir), is(true));
} }
} }

@ -0,0 +1,79 @@
package de.exlll.configlib.util;
import java.util.*;
public final class CollectionFactory {
private CollectionFactory() { throw new AssertionError(); }
@SafeVarargs
public static <T> List<T> listOf(T... values) {
return Arrays.asList(values);
}
@SafeVarargs
public static <T> Set<T> setOf(T... values) {
return new HashSet<>(Arrays.asList(values));
}
public static <K, V> Map<K, V> mapOf() {
return new HashMap<>();
}
public static <K, V> Map<K, V> mapOf(K k, V v) {
HashMap<K, V> map = new HashMap<>();
map.put(k, v);
return map;
}
public static <K, V> Map<K, V> mapOf(K k1, V v1, K k2, V v2) {
Map<K, V> map = mapOf(k1, v1);
map.put(k2, v2);
return map;
}
public static <K, V> Map<K, V> mapOf(K k1, V v1, K k2, V v2, K k3, V v3) {
Map<K, V> map = mapOf(k1, v1, k2, v2);
map.put(k3, v3);
return map;
}
public static <K, V> Map<K, V> mapOf(
K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4
) {
Map<K, V> map = mapOf(k1, v1, k2, v2, k3, v3);
map.put(k4, v4);
return map;
}
public static <K, V> Map.Entry<K, V> mapEntry(K k, V v) {
return new MapEntry<>(k, v);
}
private static final class MapEntry<K, V> implements Map.Entry<K, V> {
private final K key;
private V value;
public MapEntry(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
@Override
public V setValue(V value) {
V old = this.value;
this.value = value;
return old;
}
}
}

@ -430,14 +430,14 @@ public final class DatabasePlugin extends JavaPlugin {
<dependency> <dependency>
<groupId>de.exlll</groupId> <groupId>de.exlll</groupId>
<artifactId>configlib-bukkit</artifactId> <artifactId>configlib-bukkit</artifactId>
<version>2.0.0</version> <version>2.0.1</version>
</dependency> </dependency>
<!-- for Bungee plugins --> <!-- for Bungee plugins -->
<dependency> <dependency>
<groupId>de.exlll</groupId> <groupId>de.exlll</groupId>
<artifactId>configlib-bungee</artifactId> <artifactId>configlib-bungee</artifactId>
<version>2.0.0</version> <version>2.0.1</version>
</dependency> </dependency>
``` ```
#### Gradle #### Gradle
@ -449,9 +449,9 @@ repositories {
} }
dependencies { dependencies {
// for Bukkit plugins // for Bukkit plugins
compile group: 'de.exlll', name: 'configlib-bukkit', version: '2.0.0' compile group: 'de.exlll', name: 'configlib-bukkit', version: '2.0.1'
// for Bungee plugins // for Bungee plugins
compile group: 'de.exlll', name: 'configlib-bungee', version: '2.0.0' compile group: 'de.exlll', name: 'configlib-bungee', version: '2.0.1'
} }
``` ```

@ -1,10 +1,13 @@
allprojects { allprojects {
group 'de.exlll' group 'de.exlll'
version '2.0.0' version '2.0.1'
} }
subprojects { subprojects {
apply plugin: 'java' apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories { mavenCentral() } repositories { mavenCentral() }
dependencies { dependencies {

Loading…
Cancel
Save