Skip to content

Commit

Permalink
Test summoning all of JavaFactories
Browse files Browse the repository at this point in the history
  • Loading branch information
MateuszKubuszok committed Sep 27, 2023
1 parent 666080b commit 22bdd28
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.scalaland.chimney.javacollections

import java.util as ju

import scala.collection.compat.*
import scala.jdk.CollectionConverters.*
import scala.reflect.{classTag, ClassTag}
Expand Down Expand Up @@ -45,6 +44,20 @@ object JavaFactory {
}
}

final class EnumerationFactory[A] extends JavaFactory[A, ju.Enumeration[A]] {

def fromIterator(it: ju.Iterator[A]): ju.Enumeration[A] = new ju.Enumeration[A] {
def hasMoreElements: Boolean = it.hasNext()
def nextElement(): A = it.next()
}

def newBuilder: Builder[A, ju.Enumeration[A]] = new Builder[A, ju.Enumeration[A]] {
private val collection = new ju.ArrayList[A]()
def addOne(a: A): Unit = { collection.add(a); () }
def result(): ju.Enumeration[A] = fromIterator(collection.iterator())

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

View check run for this annotation

Codecov / codecov/patch

chimney-java-collections/src/main/scala/io/scalaland/chimney/javacollections/JavaFactory.scala#L53-L57

Added lines #L53 - L57 were not covered by tests
}
}

final class CollectionFactory[A, CC[A1] <: ju.Collection[A1]](
create: => CC[A]
) extends JavaFactory[A, CC[A]] {
Expand Down Expand Up @@ -84,13 +97,33 @@ object JavaFactory {
}
}

// TODO: Enumeration?
final class BitSetFactory extends JavaFactory[Int, ju.BitSet] {
def fromIterator(it: ju.Iterator[Int]): ju.BitSet = {
val collection = new ju.BitSet()
while (it.hasNext()) {
collection.set(it.next())
()
}
collection
}

def newBuilder: Builder[Int, ju.BitSet] = new Builder[Int, ju.BitSet] {
private val collection = new ju.BitSet()
final def addOne(a: Int): Unit = { collection.set(a); () }
final def result(): ju.BitSet = collection

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

View check run for this annotation

Codecov / codecov/patch

chimney-java-collections/src/main/scala/io/scalaland/chimney/javacollections/JavaFactory.scala#L109-L113

Added lines #L109 - L113 were not covered by tests
}
}

// Iterator

/** @since 0.8.1 */
implicit def javaFactoryForIterator[A]: JavaFactory[A, ju.Iterator[A]] = new IteratorFactory[A]

// Enumeration

/** @since 0.8.1 */
implicit def javaFactoryForEnumeration[A]: JavaFactory[A, ju.Enumeration[A]] = new EnumerationFactory[A]

// Collections

/** @since 0.8.1 */
Expand Down Expand Up @@ -169,11 +202,6 @@ object JavaFactory {
implicit def javaFactoryForNavigableSet[A: Ordering]: JavaFactory[A, ju.NavigableSet[A]] =
javaFactoryForTreeSet[A].narrow

/** @since 0.8.1 */
implicit def javaFactoryForEnumSet[A <: java.lang.Enum[A]: ClassTag]: JavaFactory[A, ju.EnumSet[A]] =
new CollectionFactory[A, ju.Set](ju.EnumSet.noneOf[A](classTag[A].runtimeClass.asInstanceOf[Class[A]]))
.asInstanceOf[JavaFactory[A, ju.EnumSet[A]]]

/** @since 0.8.1 */
implicit def javaFactoryForHashSet[A]: JavaFactory[A, ju.HashSet[A]] = new CollectionFactory(new ju.HashSet[A])

Expand All @@ -187,7 +215,13 @@ object JavaFactory {
new ju.TreeSet[A](Ordering[A])
)

// TODO: BitSet?
/** @since 0.8.1 */
implicit def javaFactoryForEnumSet[A <: java.lang.Enum[A]: ClassTag]: JavaFactory[A, ju.EnumSet[A]] =

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

View check run for this annotation

Codecov / codecov/patch

chimney-java-collections/src/main/scala/io/scalaland/chimney/javacollections/JavaFactory.scala#L219

Added line #L219 was not covered by tests
new CollectionFactory[A, ju.Set](ju.EnumSet.noneOf[A](classTag[A].runtimeClass.asInstanceOf[Class[A]]))
.asInstanceOf[JavaFactory[A, ju.EnumSet[A]]]

/** @since 0.8.1 */
implicit val javaFactoryForBitSet: JavaFactory[Int, ju.BitSet] = new BitSetFactory

// Dictionaries

Expand Down Expand Up @@ -218,11 +252,6 @@ object JavaFactory {
implicit def javaFactoryForNavigableMap[K: Ordering, V]: JavaFactory[(K, V), ju.NavigableMap[K, V]] =
javaFactoryForTreeMap[K, V].narrow

/** @since 0.8.1 */
implicit def javaFactoryForEnumMap[K <: java.lang.Enum[K]: ClassTag, V]: JavaFactory[(K, V), ju.EnumMap[K, V]] =
new MapFactory[K, V, ju.Map](new ju.EnumMap[K, V](classTag[K].runtimeClass.asInstanceOf[Class[K]]))
.asInstanceOf[JavaFactory[(K, V), ju.EnumMap[K, V]]]

/** @since 0.8.1 */
implicit def javaFactoryForHashMap[K, V]: JavaFactory[(K, V), ju.HashMap[K, V]] =
new MapFactory[K, V, ju.HashMap](new ju.HashMap[K, V])
Expand All @@ -244,4 +273,9 @@ object JavaFactory {
implicit def javaFactoryForTreeMap[K: Ordering, V]: JavaFactory[(K, V), ju.TreeMap[K, V]] = new MapFactory(
new ju.TreeMap[K, V](Ordering[K])
)

/** @since 0.8.1 */
implicit def javaFactoryForEnumMap[K <: java.lang.Enum[K]: ClassTag, V]: JavaFactory[(K, V), ju.EnumMap[K, V]] =

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

View check run for this annotation

Codecov / codecov/patch

chimney-java-collections/src/main/scala/io/scalaland/chimney/javacollections/JavaFactory.scala#L278

Added line #L278 was not covered by tests
new MapFactory[K, V, ju.Map](new ju.EnumMap[K, V](classTag[K].runtimeClass.asInstanceOf[Class[K]]))
.asInstanceOf[JavaFactory[(K, V), ju.EnumMap[K, V]]]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.scalaland.chimney.fixtures;

public enum JavaEnum {
Red, Green, Blue;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,25 @@ package io.scalaland.chimney.javacollections

import java.util as ju
import io.scalaland.chimney.ChimneySpec
import io.scalaland.chimney.fixtures.JavaEnum

class JavaFactorySpec extends ChimneySpec {

// TODO: Iterator
test("java.util.Iterator instance should be resolved") {
val iterator =
implicitly[JavaFactory[String, java.util.Iterator[String]]].fromSpecific(Iterator("d", "c", "b", "a"))

// TODO: Enumeration
import scala.jdk.CollectionConverters.*
iterator.asScala.toVector ==> Vector("d", "c", "b", "a")
}

test("java.util.Enumeration instance should be resolved") {
val iterator =
implicitly[JavaFactory[String, java.util.Enumeration[String]]].fromSpecific(Iterator("d", "c", "b", "a"))

import scala.jdk.CollectionConverters.*
iterator.asScala.toVector ==> Vector("d", "c", "b", "a")
}

test("java.util.Collection instances should be resolved for both concrete and abstract types for non-Map types") {
def convertAndVerifyStable[A, CC[A1] <: ju.Collection[A1]](values: A*)(implicit
Expand Down Expand Up @@ -56,8 +69,6 @@ class JavaFactorySpec extends ChimneySpec {
convertAndVerifyUnstable[String, ju.HashSet]("d", "c", "b", "a")
convertAndVerifyStable[String, ju.LinkedHashSet]("d", "c", "b", "a")
convertAndVerifySorted[String, ju.TreeSet]("d", "c", "b", "a")

// TODO: EnumSet
}

test(
Expand Down Expand Up @@ -112,7 +123,31 @@ class JavaFactorySpec extends ChimneySpec {
convertAndVerifyStable[String, Int, ju.LinkedHashMap]("d" -> 10, "c" -> 8, "b" -> 4, "a" -> 0)
convertAndVerifyUnstable[String, Int, ju.WeakHashMap]("d" -> 10, "c" -> 8, "b" -> 4, "a" -> 0)
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") {
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(
Iterator(
JavaEnum.Blue -> 10,
JavaEnum.Green -> 5,
JavaEnum.Red -> 0
)
)

import scala.jdk.CollectionConverters.*
enumSet.iterator().asScala.toVector ==> Vector(JavaEnum.Red, JavaEnum.Green, JavaEnum.Blue)
enumMap.entrySet().iterator().asScala.map(es => es.getKey -> es.getValue).toVector ==> Vector(
JavaEnum.Red -> 0,
JavaEnum.Green -> 5,
JavaEnum.Blue -> 10
)
}

test("java.util.Collection instances should be resolved for java.util.BitSet specialization") {
val bitSet = implicitly[JavaFactory[Int, ju.BitSet]].fromSpecific(Iterator(10, 8, 4, 2, 0))

// TODO: EnumMap
bitSet.toLongArray ==> Array((1L << 10) + (1L << 8) + (1L << 4) + (1L << 2) + (1L << 0))
}
}

0 comments on commit 22bdd28

Please sign in to comment.