diff --git a/src/main/java/dev/krud/shapeshift/ShapeShift.kt b/src/main/java/dev/krud/shapeshift/ShapeShift.kt index af73575..664459c 100644 --- a/src/main/java/dev/krud/shapeshift/ShapeShift.kt +++ b/src/main/java/dev/krud/shapeshift/ShapeShift.kt @@ -23,10 +23,10 @@ import dev.krud.shapeshift.util.setValue import org.slf4j.LoggerFactory import java.lang.reflect.Field -class ShapeShift( - transformers: List> = emptyList() +class ShapeShift constructor( + transformersRegistrations: List> = emptyList() ) { - internal val transformers: MutableList> = transformers.toMutableList() + internal val transformers: MutableList> = mutableListOf() internal val transformersByNameCache: MutableMap> = mutableMapOf() internal val transformersByTypeCache: MutableMap>, TransformerRegistration<*, *>> = mutableMapOf() @@ -34,24 +34,10 @@ class ShapeShift( private val mappingStructures: MutableMap = mutableMapOf() private val entityFieldsCache: MutableMap, Map> = mutableMapOf() - fun registerTransformer(registration: TransformerRegistration) { - val name = registration.name ?: registration.transformer::class.simpleName!! - val newRegistration = registration.copy(name = name) - val existingTransformer = getTransformerByName(name) - if (existingTransformer != TransformerRegistration.EMPTY) { - error("Transformer with name $name already exists with type ${existingTransformer.transformer::class}") - } - if (newRegistration.default) { - val existingDefaultTransformer = defaultTransformers[newRegistration.transformer.id] - if (existingDefaultTransformer != null) { - error("Default transformer with pair ${newRegistration.transformer.id} already exists") - } - defaultTransformers[newRegistration.transformer.id] = newRegistration + init { + for (registration in transformersRegistrations) { + registerTransformer(registration) } - - transformers.add(newRegistration) - transformersByNameCache.remove(name) - transformersByTypeCache.remove(newRegistration.transformer::class.java) } fun map(fromObject: From, toClazz: Class): To { @@ -290,6 +276,26 @@ class ShapeShift( return transformerRegistration } + private fun registerTransformer(registration: TransformerRegistration) { + val name = registration.name ?: registration.transformer::class.simpleName!! + val newRegistration = registration.copy(name = name) + val existingTransformer = getTransformerByName(name) + if (existingTransformer != TransformerRegistration.EMPTY) { + error("Transformer with name $name already exists with type ${existingTransformer.transformer::class}") + } + if (newRegistration.default) { + val existingDefaultTransformer = defaultTransformers[newRegistration.transformer.id] + if (existingDefaultTransformer != null) { + error("Default transformer with pair ${newRegistration.transformer.id} already exists") + } + defaultTransformers[newRegistration.transformer.id] = newRegistration + } + + transformers.add(newRegistration) + transformersByNameCache.remove(name) + transformersByTypeCache.remove(newRegistration.transformer::class.java) + } + companion object { private val log = LoggerFactory.getLogger(ShapeShift::class.java) const val NODE_DELIMITER = "." diff --git a/src/main/java/dev/krud/shapeshift/ShapeShiftBuilder.kt b/src/main/java/dev/krud/shapeshift/ShapeShiftBuilder.kt new file mode 100644 index 0000000..de1a2bd --- /dev/null +++ b/src/main/java/dev/krud/shapeshift/ShapeShiftBuilder.kt @@ -0,0 +1,24 @@ +/* + * Copyright KRUD 2022 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package dev.krud.shapeshift + +class ShapeShiftBuilder { + private val transformers: MutableList> = mutableListOf() + + fun withTransformer(transformerRegistration: TransformerRegistration<*, *>): ShapeShiftBuilder { + transformers += transformerRegistration + return this + } + + fun build(): ShapeShift { + return ShapeShift(transformers) + } +} diff --git a/src/test/java/dev/krud/shapeshift/ShapeShiftTests.kt b/src/test/java/dev/krud/shapeshift/ShapeShiftTests.kt index e578066..ffd5763 100644 --- a/src/test/java/dev/krud/shapeshift/ShapeShiftTests.kt +++ b/src/test/java/dev/krud/shapeshift/ShapeShiftTests.kt @@ -51,11 +51,14 @@ internal class ShapeShiftTests { @Test internal fun `simple mapping with transformer by type`() { - shapeShift.registerTransformer( - TransformerRegistration( - LongToStringTransformer() + shapeShift = ShapeShiftBuilder() + .withTransformer( + TransformerRegistration( + LongToStringTransformer() + ) ) - ) + .build() + val result = shapeShift.map(TypeTransformerFrom(), StringTo::class.java) expectThat(result.long) @@ -64,12 +67,14 @@ internal class ShapeShiftTests { @Test internal fun `simple mapping with transformer by name`() { - shapeShift.registerTransformer( - TransformerRegistration( - LongToStringTransformer(), - name = "myTransformer" + shapeShift = ShapeShiftBuilder() + .withTransformer( + TransformerRegistration( + LongToStringTransformer(), + name = "myTransformer" + ) ) - ) + .build() val result = shapeShift.map(NameTransformerFrom(), StringTo::class.java) expectThat(result.long) @@ -78,12 +83,14 @@ internal class ShapeShiftTests { @Test internal fun `simple mapping with default transformer`() { - shapeShift.registerTransformer( - TransformerRegistration( - LongToStringTransformer(), - default = true + shapeShift = ShapeShiftBuilder() + .withTransformer( + TransformerRegistration( + LongToStringTransformer(), + default = true + ) ) - ) + .build() val result = shapeShift.map(DefaultTransformerFrom(), StringTo::class.java) expectThat(result.long) @@ -225,9 +232,13 @@ internal class ShapeShiftTests { true, "second" ) - shapeShift.registerTransformer(firstRegistration) + + val builder = ShapeShiftBuilder() + .withTransformer(firstRegistration) + .withTransformer(secondRegistration) + expectThrows { - shapeShift.registerTransformer(secondRegistration) + builder.build() } } @@ -238,7 +249,9 @@ internal class ShapeShiftTests { false, null ) - shapeShift.registerTransformer(registration) + shapeShift = ShapeShiftBuilder() + .withTransformer(registration) + .build() expectThat(shapeShift.transformers.first().name) .isEqualTo( "ExampleFieldTransformer" @@ -253,9 +266,12 @@ internal class ShapeShiftTests { "first" ) - shapeShift.registerTransformer(registration) + val builder = ShapeShiftBuilder() + .withTransformer(registration) + .withTransformer(registration) + expectThrows { - shapeShift.registerTransformer(registration) + builder.build() } } @@ -330,6 +346,7 @@ internal class ShapeShiftTests { class Grandchild { val greatGrandchild: GreatGrandchild = GreatGrandchild() + class GreatGrandchild { val long: Long = 1L } @@ -383,6 +400,7 @@ internal class ShapeShiftTests { internal class FromWithMapFromSelfQualifier { @MappedField(target = GenericTo::class, mapFrom = "child.long") val child: Child = Child() + class Child { val long: Long = 1L } @@ -391,6 +409,7 @@ internal class ShapeShiftTests { @MappedField(target = GenericTo::class, mapFrom = "fromWithMapFromSelfQualifierOnType.child.long") internal class FromWithMapFromSelfQualifierOnType { val child: Child = Child() + class Child { val long: Long = 1L } @@ -465,6 +484,7 @@ internal class ShapeShiftTests { class Grandchild { val greatGrandchild: GreatGrandchild? = null + class GreatGrandchild { val long: Long? = null }