Интернинация sbpattern

This commit is contained in:
DBotThePony 2023-02-06 18:20:57 +07:00
parent 6646c1879d
commit 6aca9d40f1
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -3,6 +3,7 @@ package ru.dbotthepony.kstarbound.util
import com.google.common.collect.ImmutableList
import com.google.common.collect.ImmutableMap
import com.google.common.collect.ImmutableSet
import com.google.common.collect.Interners
import com.google.gson.Gson
import com.google.gson.TypeAdapter
import com.google.gson.TypeAdapterFactory
@ -102,6 +103,7 @@ class SBPattern private constructor(
override fun <T : Any?> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {
if (type.rawType == SBPattern::class.java) {
return object : TypeAdapter<SBPattern>() {
private val interner = Interners.newWeakInterner<SBPattern>()
private val strings = gson.getAdapter(String::class.java)
override fun write(out: JsonWriter, value: SBPattern?) {
@ -109,7 +111,7 @@ class SBPattern private constructor(
}
override fun read(`in`: JsonReader): SBPattern? {
return of(strings.read(`in`) ?: return null)
return interner.intern(of(strings.read(`in`) ?: return null))
}
} as TypeAdapter<T>
}