Skip to content

Commit ccd0ba1

Browse files
committed
Add serializer for ReferenceSet
1 parent 9a61ddb commit ccd0ba1

File tree

4 files changed

+54
-31
lines changed

4 files changed

+54
-31
lines changed

core/src/main/kotlin/io/github/rothes/esu/core/configuration/ConfigLoader.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import io.github.rothes.esu.lib.configurate.yaml.NodeStyle
2525
import io.github.rothes.esu.lib.configurate.yaml.YamlConfigurationLoader
2626
import io.leangen.geantyref.GenericTypeReflector
2727
import io.leangen.geantyref.TypeToken
28+
import it.unimi.dsi.fastutil.objects.ReferenceSet
2829
import net.kyori.adventure.text.Component
2930
import java.io.File
3031
import java.lang.reflect.Type
@@ -354,6 +355,7 @@ object ConfigLoader {
354355
GenericTypeReflector.annotate(type).isAnnotationPresent(ConfigSerializable::class.java)
355356
}, objectSerializer
356357
)
358+
.register(TypeToken.get(ReferenceSet::class.java), ReferenceSetSerializer)
357359
.register(TypeToken.get(List::class.java), ListSerializer)
358360
.register(TypeToken.get(Map::class.java), MapSerializer)
359361
.register(TypeToken.get(Optional::class.java), OptionalSerializer)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.github.rothes.esu.core.configuration.serializer
2+
3+
import io.github.rothes.esu.core.configuration.meta.NoDeserializeNull
4+
import io.github.rothes.esu.lib.configurate.ConfigurationOptions
5+
import io.github.rothes.esu.lib.configurate.serialize.AbstractListChildSerializer
6+
import io.github.rothes.esu.lib.configurate.serialize.SerializationException
7+
import io.github.rothes.esu.lib.configurate.util.CheckedConsumer
8+
import java.lang.reflect.AnnotatedParameterizedType
9+
import java.lang.reflect.AnnotatedType
10+
11+
abstract class AbstractListSerializer<T>: AbstractListChildSerializer<T>() {
12+
13+
override fun emptyValue(specificType: AnnotatedType, options: ConfigurationOptions): T? {
14+
if (specificType.isAnnotationPresent(NoDeserializeNull::class.java)) {
15+
return null
16+
}
17+
return super.emptyValue(specificType, options)
18+
}
19+
20+
override fun deserializeSingle(index: Int, collection: T, deserialized: Any?) {
21+
@Suppress("UNCHECKED_CAST") (collection as MutableCollection<Any?>).add(deserialized)
22+
}
23+
24+
override fun forEachElement(collection: T, action: CheckedConsumer<Any, SerializationException>) {
25+
@Suppress("UNCHECKED_CAST") (collection as Iterable<Any?>).forEach { action.accept(it) }
26+
}
27+
28+
override fun elementType(containerType: AnnotatedType?): AnnotatedType {
29+
if (containerType !is AnnotatedParameterizedType) {
30+
throw SerializationException(containerType, "Raw types are not supported for collections")
31+
}
32+
return containerType.annotatedActualTypeArguments[0]
33+
}
34+
35+
}
Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,11 @@
11
package io.github.rothes.esu.core.configuration.serializer
22

3-
import io.github.rothes.esu.core.configuration.meta.NoDeserializeNull
4-
import io.github.rothes.esu.lib.configurate.ConfigurationOptions
5-
import io.github.rothes.esu.lib.configurate.serialize.AbstractListChildSerializer
6-
import io.github.rothes.esu.lib.configurate.serialize.SerializationException
7-
import io.github.rothes.esu.lib.configurate.util.CheckedConsumer
8-
import java.lang.reflect.AnnotatedParameterizedType
93
import java.lang.reflect.AnnotatedType
104

11-
object ListSerializer: AbstractListChildSerializer<List<*>>() {
5+
object ListSerializer: AbstractListSerializer<List<*>>() {
126

13-
override fun emptyValue(specificType: AnnotatedType, options: ConfigurationOptions): List<*>? {
14-
if (specificType.isAnnotationPresent(NoDeserializeNull::class.java)) {
15-
return null
16-
}
17-
return super.emptyValue(specificType, options)
18-
}
19-
20-
override fun deserializeSingle(index: Int, collection: List<*>, deserialized: Any?) {
21-
@Suppress("UNCHECKED_CAST") (collection as MutableList<Any?>).add(deserialized)
22-
}
23-
24-
override fun forEachElement(collection: List<*>, action: CheckedConsumer<Any, SerializationException>) {
25-
collection.forEach { action.accept(it) }
26-
}
27-
28-
override fun elementType(containerType: AnnotatedType?): AnnotatedType {
29-
if (containerType !is AnnotatedParameterizedType) {
30-
throw SerializationException(containerType, "Raw types are not supported for collections")
31-
}
32-
return containerType.annotatedActualTypeArguments[0]
33-
}
34-
35-
override fun createNew(length: Int, elementType: AnnotatedType?): List<*> {
36-
return arrayListOf<Any>()
7+
override fun createNew(length: Int, elementType: AnnotatedType?): List<Any?> {
8+
return ArrayList(length)
379
}
3810

3911
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.github.rothes.esu.core.configuration.serializer
2+
3+
import it.unimi.dsi.fastutil.objects.ReferenceArraySet
4+
import it.unimi.dsi.fastutil.objects.ReferenceLinkedOpenHashSet
5+
import it.unimi.dsi.fastutil.objects.ReferenceSet
6+
import java.lang.reflect.AnnotatedType
7+
8+
object ReferenceSetSerializer: AbstractListSerializer<ReferenceSet<*>>() {
9+
10+
override fun createNew(length: Int, elementType: AnnotatedType?): ReferenceSet<Any> {
11+
return if (length <= 3) ReferenceArraySet(length) else ReferenceLinkedOpenHashSet(length)
12+
}
13+
14+
}

0 commit comments

Comments
 (0)