Skip to content

Commit

Permalink
finos#1296 support for conversion from primitive to wrapper types for…
Browse files Browse the repository at this point in the history
… consistency
  • Loading branch information
junaidzm13 committed Apr 22, 2024
1 parent cac2d88 commit 66fc82e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
1 change: 0 additions & 1 deletion vuu/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ target/
.idea/vcs.xml
.idea/workspace.xml
src/run-config/
src/test/java/
target
target/**
target/generated-test-sources/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.finos.vuu.util.schema.typeConversion

import org.finos.vuu.util.schema.typeConversion.DefaultTypeConverters._

import org.finos.vuu.util.schema.typeConversion.TypeConverter.toWrapperType

trait TypeConverter[FromType, ToType] {
val fromClass: Class[FromType]
val toClass: Class[ToType]
def convert(v: FromType): ToType
final def name: String = s"${fromClass.getTypeName}->${toClass.getTypeName}"
final def name: String = s"${toWrapperType(fromClass).getTypeName}->${toWrapperType(toClass).getTypeName}"
}

object TypeConverter {
Expand All @@ -19,6 +19,18 @@ object TypeConverter {
StringToLongConverter(),
StringToIntConverter()
)

def toWrapperType(t: Class[_]): Class[_] = if (t.isPrimitive) primitiveToWrapperType(t) else t

private val primitiveToWrapperType: Map[Class[_], Class[_]] = Map(
classOf[Char] -> classOf[java.lang.Character],
classOf[Byte] -> classOf[java.lang.Byte],
classOf[Short] -> classOf[java.lang.Short],
classOf[Int] -> classOf[java.lang.Integer],
classOf[Long] -> classOf[java.lang.Long],
classOf[Float] -> classOf[java.lang.Float],
classOf[Double] -> classOf[java.lang.Double],
)
}

private class TypeConverterImpl[From, To](override val fromClass: Class[From],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public Double convert(String v) {
final var tc = new MyTypeConverter();

assertEquals(Double.valueOf(20.56), tc.convert("20.56"));
assertEquals(tc.name(), "java.lang.String->double");
assertEquals(tc.name(), "java.lang.String->java.lang.Double");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TypeConverterTest extends AnyFeatureSpec with Matchers {
val tc: TypeConverter[Int, String] = TypeConverter(classOf[Int], classOf[String], _.toString)

tc.convert(101) should equal("101")
tc.name should equal("int->java.lang.String")
tc.name should equal("java.lang.Integer->java.lang.String")
}

Scenario("instantiation through trait implementation") {
Expand All @@ -24,7 +24,7 @@ class TypeConverterTest extends AnyFeatureSpec with Matchers {
val tc: TypeConverter[Double, String] = new MyTypeConverter()

tc.convert(10.56) should equal("10.56")
tc.name should equal("double->java.lang.String")
tc.name should equal("java.lang.Double->java.lang.String")
}
}
}

0 comments on commit 66fc82e

Please sign in to comment.