Skip to content

Commit

Permalink
Add conversion to and from Java primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
MateuszKubuszok committed Jun 25, 2024
1 parent 2e1a36f commit f65103e
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.scalaland.chimney.javacollections

import io.scalaland.chimney.Transformer

/** @since 1.2.0 */
trait JavaPrimitivesImplicits {

// Java to Scala

/** @since 1.2.0 */
implicit val totalTransformerFromJavaBooleanToScalaBoolean: Transformer[java.lang.Boolean, Boolean] = _.booleanValue()

/** @since 1.2.0 */
implicit val totalTransformerFromJavaByteToScalaByte: Transformer[java.lang.Byte, Byte] = _.byteValue()

/** @since 1.2.0 */
implicit val totalTransformerFromJavaCharacterToScalaChar: Transformer[java.lang.Character, Char] = _.charValue()

/** @since 1.2.0 */
implicit val totalTransformerFromJavaIntegerToScalaInt: Transformer[java.lang.Integer, Int] = _.intValue()

/** @since 1.2.0 */
implicit val totalTransformerFromJavaLongToScalaLong: Transformer[java.lang.Long, Long] = _.longValue()

/** @since 1.2.0 */
implicit val totalTransformerFromJavaShortToScalaShort: Transformer[java.lang.Short, Short] = _.shortValue()

/** @since 1.2.0 */
implicit val totalTransformerFromJavaFloatToScalaFloat: Transformer[java.lang.Float, Float] = _.floatValue()

/** @since 1.2.0 */
implicit val totalTransformerFromJavaDoubleToScalaDouble: Transformer[java.lang.Double, Double] = _.doubleValue()

// Scala to Java

/** @since 1.2.0 */
implicit val totalTransformerFromScalaBooleanToJavaBoolean: Transformer[Boolean, java.lang.Boolean] = b => b

/** @since 1.2.0 */
implicit val totalTransformerFromScalaByteToJavaByte: Transformer[Byte, java.lang.Byte] = b => b

/** @since 1.2.0 */
implicit val totalTransformerFromScalaCharToJavaCharacter: Transformer[Char, java.lang.Character] = c => c

/** @since 1.2.0 */
implicit val totalTransformerFromScalaIntToJavaInteger: Transformer[Int, java.lang.Integer] = i => i

/** @since 1.2.0 */
implicit val totalTransformerFromScalaLongToJavaLong: Transformer[Long, java.lang.Long] = l => l

/** @since 1.2.0 */
implicit val totalTransformerFromScalaShortToJavaShort: Transformer[Short, java.lang.Short] = s => s

/** @since 1.2.0 */
implicit val totalTransformerFromScalaFloatToJavaFloat: Transformer[Float, java.lang.Float] = f => f

/** @since 1.2.0 */
implicit val totalTransformerFromScalaDoubleToJavaDouble: Transformer[Double, java.lang.Double] = d => d
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.scalaland.chimney

/** @since 0.8.0 */
package object javacollections extends JavaCollectionsImplicits
package object javacollections extends JavaCollectionsImplicits with JavaPrimitivesImplicits
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package io.scalaland.chimney.javacollections

import io.scalaland.chimney.ChimneySpec
import io.scalaland.chimney.dsl.*

class TotalTransformerJavaPrimitivesSpec extends ChimneySpec {

group("conversion between java.lang.Boolean and scala.Boolean") {

val javaValue = java.lang.Boolean.valueOf("true")
val scalaValue = true

test("Java to Scala") {
javaValue.transformInto[Boolean] ==> scalaValue
}

test("Scala to Java") {
scalaValue.transformInto[java.lang.Boolean] ==> javaValue
}
}

group("conversion between java.lang.Byte and scala.Byte") {

val javaValue = java.lang.Byte.valueOf("#00")
val scalaValue = 0.toByte

test("Java to Scala") {
javaValue.transformInto[Byte] ==> scalaValue
}

test("Scala to Java") {
scalaValue.transformInto[java.lang.Byte] ==> javaValue
}
}

group("conversion between java.lang.Character and scala.Char") {

val javaValue = java.lang.Character.valueOf('c')
val scalaValue = 'c'

test("Java to Scala") {
javaValue.transformInto[Char] ==> scalaValue
}

test("Scala to Java") {
scalaValue.transformInto[java.lang.Character] ==> javaValue
}
}

group("conversion between java.lang.Integer and scala.Int") {

val javaValue = java.lang.Integer.valueOf(1024)
val scalaValue = 1024

test("Java to Scala") {
javaValue.transformInto[Int] ==> scalaValue
}

test("Scala to Java") {
scalaValue.transformInto[java.lang.Integer] ==> javaValue
}
}

group("conversion between java.lang.Long and scala.Long") {

val javaValue = java.lang.Long.valueOf(1024L)
val scalaValue = 1024L

test("Java to Scala") {
javaValue.transformInto[Long] ==> scalaValue
}

test("Scala to Java") {
scalaValue.transformInto[java.lang.Long] ==> javaValue
}
}

group("conversion between java.lang.Short and scala.Short") {

val javaValue = java.lang.Short.valueOf(1024.toShort)
val scalaValue = 1024.toShort

test("Java to Scala") {
javaValue.transformInto[Short] ==> scalaValue
}

test("Scala to Java") {
scalaValue.transformInto[java.lang.Short] ==> javaValue
}
}

group("conversion between java.lang.Float and scala.Float") {

val javaValue = java.lang.Float.valueOf(1024.0f)
val scalaValue = 1024.0f

test("Java to Scala") {
javaValue.transformInto[Float] ==> scalaValue
}

test("Scala to Java") {
scalaValue.transformInto[java.lang.Float] ==> javaValue
}
}

group("conversion between java.lang.Double and scala.Double") {

val javaValue = java.lang.Double.valueOf(1024.0)
val scalaValue = 1024.0

test("Java to Scala") {
javaValue.transformInto[Double] ==> scalaValue
}

test("Scala to Java") {
scalaValue.transformInto[java.lang.Double] ==> javaValue
}
}
}

0 comments on commit f65103e

Please sign in to comment.