Make ConfigLib Java 8 compatible

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

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

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

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

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

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

@ -31,7 +31,7 @@ final class YamlComments {
Map.Entry<String, List<String>> entry
) {
String fieldComments = commentListToString(entry.getValue());
return Map.entry(entry.getKey(), fieldComments);
return new MapEntry<>(entry.getKey(), fieldComments);
}
private String commentListToString(List<String> comments) {
@ -43,4 +43,32 @@ final class YamlComments {
private String toCommentLine(String 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.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@ -103,7 +104,7 @@ final class YamlSource implements ConfigurationSource<YamlConfiguration> {
private void addFieldComments() {
if (comments.hasFieldComments()) {
List<String> dumpLines = List.of(dump.split("\n"));
List<String> dumpLines = Arrays.asList(dump.split("\n"));
addDumpLines(dumpLines);
} else {
builder.append(dump);

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

@ -12,6 +12,9 @@ import java.util.Map;
import java.util.Set;
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.junit.Assert.assertThat;
@ -39,7 +42,7 @@ public class FieldMapperConverterTest {
implements Converter<Point2D, List<Integer>> {
@Override
public List<Integer> convertTo(Point2D element, ConversionInfo info) {
return List.of(element.x, element.y);
return listOf(element.x, element.y);
}
@Override
@ -57,7 +60,7 @@ public class FieldMapperConverterTest {
public Map<String, String> convertTo(Point2D element, ConversionInfo info) {
int x = element.x;
int y = element.y;
return Map.of("p", x + ":" + y);
return mapOf("p", x + ":" + y);
}
@Override
@ -153,7 +156,7 @@ public class FieldMapperConverterTest {
@Convert(NullConverter.class)
String s = "string";
}
Map<String, Object> map = Map.of("s", "value");
Map<String, Object> map = mapOf("s", "value");
A a = instanceFromMap(new A(), map);
assertThat(a.s, is("string"));
}
@ -190,7 +193,7 @@ public class FieldMapperConverterTest {
@Convert(IntToStringConverter.class)
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));
}
@ -203,8 +206,8 @@ public class FieldMapperConverterTest {
Point2D p2 = new Point2D();
}
Map<String, Object> map = instanceToMap(new A());
assertThat(map.get("p1"), is(List.of(1, 2)));
assertThat(map.get("p2"), is(Map.of("p", "1:2")));
assertThat(map.get("p1"), is(listOf(1, 2)));
assertThat(map.get("p2"), is(mapOf("p", "1:2")));
}
@Test
@ -215,9 +218,9 @@ public class FieldMapperConverterTest {
@Convert(PointToMapConverter.class)
Point2D p2 = new Point2D();
}
Map<String, Object> map = Map.of(
"p1", List.of(10, 11),
"p2", Map.of("p", "11:12")
Map<String, Object> map = mapOf(
"p1", listOf(10, 11),
"p2", mapOf("p", "11:12")
);
A i = instanceFromMap(new A(), map);
assertThat(i.p1.x, is(10));
@ -277,7 +280,7 @@ public class FieldMapperConverterTest {
@Convert(TestSubClassConverter.class)
TestSubClass a = new TestSubClass();
}
Map<String, Object> map = Map.of(
Map<String, Object> map = mapOf(
"a", 1
);
String msg = "The value for field 'a' with type 'TestSubClass' " +
@ -294,7 +297,7 @@ public class FieldMapperConverterTest {
class D {
char d;
}
Map<String, Object> map = Map.of(
Map<String, Object> map = mapOf(
"c", "", "d", "12"
);
String msg = "The value for field 'c' with type 'char' " +
@ -313,7 +316,7 @@ public class FieldMapperConverterTest {
class B {
String b = "string";
}
Map<String, Object> map = Map.of(
Map<String, Object> map = mapOf(
"b", 2
);
String msg = "The value for field 'b' with type 'String' " +
@ -327,7 +330,7 @@ public class FieldMapperConverterTest {
class A {
LocalTestEnum e = LocalTestEnum.T;
}
Map<String, Object> map = Map.of(
Map<String, Object> map = mapOf(
"e", "V"
);
String msg = "The value for field 'e' with type 'LocalTestEnum' " +
@ -340,10 +343,10 @@ public class FieldMapperConverterTest {
void instanceFromMapCatchesClassCastExceptionOfUnknownEnumConstantsInLists() {
class A {
@ElementType(LocalTestEnum.class)
List<List<LocalTestEnum>> l = List.of();
List<List<LocalTestEnum>> l = listOf();
}
Map<String, Object> map = Map.of(
"l", List.of(List.of("Q", "V"))
Map<String, Object> map = mapOf(
"l", listOf(listOf("Q", "V"))
);
ConfigurationException ex = assertIfmThrowsCfgException(new A(), map);
Throwable cause = ex.getCause();
@ -357,10 +360,10 @@ public class FieldMapperConverterTest {
void instanceFromMapCatchesClassCastExceptionOfUnknownEnumConstantsInSets() {
class A {
@ElementType(LocalTestEnum.class)
Set<List<LocalTestEnum>> s = Set.of();
Set<List<LocalTestEnum>> s = setOf();
}
Map<String, Object> map = Map.of(
"s", Set.of(List.of("Q", "V"))
Map<String, Object> map = mapOf(
"s", setOf(listOf("Q", "V"))
);
ConfigurationException ex = assertIfmThrowsCfgException(new A(), map);
Throwable cause = ex.getCause();
@ -374,10 +377,10 @@ public class FieldMapperConverterTest {
void instanceFromMapCatchesClassCastExceptionOfUnknownEnumConstantsInMaps() {
class A {
@ElementType(LocalTestEnum.class)
Map<Integer, List<LocalTestEnum>> m = Map.of();
Map<Integer, List<LocalTestEnum>> m = mapOf();
}
Map<String, Object> map = Map.of(
"m", Map.of(1, List.of("Q", "V"))
Map<String, Object> map = mapOf(
"m", mapOf(1, listOf("Q", "V"))
);
ConfigurationException ex = assertIfmThrowsCfgException(new A(), map);
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.SIMPLE_TYPE_CONVERTER;
import static de.exlll.configlib.FieldMapperHelpers.*;
import static de.exlll.configlib.util.CollectionFactory.*;
import static java.util.stream.Collectors.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
@ -108,7 +109,7 @@ class FieldMapperTest {
@Test
void instanceFromMapDoesNotSetFinalStaticOrTransientFields() {
Map<String, Object> map = Map.of(
Map<String, Object> map = mapOf(
"staticFinalInt", 10,
"staticInt", 10,
"finalInt", 10,
@ -131,7 +132,7 @@ class FieldMapperTest {
class A {
LocalTestEnum t = LocalTestEnum.T;
}
Map<String, Object> map = Map.of(
Map<String, Object> map = mapOf(
"t", "R"
);
@ -150,7 +151,7 @@ class FieldMapperTest {
class A {
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();
assertThat(a.c.getPrimInt(), is(0));
@ -177,8 +178,8 @@ class FieldMapperTest {
class B {
LocalTestAbstractClass l = new LocalTestAbstractClassImpl();
}
instanceFromMap(new A(), Map.of("l", Map.of()));
instanceFromMap(new B(), Map.of("l", Map.of()));
instanceFromMap(new A(), mapOf("l", mapOf()));
instanceFromMap(new B(), mapOf("l", mapOf()));
}
@ -255,39 +256,39 @@ class FieldMapperTest {
void instanceToMapConvertsEnumsContainersToStringContainers() {
class A {
@ElementType(LocalTestEnum.class)
List<LocalTestEnum> el = List.of(LocalTestEnum.S, LocalTestEnum.T);
List<LocalTestEnum> el = listOf(LocalTestEnum.S, LocalTestEnum.T);
@ElementType(LocalTestEnum.class)
Set<LocalTestEnum> es = Set.of(LocalTestEnum.S, LocalTestEnum.T);
Set<LocalTestEnum> es = setOf(LocalTestEnum.S, LocalTestEnum.T);
@ElementType(LocalTestEnum.class)
Map<String, LocalTestEnum> em = Map.of(
Map<String, LocalTestEnum> em = mapOf(
"1", LocalTestEnum.S, "2", LocalTestEnum.T
);
}
Map<String, Object> map = instanceToMap(new A());
assertThat(map.get("el"), is(List.of("S", "T")));
assertThat(map.get("es"), is(Set.of("S", "T")));
assertThat(map.get("em"), is(Map.of("1", "S", "2", "T")));
assertThat(map.get("el"), is(listOf("S", "T")));
assertThat(map.get("es"), is(setOf("S", "T")));
assertThat(map.get("em"), is(mapOf("1", "S", "2", "T")));
}
@Test
void instanceFromMapConvertsStringContainersToEnumContainers() {
class A {
@ElementType(LocalTestEnum.class)
List<LocalTestEnum> el = List.of();
List<LocalTestEnum> el = listOf();
@ElementType(LocalTestEnum.class)
Set<LocalTestEnum> es = Set.of();
Set<LocalTestEnum> es = setOf();
@ElementType(LocalTestEnum.class)
Map<String, LocalTestEnum> em = Map.of();
Map<String, LocalTestEnum> em = mapOf();
}
Map<String, Object> map = Map.of(
"el", List.of("S", "T"),
"es", Set.of("S", "T"),
"em", Map.of("1", "S", "2", "T")
Map<String, Object> map = mapOf(
"el", listOf("S", "T"),
"es", setOf("S", "T"),
"em", mapOf("1", "S", "2", "T")
);
A a = instanceFromMap(new A(), map);
assertThat(a.el, is(List.of(LocalTestEnum.S, LocalTestEnum.T)));
assertThat(a.es, is(Set.of(LocalTestEnum.S, LocalTestEnum.T)));
assertThat(a.em, is(Map.of(
assertThat(a.el, is(listOf(LocalTestEnum.S, LocalTestEnum.T)));
assertThat(a.es, is(setOf(LocalTestEnum.S, LocalTestEnum.T)));
assertThat(a.em, is(mapOf(
"1", LocalTestEnum.S, "2", LocalTestEnum.T
)));
}
@ -303,7 +304,7 @@ class FieldMapperTest {
.collect(toSet());
Map<String, Map<String, Object>> subClassMap = t.getSubClassMap()
.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));
assertAll(
() -> assertThat(map.get("subClassSet"), is(subClassSet)),
@ -339,7 +340,7 @@ class FieldMapperTest {
);
Map<Integer, Map<String, Map<String, Object>>> m = t.getSubClassMapsMap()
.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));
assertThat(map.get("subClassListsList"), is(lists));
@ -515,7 +516,7 @@ class FieldMapperTest {
}
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);
assertThat(a.ex, sameInstance(cls));
}

@ -9,11 +9,12 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import static de.exlll.configlib.util.CollectionFactory.setOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
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,
byte.class, Byte.class,
char.class, Character.class,

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

@ -10,6 +10,8 @@ import java.nio.file.Path;
import java.nio.file.Paths;
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;
@SuppressWarnings("FieldCanBeLocal")
@ -44,13 +46,13 @@ public final class TestClass extends YamlConfiguration {
TEST_VALUES.subClass = TestSubClass.TEST_VALUES;
/* containers of simple types */
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);
/* containers of other types */
TEST_VALUES.subClassSet = linkedHashSetOf(
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")
);
TEST_VALUES.subClassMap = linkedHashMap(
@ -58,28 +60,28 @@ public final class TestClass extends YamlConfiguration {
"2", TestSubClass.of(2, "2")
);
/* nested containers of simple types */
TEST_VALUES.listsList = List.of(
List.of(1, 2), List.of(3, 4)
TEST_VALUES.listsList = listOf(
listOf(1, 2), listOf(3, 4)
);
TEST_VALUES.setsSet = linkedHashSetOf(
linkedHashSetOf("a", "b"), linkedHashSetOf("c", "d")
);
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 */
TEST_VALUES.subClassListsList = List.of(
List.of(TestSubClass.of(1, "1"), TestSubClass.of(2, "2"))
TEST_VALUES.subClassListsList = listOf(
listOf(TestSubClass.of(1, "1"), TestSubClass.of(2, "2"))
);
TEST_VALUES.subClassSetsSet = linkedHashSetOf(linkedHashSetOf(
TestSubClass.of(1, "1"), TestSubClass.of(2, "2")
));
TEST_VALUES.subClassMapsMap = linkedHashMap(
1, Map.of("1", TestSubClass.of(1, "2")),
2, Map.of("2", TestSubClass.of(2, "2"))
1, mapOf("1", TestSubClass.of(1, "2")),
2, mapOf("2", TestSubClass.of(2, "2"))
);
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.excludedClass = TestExcludedClass.TEST_VALUES;
}

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

@ -13,8 +13,8 @@ import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
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 org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
@ -129,8 +129,8 @@ class YamlConfigurationTest {
}
}
YamlProperties properties = YamlProperties.builder()
.setPrependedComments(List.of("AB", "", "CD"))
.setAppendedComments(List.of("AB", "", "CD"))
.setPrependedComments(listOf("AB", "", "CD"))
.setAppendedComments(listOf("AB", "", "CD"))
.build();
new A(properties).save();
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.Files;
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.junit.Assert.assertThat;
@ -36,7 +36,7 @@ class YamlSourceTest {
YamlSource source = new YamlSource(configPath, YamlProperties.DEFAULT);
Path parentDir = configPath.getParent();
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));
}
}

@ -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>
<groupId>de.exlll</groupId>
<artifactId>configlib-bukkit</artifactId>
<version>2.0.0</version>
<version>2.0.1</version>
</dependency>
<!-- for Bungee plugins -->
<dependency>
<groupId>de.exlll</groupId>
<artifactId>configlib-bungee</artifactId>
<version>2.0.0</version>
<version>2.0.1</version>
</dependency>
```
#### Gradle
@ -449,9 +449,9 @@ repositories {
}
dependencies {
// 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
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 {
group 'de.exlll'
version '2.0.0'
version '2.0.1'
}
subprojects {
apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories { mavenCentral() }
dependencies {

Loading…
Cancel
Save