@@ -17,13 +17,12 @@ import io.github.projectmapk.jackson.module.kogera.deser.singletonSupport.Kotlin
1717import io.github.projectmapk.jackson.module.kogera.deser.valueInstantiator.KotlinInstantiators
1818import io.github.projectmapk.jackson.module.kogera.ser.serializers.KotlinKeySerializers
1919import io.github.projectmapk.jackson.module.kogera.ser.serializers.KotlinSerializers
20+ import java.io.Serializable
2021import java.util.*
2122
2223/* *
23- * @param initialCacheSize
24- * Default: [Builder.DEFAULT_CACHE_SIZE]. See [Builder.withInitialCacheSize] for details.
25- * @param maxCacheSize
26- * Default: [Builder.DEFAULT_CACHE_SIZE]. See [Builder.withMaxCacheSize] for details.
24+ * @param cacheSize
25+ * Default: [Builder.DEFAULT_CACHE_SIZE]. See [CacheSize] for details.
2726 * @param nullToEmptyCollection
2827 * Default: false. See [KotlinFeature.NullToEmptyCollection] for details.
2928 * @param nullToEmptyMap
@@ -42,8 +41,7 @@ import java.util.*
4241// Do not delete default arguments,
4342// as this will cause an error during initialization by Spring's Jackson2ObjectMapperBuilder.
4443public class KotlinModule private constructor(
45- public val initialCacheSize : Int = Builder .DEFAULT_CACHE_SIZE ,
46- public val maxCacheSize : Int = Builder .DEFAULT_CACHE_SIZE ,
44+ public val cacheSize : CacheSize = CacheSize (),
4745 public val nullToEmptyCollection : Boolean = NullToEmptyCollection .enabledByDefault,
4846 public val nullToEmptyMap : Boolean = NullToEmptyMap .enabledByDefault,
4947 public val nullIsSameAsDefault : Boolean = NullIsSameAsDefault .enabledByDefault,
@@ -54,8 +52,7 @@ public class KotlinModule private constructor(
5452 public val useJavaDurationConversion : Boolean = UseJavaDurationConversion .enabledByDefault
5553) : SimpleModule(KotlinModule : :class.java.name, kogeraVersion) { // kogeraVersion is generated by building.
5654 private constructor (builder: Builder ) : this (
57- builder.initialCacheSize,
58- builder.maxCacheSize,
55+ builder.cacheSize,
5956 builder.isEnabled(NullToEmptyCollection ),
6057 builder.isEnabled(NullToEmptyMap ),
6158 builder.isEnabled(NullIsSameAsDefault ),
@@ -71,16 +68,13 @@ public class KotlinModule private constructor(
7168 )
7269 public constructor () : this (Builder ())
7370
74- private val cache: ReflectionCache
75- private val primaryAnnotationIntrospector: KotlinPrimaryAnnotationIntrospector
76- private val fallbackAnnotationIntrospector: KotlinFallbackAnnotationIntrospector
71+ private val cache: ReflectionCache = ReflectionCache (cacheSize.initialCacheSize, cacheSize.maxCacheSize)
72+ private val primaryAnnotationIntrospector: KotlinPrimaryAnnotationIntrospector =
73+ KotlinPrimaryAnnotationIntrospector (nullToEmptyCollection, nullToEmptyMap, cache)
74+ private val fallbackAnnotationIntrospector: KotlinFallbackAnnotationIntrospector =
75+ KotlinFallbackAnnotationIntrospector (strictNullChecks, useJavaDurationConversion, cache)
7776
7877 init {
79- checkMaxCacheSize(maxCacheSize)
80- checkCacheSize(initialCacheSize, maxCacheSize)
81-
82- cache = ReflectionCache (initialCacheSize, maxCacheSize)
83-
8478 _serializers = KotlinSerializers (cache)
8579 _deserializers = KotlinDeserializers (cache, useJavaDurationConversion)
8680
@@ -96,28 +90,11 @@ public class KotlinModule private constructor(
9690 }
9791
9892 setMixInAnnotation(ClosedRange ::class .java, ClosedRangeMixin ::class .java)
99-
100- primaryAnnotationIntrospector =
101- KotlinPrimaryAnnotationIntrospector (nullToEmptyCollection, nullToEmptyMap, cache)
102- fallbackAnnotationIntrospector =
103- KotlinFallbackAnnotationIntrospector (strictNullChecks, useJavaDurationConversion, cache)
10493 }
10594
10695 public companion object {
10796 @Suppress(" ConstPropertyName" )
108- private const val serialVersionUID = 2L
109-
110- private fun checkMaxCacheSize (maxCacheSize : Int ) {
111- if (maxCacheSize < 16 ) throw IllegalArgumentException (" 16 or higher must be specified" )
112- }
113-
114- private fun checkCacheSize (initialCacheSize : Int , maxCacheSize : Int ) {
115- if (maxCacheSize < initialCacheSize) {
116- throw IllegalArgumentException (
117- " maxCacheSize($maxCacheSize ) was less than initialCacheSize($initialCacheSize )."
118- )
119- }
120- }
97+ private const val serialVersionUID = 3L
12198 }
12299
123100 override fun setupModule (context : SetupContext ) {
@@ -137,40 +114,49 @@ public class KotlinModule private constructor(
137114 }
138115 }
139116
117+ /* *
118+ * @property maxCacheSize Kogera's internal processing requires a certain cache size.
119+ * The lower limit is set to 16 for extreme use cases,
120+ * but it is recommended to set it to 100 or more unless there is a very clear reason.
121+ *
122+ * @throws IllegalArgumentException Specified [maxCacheSize] was less than the 16.
123+ * @throws IllegalArgumentException A value less than [maxCacheSize] was entered for [initialCacheSize].
124+ */
125+ public data class CacheSize (
126+ val initialCacheSize : Int = Builder .DEFAULT_CACHE_SIZE ,
127+ val maxCacheSize : Int = Builder .DEFAULT_CACHE_SIZE
128+ ) : Serializable {
129+ /* *
130+ * Set the same size for [initialCacheSize] and [maxCacheSize].
131+ */
132+ public constructor (cacheSize: Int ) : this (cacheSize, cacheSize)
133+
134+ init {
135+ if (maxCacheSize < 16 ) {
136+ throw IllegalArgumentException (
137+ " The maxCacheSize must be at least 16. The recommended value is 100 or more."
138+ )
139+ }
140+ if (maxCacheSize < initialCacheSize) {
141+ throw IllegalArgumentException (
142+ " maxCacheSize($maxCacheSize ) was less than initialCacheSize($initialCacheSize )."
143+ )
144+ }
145+ }
146+ }
147+
140148 public class Builder {
141149 public companion object {
142150 internal const val DEFAULT_CACHE_SIZE = 512
143151 }
144152
145- public var initialCacheSize: Int = DEFAULT_CACHE_SIZE
146- private set
147- public var maxCacheSize: Int = DEFAULT_CACHE_SIZE
153+ public var cacheSize: CacheSize = CacheSize ()
148154 private set
149155
150156 private val bitSet: BitSet = KotlinFeature .defaults
151157
152- /* *
153- * @throws IllegalArgumentException A value less than [maxCacheSize] was entered for [initialCacheSize].
154- */
155- public fun withInitialCacheSize (initialCacheSize : Int ): Builder = apply {
156- checkCacheSize(initialCacheSize, maxCacheSize)
157-
158- this .initialCacheSize = initialCacheSize
159- }
160-
161- /* *
162- * Kogera's internal processing requires a certain cache size.
163- * The lower limit of [maxCacheSize] is set to 16 for extreme use cases,
164- * but it is recommended to set it to 100 or more unless there is a very clear reason.
165- *
166- * @throws IllegalArgumentException Specified size was less than the 16.
167- * @throws IllegalArgumentException A value less than [initialCacheSize] was entered for maxCacheSize.
168- */
169- public fun withMaxCacheSize (maxCacheSize : Int ): Builder = apply {
170- checkMaxCacheSize(maxCacheSize)
171- checkCacheSize(initialCacheSize, maxCacheSize)
172-
173- this .maxCacheSize = maxCacheSize
158+ public fun withCacheSize (cacheSize : CacheSize ): Builder = apply {
159+ this .cacheSize = cacheSize
174160 }
175161
176162 public fun enable (feature : KotlinFeature ): Builder = apply {
0 commit comments