Skip to content

Commit 703d998

Browse files
authored
Merge pull request #176 from ProjectMapK/fix-cache-size
Consolidate cache size to one property
2 parents 1655063 + 2db55a2 commit 703d998

File tree

4 files changed

+58
-75
lines changed

4 files changed

+58
-75
lines changed

src/main/kotlin/io/github/projectmapk/jackson/module/kogera/KotlinModule.kt

Lines changed: 45 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ import io.github.projectmapk.jackson.module.kogera.deser.singletonSupport.Kotlin
1717
import io.github.projectmapk.jackson.module.kogera.deser.valueInstantiator.KotlinInstantiators
1818
import io.github.projectmapk.jackson.module.kogera.ser.serializers.KotlinKeySerializers
1919
import io.github.projectmapk.jackson.module.kogera.ser.serializers.KotlinSerializers
20+
import java.io.Serializable
2021
import 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.
4443
public 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 {

src/test/kotlin/io/github/projectmapk/jackson/module/kogera/KotlinModuleTest.kt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class KotlinModuleTest {
4545
fun `Cannot set initialCacheSize to a value larger than maxCacheSize`() {
4646
assertThrows<IllegalArgumentException> {
4747
KotlinModule.Builder().apply {
48-
withInitialCacheSize(maxCacheSize + 1)
48+
withCacheSize(KotlinModule.CacheSize(initialCacheSize = KotlinModule.Builder.DEFAULT_CACHE_SIZE + 1))
4949
}
5050
}
5151
}
@@ -54,7 +54,7 @@ class KotlinModuleTest {
5454
fun `Cannot set maxCacheSize to a value smaller than initialCacheSize`() {
5555
assertThrows<IllegalArgumentException> {
5656
KotlinModule.Builder().apply {
57-
withMaxCacheSize(initialCacheSize - 1)
57+
withCacheSize(KotlinModule.CacheSize(maxCacheSize = KotlinModule.Builder.DEFAULT_CACHE_SIZE - 1))
5858
}
5959
}
6060
}
@@ -63,8 +63,7 @@ class KotlinModuleTest {
6363
fun `Cannot set maxCacheSize to a value smaller than 15`() {
6464
assertThrows<IllegalArgumentException> {
6565
KotlinModule.Builder().apply {
66-
withInitialCacheSize(0)
67-
withMaxCacheSize(15)
66+
KotlinModule.CacheSize(0, 15)
6867
}
6968
}
7069
}
@@ -73,8 +72,7 @@ class KotlinModuleTest {
7372
fun test() {
7473
assertDoesNotThrow {
7574
KotlinModule.Builder().apply {
76-
withInitialCacheSize(0)
77-
withMaxCacheSize(16)
75+
KotlinModule.CacheSize(0, 16)
7876
}
7977
}
8078
}
@@ -84,8 +82,7 @@ class KotlinModuleTest {
8482
@ValueSource(booleans = [true, false])
8583
fun jdkSerializabilityTest(enabled: Boolean) {
8684
val module = KotlinModule.Builder().apply {
87-
withInitialCacheSize(123)
88-
withMaxCacheSize(321)
85+
withCacheSize(KotlinModule.CacheSize(123, 321))
8986

9087
KotlinFeature.values().forEach {
9188
configure(it, enabled)
@@ -98,8 +95,7 @@ class KotlinModuleTest {
9895
val deserialized = jdkDeserialize<KotlinModule>(serialized)
9996

10097
assertNotNull(deserialized)
101-
assertEquals(123, deserialized.initialCacheSize)
102-
assertEquals(321, deserialized.maxCacheSize)
98+
assertEquals(KotlinModule.CacheSize(123, 321), deserialized.cacheSize)
10399
assertEquals(module.nullToEmptyCollection, deserialized.nullToEmptyCollection)
104100
assertEquals(module.nullToEmptyMap, deserialized.nullToEmptyMap)
105101
assertEquals(module.nullIsSameAsDefault, deserialized.nullIsSameAsDefault)

src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/DslTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.github.projectmapk.jackson.module.kogera.KotlinFeature.NullToEmptyColl
77
import io.github.projectmapk.jackson.module.kogera.KotlinFeature.NullToEmptyMap
88
import io.github.projectmapk.jackson.module.kogera.KotlinFeature.SingletonSupport
99
import io.github.projectmapk.jackson.module.kogera.KotlinFeature.StrictNullChecks
10+
import io.github.projectmapk.jackson.module.kogera.KotlinModule
1011
import io.github.projectmapk.jackson.module.kogera.jsonMapper
1112
import io.github.projectmapk.jackson.module.kogera.kotlinModule
1213
import org.junit.jupiter.api.Assertions.assertEquals
@@ -32,7 +33,7 @@ class DslTest {
3233
@Test
3334
fun createModuleWithBuilderOptions() {
3435
val module = kotlinModule {
35-
withInitialCacheSize(123)
36+
withCacheSize(KotlinModule.CacheSize(123))
3637
enable(NullToEmptyCollection)
3738
enable(NullToEmptyMap)
3839
enable(NullIsSameAsDefault)
@@ -41,7 +42,7 @@ class DslTest {
4142
}
4243

4344
assertNotNull(module)
44-
assertEquals(module.initialCacheSize, 123)
45+
assertEquals(module.cacheSize, KotlinModule.CacheSize(123, 123))
4546
assertTrue(module.nullToEmptyCollection)
4647
assertTrue(module.nullToEmptyMap)
4748
assertTrue(module.nullIsSameAsDefault)

src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/KotlinModuleTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class KotlinModuleTest {
1919
fun builderDefaultsMatchFeatures() {
2020
val module = KotlinModule.Builder().build()
2121

22-
assertEquals(module.initialCacheSize, 512)
22+
assertEquals(KotlinModule.CacheSize(512), module.cacheSize)
2323
assertFalse(module.nullToEmptyCollection)
2424
assertFalse(module.nullToEmptyMap)
2525
assertFalse(module.nullIsSameAsDefault)
@@ -31,7 +31,7 @@ class KotlinModuleTest {
3131
fun builder_Defaults() {
3232
val module = KotlinModule.Builder().build()
3333

34-
assertEquals(512, module.maxCacheSize)
34+
assertEquals(KotlinModule.CacheSize(512), module.cacheSize)
3535
assertFalse(module.nullToEmptyCollection)
3636
assertFalse(module.nullToEmptyMap)
3737
assertFalse(module.nullIsSameAsDefault)
@@ -42,15 +42,15 @@ class KotlinModuleTest {
4242
@Test
4343
fun builder_SetAll() {
4444
val module = KotlinModule.Builder().apply {
45-
withInitialCacheSize(123)
45+
withCacheSize(KotlinModule.CacheSize(initialCacheSize = 123))
4646
enable(NullToEmptyCollection)
4747
enable(NullToEmptyMap)
4848
enable(NullIsSameAsDefault)
4949
enable(SingletonSupport)
5050
enable(StrictNullChecks)
5151
}.build()
5252

53-
assertEquals(123, module.initialCacheSize)
53+
assertEquals(123, module.cacheSize.initialCacheSize)
5454
assertTrue(module.nullToEmptyCollection)
5555
assertTrue(module.nullToEmptyMap)
5656
assertTrue(module.nullIsSameAsDefault)

0 commit comments

Comments
 (0)