Skip to content

Commit 57a9b33

Browse files
committed
Add UUID serializer
1 parent c2c35da commit 57a9b33

File tree

10 files changed

+209
-2
lines changed

10 files changed

+209
-2
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ the [Tutorial](https://github.com/Exlll/ConfigLib/wiki/Tutorial) page on the wik
1414
* Automatic creation, saving, loading, and updating of configuration files
1515
* Support for comments through annotations
1616
* Support for all primitive types, their wrapper types, and Strings
17-
* Support for `BigInteger`, `BigDecimal`, `LocalDate`, `LocalTime`, and `LocalDateTime`
17+
* Support for `BigInteger`, `BigDecimal`, `LocalDate`, `LocalTime`, `LocalDateTime`, and `UUID`
1818
* Support for (nested) lists, sets, arrays, and maps
1919
* Support for enums and POJOs (+ inheritance!)
2020
* Support for Bukkit's `ConfigurationSerializable` types (e.g. `ItemStack`)
@@ -155,6 +155,7 @@ A configuration type may only contain fields of the following types:
155155
| Characters and strings | `char`, `Character`, `String` |
156156
| Big numeric types | `BigInteger`, `BigDecimal` |
157157
| Time related types | `LocalTime`, `LocalDate`, `LocalDateTime` |
158+
| Utility types | `UUID` |
158159
| Enums | Any Java enum |
159160
| Configurations | Any configuration type |
160161
| `ConfigurationSerializable` | All Bukkit classes that implement this interface, like `ItemStack` |
@@ -172,6 +173,8 @@ public final class SupportedTypes {
172173
boolean supported;
173174
Character supported;
174175
String supported;
176+
LocalTime supported;
177+
UUID supported;
175178
ExampleEnum supported; // where 'ExampleEnum' is some Java enum type
176179
ExampleConf supported; // where 'ExampleConf' is another configuration type
177180
@@ -439,6 +442,7 @@ library knows how to handle). The conversion happens according to the following
439442
| Characters and strings | `String` |
440443
| Big numeric types | `String` |
441444
| Time related types | `String` |
445+
| Utility types | `String` |
442446
| Enums | `String` |
443447
| Configurations | `Map<String, ?>` |
444448
| `Set<S>` | `List<T>`* |

configlib-core/src/main/java/de/exlll/configlib/SerializerSelector.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.time.LocalDateTime;
1010
import java.time.LocalTime;
1111
import java.util.Map;
12+
import java.util.UUID;
1213

1314
final class SerializerSelector {
1415
private static final Map<Class<?>, Serializer<?, ?>> DEFAULT_SERIALIZERS = Map.ofEntries(
@@ -33,7 +34,8 @@ final class SerializerSelector {
3334
Map.entry(BigDecimal.class, new BigDecimalSerializer()),
3435
Map.entry(LocalDate.class, new LocalDateSerializer()),
3536
Map.entry(LocalTime.class, new LocalTimeSerializer()),
36-
Map.entry(LocalDateTime.class, new LocalDateTimeSerializer())
37+
Map.entry(LocalDateTime.class, new LocalDateTimeSerializer()),
38+
Map.entry(UUID.class, new UuidSerializer())
3739
);
3840
private final ConfigurationProperties properties;
3941

configlib-core/src/main/java/de/exlll/configlib/Serializers.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,18 @@ public LocalDateTime deserialize(String element) {
241241
}
242242
}
243243

244+
static final class UuidSerializer implements Serializer<UUID, String> {
245+
@Override
246+
public String serialize(UUID element) {
247+
return element.toString();
248+
}
249+
250+
@Override
251+
public UUID deserialize(String element) {
252+
return UUID.fromString(element);
253+
}
254+
}
255+
244256
static final class EnumSerializer implements Serializer<Enum<?>, String> {
245257
private final Class<? extends Enum<?>> cls;
246258

configlib-core/src/test/java/de/exlll/configlib/ExampleConfigurationTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ private static void assertExampleConfigurationsA1Equal(
272272
assertThat(a1_1.getA1_localDate(), is(a1_2.getA1_localDate()));
273273
assertThat(a1_1.getA1_localTime(), is(a1_2.getA1_localTime()));
274274
assertThat(a1_1.getA1_localDateTime(), is(a1_2.getA1_localDateTime()));
275+
assertThat(a1_1.getA1_uuid(), is(a1_2.getA1_uuid()));
275276
assertThat(a1_1.getA1_Enm(), is(a1_2.getA1_Enm()));
276277
assertThat(a1_1.getA1_b1(), is(a1_2.getA1_b1()));
277278
assertThat(a1_1.getA1_b2(), is(a1_2.getA1_b2()));
@@ -289,6 +290,7 @@ private static void assertExampleConfigurationsA1Equal(
289290
assertThat(a1_1.getA1_listLocalDate(), is(a1_2.getA1_listLocalDate()));
290291
assertThat(a1_1.getA1_listLocalTime(), is(a1_2.getA1_listLocalTime()));
291292
assertThat(a1_1.getA1_listLocalDateTime(), is(a1_2.getA1_listLocalDateTime()));
293+
assertThat(a1_1.getA1_listUuid(), is(a1_2.getA1_listUuid()));
292294
assertThat(a1_1.getA1_listEnm(), is(a1_2.getA1_listEnm()));
293295
assertThat(a1_1.getA1_listB1(), is(a1_2.getA1_listB1()));
294296
assertThat(a1_1.getA1_listB2(), is(a1_2.getA1_listB2()));
@@ -314,6 +316,7 @@ private static void assertExampleConfigurationsA1Equal(
314316
assertThat(a1_1.getA1_arrayLocalDate(), is(a1_2.getA1_arrayLocalDate()));
315317
assertThat(a1_1.getA1_arrayLocalTime(), is(a1_2.getA1_arrayLocalTime()));
316318
assertThat(a1_1.getA1_arrayLocalDateTime(), is(a1_2.getA1_arrayLocalDateTime()));
319+
assertThat(a1_1.getA1_arrayUuid(), is(a1_2.getA1_arrayUuid()));
317320
assertThat(a1_1.getA1_arrayEnm(), is(a1_2.getA1_arrayEnm()));
318321
assertThat(a1_1.getA1_arrayB1(), is(a1_2.getA1_arrayB1()));
319322
assertThat(a1_1.getA1_arrayB2(), is(a1_2.getA1_arrayB2()));
@@ -331,6 +334,7 @@ private static void assertExampleConfigurationsA1Equal(
331334
assertThat(a1_1.getA1_setLocalDate(), is(a1_2.getA1_setLocalDate()));
332335
assertThat(a1_1.getA1_setLocalTime(), is(a1_2.getA1_setLocalTime()));
333336
assertThat(a1_1.getA1_setLocalDateTime(), is(a1_2.getA1_setLocalDateTime()));
337+
assertThat(a1_1.getA1_setUuid(), is(a1_2.getA1_setUuid()));
334338
assertThat(a1_1.getA1_setEnm(), is(a1_2.getA1_setEnm()));
335339
assertThat(a1_1.getA1_setB1(), is(a1_2.getA1_setB1()));
336340
assertThat(a1_1.getA1_setB2(), is(a1_2.getA1_setB2()));
@@ -348,6 +352,7 @@ private static void assertExampleConfigurationsA1Equal(
348352
assertThat(a1_1.getA1_mapLocalDateLocalDate(), is(a1_2.getA1_mapLocalDateLocalDate()));
349353
assertThat(a1_1.getA1_mapLocalTimeLocalTime(), is(a1_2.getA1_mapLocalTimeLocalTime()));
350354
assertThat(a1_1.getA1_mapLocalDateTimeLocalDateTime(), is(a1_2.getA1_mapLocalDateTimeLocalDateTime()));
355+
assertThat(a1_1.getA1_mapUuidUuid(), is(a1_2.getA1_mapUuidUuid()));
351356
assertThat(a1_1.getA1_mapEnmEnm(), is(a1_2.getA1_mapEnmEnm()));
352357
assertThat(a1_1.getA1_mapIntegerB1(), is(a1_2.getA1_mapIntegerB1()));
353358
assertThat(a1_1.getA1_mapEnmB2(), is(a1_2.getA1_mapEnmB2()));
@@ -391,6 +396,7 @@ private static void assertExampleConfigurationsA1Equal(
391396
assertThat(a1_1.getA1_arrayArrayLocalDate(), is(a1_2.getA1_arrayArrayLocalDate()));
392397
assertThat(a1_1.getA1_arrayArrayLocalTime(), is(a1_2.getA1_arrayArrayLocalTime()));
393398
assertThat(a1_1.getA1_arrayArrayLocalDateTime(), is(a1_2.getA1_arrayArrayLocalDateTime()));
399+
assertThat(a1_1.getA1_arrayArrayUuid(), is(a1_2.getA1_arrayArrayUuid()));
394400
assertThat(a1_1.getA1_arrayArrayEnm(), is(a1_2.getA1_arrayArrayEnm()));
395401
assertThat(a1_1.getA1_arrayArrayB1(), is(a1_2.getA1_arrayArrayB1()));
396402
assertThat(a1_1.getA1_arrayArrayB2(), is(a1_2.getA1_arrayArrayB2()));
@@ -437,6 +443,7 @@ static void assertExampleConfigurationsA2Equal(
437443
assertThat(a2_1.getA2_localDate(), is(a2_2.getA2_localDate()));
438444
assertThat(a2_1.getA2_localTime(), is(a2_2.getA2_localTime()));
439445
assertThat(a2_1.getA2_localDateTime(), is(a2_2.getA2_localDateTime()));
446+
assertThat(a2_1.getA2_uuid(), is(a2_2.getA2_uuid()));
440447
assertThat(a2_1.getA2_Enm(), is(a2_2.getA2_Enm()));
441448
assertThat(a2_1.getA2_b1(), is(a2_2.getA2_b1()));
442449
assertThat(a2_1.getA2_b2(), is(a2_2.getA2_b2()));
@@ -454,6 +461,7 @@ static void assertExampleConfigurationsA2Equal(
454461
assertThat(a2_1.getA2_listLocalDate(), is(a2_2.getA2_listLocalDate()));
455462
assertThat(a2_1.getA2_listLocalTime(), is(a2_2.getA2_listLocalTime()));
456463
assertThat(a2_1.getA2_listLocalDateTime(), is(a2_2.getA2_listLocalDateTime()));
464+
assertThat(a2_1.getA2_listUuid(), is(a2_2.getA2_listUuid()));
457465
assertThat(a2_1.getA2_listEnm(), is(a2_2.getA2_listEnm()));
458466
assertThat(a2_1.getA2_listB1(), is(a2_2.getA2_listB1()));
459467
assertThat(a2_1.getA2_listB2(), is(a2_2.getA2_listB2()));
@@ -479,6 +487,7 @@ static void assertExampleConfigurationsA2Equal(
479487
assertThat(a2_1.getA2_arrayLocalDate(), is(a2_2.getA2_arrayLocalDate()));
480488
assertThat(a2_1.getA2_arrayLocalTime(), is(a2_2.getA2_arrayLocalTime()));
481489
assertThat(a2_1.getA2_arrayLocalDateTime(), is(a2_2.getA2_arrayLocalDateTime()));
490+
assertThat(a2_1.getA2_arrayUuid(), is(a2_2.getA2_arrayUuid()));
482491
assertThat(a2_1.getA2_arrayEnm(), is(a2_2.getA2_arrayEnm()));
483492
assertThat(a2_1.getA2_arrayB1(), is(a2_2.getA2_arrayB1()));
484493
assertThat(a2_1.getA2_arrayB2(), is(a2_2.getA2_arrayB2()));
@@ -496,6 +505,7 @@ static void assertExampleConfigurationsA2Equal(
496505
assertThat(a2_1.getA2_setLocalDate(), is(a2_2.getA2_setLocalDate()));
497506
assertThat(a2_1.getA2_setLocalTime(), is(a2_2.getA2_setLocalTime()));
498507
assertThat(a2_1.getA2_setLocalDateTime(), is(a2_2.getA2_setLocalDateTime()));
508+
assertThat(a2_1.getA2_setUuid(), is(a2_2.getA2_setUuid()));
499509
assertThat(a2_1.getA2_setEnm(), is(a2_2.getA2_setEnm()));
500510
assertThat(a2_1.getA2_setB1(), is(a2_2.getA2_setB1()));
501511
assertThat(a2_1.getA2_setB2(), is(a2_2.getA2_setB2()));
@@ -513,6 +523,7 @@ static void assertExampleConfigurationsA2Equal(
513523
assertThat(a2_1.getA2_mapLocalDateLocalDate(), is(a2_2.getA2_mapLocalDateLocalDate()));
514524
assertThat(a2_1.getA2_mapLocalTimeLocalTime(), is(a2_2.getA2_mapLocalTimeLocalTime()));
515525
assertThat(a2_1.getA2_mapLocalDateTimeLocalDateTime(), is(a2_2.getA2_mapLocalDateTimeLocalDateTime()));
526+
assertThat(a2_1.getA2_mapUuidUuid(), is(a2_2.getA2_mapUuidUuid()));
516527
assertThat(a2_1.getA2_mapEnmEnm(), is(a2_2.getA2_mapEnmEnm()));
517528
assertThat(a2_1.getA2_mapIntegerB1(), is(a2_2.getA2_mapIntegerB1()));
518529
assertThat(a2_1.getA2_mapEnmB2(), is(a2_2.getA2_mapEnmB2()));
@@ -556,6 +567,7 @@ static void assertExampleConfigurationsA2Equal(
556567
assertThat(a2_1.getA2_arrayArrayLocalDate(), is(a2_2.getA2_arrayArrayLocalDate()));
557568
assertThat(a2_1.getA2_arrayArrayLocalTime(), is(a2_2.getA2_arrayArrayLocalTime()));
558569
assertThat(a2_1.getA2_arrayArrayLocalDateTime(), is(a2_2.getA2_arrayArrayLocalDateTime()));
570+
assertThat(a2_1.getA2_arrayArrayUuid(), is(a2_2.getA2_arrayArrayUuid()));
559571
assertThat(a2_1.getA2_arrayArrayEnm(), is(a2_2.getA2_arrayArrayEnm()));
560572
assertThat(a2_1.getA2_arrayArrayB1(), is(a2_2.getA2_arrayArrayB1()));
561573
assertThat(a2_1.getA2_arrayArrayB2(), is(a2_2.getA2_arrayArrayB2()));

configlib-core/src/test/java/de/exlll/configlib/SerializerSelectorTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.List;
1818
import java.util.Map;
1919
import java.util.Set;
20+
import java.util.UUID;
2021

2122
import static de.exlll.configlib.TestUtils.*;
2223
import static org.hamcrest.MatcherAssert.assertThat;
@@ -93,6 +94,12 @@ void selectSerializerLocalDateTime() {
9394
assertThat(serializer, instanceOf(LocalDateTimeSerializer.class));
9495
}
9596

97+
@Test
98+
void selectSerializerUuid() {
99+
Serializer<?, ?> serializer = SELECTOR.select(UUID.class);
100+
assertThat(serializer, instanceOf(UuidSerializer.class));
101+
}
102+
96103
@Test
97104
void selectSerializerEnum() {
98105
enum E {}

configlib-core/src/test/java/de/exlll/configlib/SerializersTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.List;
2020
import java.util.Map;
2121
import java.util.Set;
22+
import java.util.UUID;
2223

2324
import static de.exlll.configlib.TestUtils.*;
2425
import static java.util.Arrays.asList;
@@ -428,6 +429,20 @@ void localDateTimeSerializer() {
428429
assertThat(serializer.deserialize("2000-02-29T10:11:12"), is(dateTime));
429430
}
430431

432+
@Test
433+
void uuidSerializer() {
434+
Serializer<UUID, String> serializer = new Serializers.UuidSerializer();
435+
436+
var uuid = UUID.randomUUID();
437+
var uuidString = uuid.toString();
438+
439+
assertThat(serializer.serialize(uuid), is(uuidString));
440+
441+
var deserialized = serializer.deserialize(uuidString);
442+
443+
assertThat(uuid, is(deserialized));
444+
}
445+
431446
enum E {X, Y, Z}
432447

433448
@ParameterizedTest

configlib-core/src/test/java/de/exlll/configlib/configurations/ExampleConfigurationA1.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.List;
1313
import java.util.Map;
1414
import java.util.Set;
15+
import java.util.UUID;
1516

1617
@SuppressWarnings({"FieldCanBeLocal", "FieldMayBeFinal"})
1718
@Configuration
@@ -55,6 +56,7 @@ public class ExampleConfigurationA1 {
5556
private LocalDate a1_localDate;
5657
private LocalTime a1_localTime;
5758
private LocalDateTime a1_localDateTime;
59+
private UUID a1_uuid;
5860
private ExampleEnum a1_Enm;
5961

6062
/* OTHER CONFIGURATIONS */
@@ -76,6 +78,7 @@ public class ExampleConfigurationA1 {
7678
private List<LocalDate> a1_listLocalDate;
7779
private List<LocalTime> a1_listLocalTime;
7880
private List<LocalDateTime> a1_listLocalDateTime;
81+
private List<UUID> a1_listUuid;
7982
private List<ExampleEnum> a1_listEnm;
8083
private List<ExampleConfigurationB1> a1_listB1;
8184
private List<ExampleConfigurationB2> a1_listB2;
@@ -103,6 +106,7 @@ public class ExampleConfigurationA1 {
103106
private LocalDate[] a1_arrayLocalDate;
104107
private LocalTime[] a1_arrayLocalTime;
105108
private LocalDateTime[] a1_arrayLocalDateTime;
109+
private UUID[] a1_arrayUuid;
106110
private ExampleEnum[] a1_arrayEnm;
107111
private ExampleConfigurationB1[] a1_arrayB1;
108112
private ExampleConfigurationB2[] a1_arrayB2;
@@ -122,6 +126,7 @@ public class ExampleConfigurationA1 {
122126
private Set<LocalDate> a1_setLocalDate;
123127
private Set<LocalTime> a1_setLocalTime;
124128
private Set<LocalDateTime> a1_setLocalDateTime;
129+
private Set<UUID> a1_setUuid;
125130
private Set<ExampleEnum> a1_setEnm;
126131
private Set<ExampleConfigurationB1> a1_setB1;
127132
private Set<ExampleConfigurationB2> a1_setB2;
@@ -141,6 +146,7 @@ public class ExampleConfigurationA1 {
141146
private Map<LocalDate, LocalDate> a1_mapLocalDateLocalDate;
142147
private Map<LocalTime, LocalTime> a1_mapLocalTimeLocalTime;
143148
private Map<LocalDateTime, LocalDateTime> a1_mapLocalDateTimeLocalDateTime;
149+
private Map<UUID, UUID> a1_mapUuidUuid;
144150
private Map<ExampleEnum, ExampleEnum> a1_mapEnmEnm;
145151

146152
private Map<Integer, ExampleConfigurationB1> a1_mapIntegerB1;
@@ -193,6 +199,7 @@ public class ExampleConfigurationA1 {
193199
private LocalDate[][] a1_arrayArrayLocalDate;
194200
private LocalTime[][] a1_arrayArrayLocalTime;
195201
private LocalDateTime[][] a1_arrayArrayLocalDateTime;
202+
private UUID[][] a1_arrayArrayUuid;
196203
private ExampleEnum[][] a1_arrayArrayEnm;
197204
private ExampleConfigurationB1[][] a1_arrayArrayB1;
198205
private ExampleConfigurationB2[][] a1_arrayArrayB2;
@@ -1415,4 +1422,52 @@ public Map<ExampleEnum, List<Point>> getA1_mapEnmListPoint() {
14151422
public void setA1_mapEnmListPoint(Map<ExampleEnum, List<Point>> a1_mapEnmListPoint) {
14161423
this.a1_mapEnmListPoint = a1_mapEnmListPoint;
14171424
}
1425+
1426+
public UUID getA1_uuid() {
1427+
return a1_uuid;
1428+
}
1429+
1430+
public void setA1_uuid(UUID a1_uuid) {
1431+
this.a1_uuid = a1_uuid;
1432+
}
1433+
1434+
public List<UUID> getA1_listUuid() {
1435+
return a1_listUuid;
1436+
}
1437+
1438+
public void setA1_listUuid(List<UUID> a1_listUuid) {
1439+
this.a1_listUuid = a1_listUuid;
1440+
}
1441+
1442+
public UUID[] getA1_arrayUuid() {
1443+
return a1_arrayUuid;
1444+
}
1445+
1446+
public void setA1_arrayUuid(UUID[] a1_arrayUuid) {
1447+
this.a1_arrayUuid = a1_arrayUuid;
1448+
}
1449+
1450+
public Set<UUID> getA1_setUuid() {
1451+
return a1_setUuid;
1452+
}
1453+
1454+
public void setA1_setUuid(Set<UUID> a1_setUuid) {
1455+
this.a1_setUuid = a1_setUuid;
1456+
}
1457+
1458+
public Map<UUID, UUID> getA1_mapUuidUuid() {
1459+
return a1_mapUuidUuid;
1460+
}
1461+
1462+
public void setA1_mapUuidUuid(Map<UUID, UUID> a1_mapUuidUuid) {
1463+
this.a1_mapUuidUuid = a1_mapUuidUuid;
1464+
}
1465+
1466+
public UUID[][] getA1_arrayArrayUuid() {
1467+
return a1_arrayArrayUuid;
1468+
}
1469+
1470+
public void setA1_arrayArrayUuid(UUID[][] a1_arrayArrayUuid) {
1471+
this.a1_arrayArrayUuid = a1_arrayArrayUuid;
1472+
}
14181473
}

0 commit comments

Comments
 (0)