Skip to content

Commit

Permalink
Start testing total conversions between Scala and Java collections
Browse files Browse the repository at this point in the history
  • Loading branch information
MateuszKubuszok committed Sep 27, 2023
1 parent 22bdd28 commit a6406ac
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.scalaland.chimney.Transformer
import scala.collection.compat.*

/** @since 0.8.1 */
trait JavaCollectionsTotalTransformerImplicits {
trait JavaCollectionsTotalTransformerImplicits extends JavaCollectionsTotalTransformerLowPriorityImplicits1 {

// Optionals

Expand Down Expand Up @@ -151,3 +151,77 @@ trait JavaCollectionsTotalTransformerImplicits {
builder.result()

Check warning on line 151 in chimney-java-collections/src/main/scala/io/scalaland/chimney/javacollections/JavaCollectionsTotalTransformerImplicits.scala

View check run for this annotation

Codecov / codecov/patch

chimney-java-collections/src/main/scala/io/scalaland/chimney/javacollections/JavaCollectionsTotalTransformerImplicits.scala#L144-L151

Added lines #L144 - L151 were not covered by tests
}
}

private[javacollections] trait JavaCollectionsTotalTransformerLowPriorityImplicits1 {

// Dictionaries

/** @since 0.8.1 */

Check warning on line 159 in chimney-java-collections/src/main/scala/io/scalaland/chimney/javacollections/JavaCollectionsTotalTransformerImplicits.scala

View check run for this annotation

Codecov / codecov/patch

chimney-java-collections/src/main/scala/io/scalaland/chimney/javacollections/JavaCollectionsTotalTransformerImplicits.scala#L159

Added line #L159 was not covered by tests
implicit def totalTransformerFromJavaDictionaryToScalaMap[
JMap[K0, V0] <: java.util.Dictionary[K0, V0],
SMap[K0, V0] <: scala.collection.Map[K0, V0],
K1,
V1,
K2,
V2
](implicit
keys: Transformer[K1, K2],
values: Transformer[V1, V2],
factory: Factory[(K2, V2), SMap[K2, V2]]
): Transformer[JMap[K1, V1], SMap[K2, V2]] =
collection => {
val builder = factory.newBuilder
val it = collection.keys()
while (it.hasMoreElements()) {
val key = it.nextElement()
builder += (keys.transform(key) -> values.transform(collection.get(key)))
}
builder.result()

Check warning on line 179 in chimney-java-collections/src/main/scala/io/scalaland/chimney/javacollections/JavaCollectionsTotalTransformerImplicits.scala

View check run for this annotation

Codecov / codecov/patch

chimney-java-collections/src/main/scala/io/scalaland/chimney/javacollections/JavaCollectionsTotalTransformerImplicits.scala#L172-L179

Added lines #L172 - L179 were not covered by tests
}

/** @since 0.8.1 */
implicit def totalTransformerFromScalaMapToJavaDictionary[
SMap[K0, V0] <: scala.collection.Map[K0, V0],
JMap[K0, V0] <: java.util.Dictionary[K0, V0],
K1,
V1,
K2,
V2
](implicit
keys: Transformer[K1, K2],
values: Transformer[V1, V2],
factory: JavaFactory[(K2, V2), JMap[K2, V2]]
): Transformer[SMap[K1, V1], JMap[K2, V2]] =
collection => {
val builder = factory.newBuilder
val it = collection.iterator
while (it.hasNext) {
val pair = it.next()
builder.addOne(keys.transform(pair._1) -> values.transform(pair._2))
}
builder.result()
}

/** @since 0.8.1 */

Check warning on line 205 in chimney-java-collections/src/main/scala/io/scalaland/chimney/javacollections/JavaCollectionsTotalTransformerImplicits.scala

View check run for this annotation

Codecov / codecov/patch

chimney-java-collections/src/main/scala/io/scalaland/chimney/javacollections/JavaCollectionsTotalTransformerImplicits.scala#L205

Added line #L205 was not covered by tests
implicit def totalTransformerFromJavaMapToJavaDictionary[
JMap1[K0, V0] <: java.util.Dictionary[K0, V0],
JMap2[K0, V0] <: java.util.Dictionary[K0, V0],
K1,
V1,
K2,
V2
](implicit
keys: Transformer[K1, K2],
values: Transformer[V1, V2],
factory: JavaFactory[(K2, V2), JMap2[K2, V2]]
): Transformer[JMap1[K1, V1], JMap2[K2, V2]] =
collection => {
val builder = factory.newBuilder
val it = collection.keys()
while (it.hasMoreElements()) {
val key = it.nextElement()
builder.addOne(keys.transform(key) -> values.transform(collection.get(key)))
}
builder.result()

Check warning on line 225 in chimney-java-collections/src/main/scala/io/scalaland/chimney/javacollections/JavaCollectionsTotalTransformerImplicits.scala

View check run for this annotation

Codecov / codecov/patch

chimney-java-collections/src/main/scala/io/scalaland/chimney/javacollections/JavaCollectionsTotalTransformerImplicits.scala#L218-L225

Added lines #L218 - L225 were not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class JavaFactorySpec extends ChimneySpec {
convertAndVerifySorted[String, Int, ju.TreeMap]("d" -> 10, "c" -> 8, "b" -> 4, "a" -> 0)
}

test("java.util.Collection instances should be resolved for java.util.Enum specializations") {
test("java.util.Collection instances should be resolved for java.lang.Enum specializations") {
val enumSet = implicitly[JavaFactory[JavaEnum, ju.EnumSet[JavaEnum]]]
.fromSpecific(Iterator(JavaEnum.Blue, JavaEnum.Green, JavaEnum.Red))
val enumMap = implicitly[JavaFactory[(JavaEnum, Int), ju.EnumMap[JavaEnum, Int]]].fromSpecific(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package io.scalaland.chimney.javacollections

import java.util as ju
import io.scalaland.chimney.{ChimneySpec, Transformer}
import io.scalaland.chimney.dsl.*

import scala.collection.immutable.ListMap
import scala.jdk.CollectionConverters.*

class TotalTransformerJavaCollectionsConversionsSpec extends ChimneySpec {

// TODO: options

// TODO iterator and enumeration
// Seq(4,3,2,1).transformInto[ju.Iterator[String]].asScala.toList ==> List("4","3","2","1")
// Seq(4,3,2,1).transformInto[ju.Enumeration[String]].asScala.toList ==> List("4","3","2","1")

group("conversion from Scala types to java.util.Collection types") {

test("to non-Map types") {
implicit val intToString: Transformer[Int, String] = _.toString

val input = Seq(4, 3, 2, 1)
val outputStable = List("4", "3", "2", "1")
val outputUnstable = Set("4", "3", "2", "1")
val outputSorted = List("1", "2", "3", "4")

input.transformInto[ju.Collection[String]].asScala.toSet ==> outputUnstable
input.transformInto[ju.AbstractCollection[String]].asScala.toSet ==> outputUnstable

input.transformInto[ju.List[String]].asScala.toList ==> outputStable
input.transformInto[ju.AbstractList[String]].asScala.toList ==> outputStable
input.transformInto[ju.AbstractSequentialList[String]].asScala.toList ==> outputStable
input.transformInto[ju.ArrayList[String]].asScala.toList ==> outputStable
input.transformInto[ju.LinkedList[String]].asScala.toList ==> outputStable
input.transformInto[ju.Vector[String]].asScala.toList ==> outputStable
input.transformInto[ju.Stack[String]].asScala.toList ==> outputStable

input.transformInto[ju.Deque[String]].asScala.toList ==> outputStable
input.transformInto[ju.ArrayDeque[String]].asScala.toList ==> outputStable

input.transformInto[ju.Queue[String]].asScala.toList ==> outputSorted
input.transformInto[ju.AbstractQueue[String]].asScala.toList ==> outputSorted
input.transformInto[ju.PriorityQueue[String]].asScala.toList ==> outputSorted

input.transformInto[ju.Set[String]].asScala.toSet ==> outputUnstable
input.transformInto[ju.AbstractSet[String]].asScala.toSet ==> outputUnstable
input.transformInto[ju.SortedSet[String]].asScala.toList ==> outputSorted
input.transformInto[ju.NavigableSet[String]].asScala.toList ==> outputSorted
input.transformInto[ju.HashSet[String]].asScala.toSet ==> outputUnstable
input.transformInto[ju.LinkedHashSet[String]].asScala.toList ==> outputStable
input.transformInto[ju.TreeSet[String]].asScala.toList ==> outputSorted
}

test("to java.util.Dictionary types") {
implicit val intToString: Transformer[Int, String] = _.toString

val input = ListMap(4 -> 4, 3 -> 3, 2 -> 2, 1 -> 1)
val output = Map("4" -> "4", "3" -> "3", "2" -> "2", "1" -> "1")

input.transformInto[ju.Dictionary[String, String]].asScala ==> output
(input.transformInto[ju.Hashtable[String, String]]: ju.Dictionary[String, String]).asScala ==> output
}

test("to java.util.Map types") {
implicit val intToString: Transformer[Int, String] = _.toString

val input = ListMap(4 -> 4, 3 -> 3, 2 -> 2, 1 -> 1)
val outputStable = List("4" -> "4", "3" -> "3", "2" -> "2", "1" -> "1")
val outputUnstable = Map("4" -> "4", "3" -> "3", "2" -> "2", "1" -> "1")
val outputSorted = List("1" -> "1", "2" -> "2", "3" -> "3", "4" -> "4")

input.transformInto[ju.Map[String, String]].asScala ==> outputUnstable
input.transformInto[ju.AbstractMap[String, String]].asScala ==> outputUnstable
input.transformInto[ju.SortedMap[String, String]].asScala.toList ==> outputSorted
input.transformInto[ju.NavigableMap[String, String]].asScala.toList ==> outputSorted
input.transformInto[ju.HashMap[String, String]].asScala ==> outputUnstable
input.transformInto[ju.LinkedHashMap[String, String]].asScala.toList ==> outputStable
input.transformInto[ju.TreeMap[String, String]].asScala.toList ==> outputSorted
}

test("to java.lang.Enum-supporting types".ignore) {} // TODO
}

group("conversion from java.util.Collection types to Scala types") {

test("from non-Map types".ignore) {} // TODO
test("from java.util.Dictionary types".ignore) {} // TODO
test("from java.util.Map types".ignore) {} // TODO
}

group("conversion from java.util.Collection types to java.util.Collection types") {

test("for non-Map types".ignore) {} // TODO
test("for java.util.Dictionary types".ignore) {} // TODO
test("for java.util.Map types".ignore) {} // TODO
}

group("java.util.BitSet conversions") {

test("from Scala types".ignore) {} // TODO
test("to Scala types".ignore) {} // TODO
}
}

0 comments on commit a6406ac

Please sign in to comment.