Skip to content

Commit

Permalink
Merge pull request #10205 from som-snytt/regress/equals-converters
Browse files Browse the repository at this point in the history
Restore IterableWrapper equals and hashCode
  • Loading branch information
lrytz committed Nov 4, 2022
2 parents 3e76cae + 669bba3 commit adb6dba
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
Expand Up @@ -56,7 +56,15 @@ private[collection] object JavaCollectionWrappers extends Serializable {
}

@SerialVersionUID(3L)
class IterableWrapper[A](val underlying: Iterable[A]) extends ju.AbstractCollection[A] with IterableWrapperTrait[A] with Serializable
class IterableWrapper[A](val underlying: Iterable[A]) extends ju.AbstractCollection[A] with IterableWrapperTrait[A] with Serializable {
import scala.runtime.Statics._
override def equals(other: Any): Boolean =
other match {
case other: IterableWrapper[_] => underlying.equals(other.underlying)
case _ => false
}
override def hashCode = finalizeHash(mix(mix(0xcafebabe, "IterableWrapper".hashCode), anyHash(underlying)), 1)
}

@SerialVersionUID(3L)
class JIterableWrapper[A](val underlying: jl.Iterable[A])
Expand Down
78 changes: 78 additions & 0 deletions test/junit/scala/collection/convert/EqualsTest.scala
@@ -0,0 +1,78 @@

package scala.collection.convert

import org.junit.Test
import org.junit.Assert._

import scala.jdk.CollectionConverters._
import JavaCollectionWrappers._

import java.util.{AbstractList, AbstractSet, List => JList, Set => JSet}

class JTestList(vs: Int*) extends AbstractList[Int] {
def this() = this(Nil: _*)
override def size = vs.size
override def get(i: Int) = vs(i)
}
class JTestSet(vs: Int*) extends AbstractSet[Int] {
def this() = this(Nil: _*)
require(vs.toSet.size == vs.size)
override def size = vs.size
override def iterator = vs.iterator.asJava
}

/** Test that collection wrappers forward equals and hashCode where appropriate. */
class EqualsTest {

def jlstOf(vs: Int*): JList[Int] = new JTestList(vs: _*)
def jsetOf(vs: Int*): JSet[Int] = new JTestSet(vs: _*)

// Seq extending AbstractList inherits equals

@Test def `List as JList has equals`: Unit = {
val list = List(1, 2, 3)
val jlst = new SeqWrapper(list)
assertEquals(jlstOf(1, 2, 3), jlst)
assertEquals(jlst, jlstOf(1, 2, 3))
assertTrue(jlst == jlstOf(1, 2, 3))
assertEquals(jlst.hashCode, jlst.hashCode)
}

@Test def `Set as JSet has equals`: Unit = {
val set = Set(1, 2, 3)
val jset = new SetWrapper(set)
assertEquals(jsetOf(1, 2, 3), jset)
assertEquals(jset, jsetOf(1, 2, 3))
assertTrue(jset == jsetOf(1, 2, 3))
assertEquals(jset.hashCode, jset.hashCode)
}

@Test def `Map as JMap has equals`: Unit = {
val map = Map(1 -> "one", 2 -> "two", 3 -> "three")
val jmap = new MapWrapper(map)
assertEquals(jmap, jmap)
}

@Test def `Anything as Collection is equal to Anything`: Unit = {
def set = Set(1, 2, 3)
def jset = new IterableWrapper(set)
assertTrue(jset == jset)
assertEquals(jset, jset)
assertNotEquals(jset, set)
assertEquals(jset.hashCode, jset.hashCode)
}

@Test def `Iterator wrapper does not compare equal`: Unit = {
def it = List(1, 2, 3).iterator
def jit = new IteratorWrapper(it)
assertNotEquals(jit, jit)
assertNotEquals(jit.hashCode, jit.hashCode)
}

@Test def `Anything.asScala Iterable has case equals`: Unit = {
def vs = jlstOf(42, 27, 37)
def it = new JListWrapper(vs)
assertEquals(it, it)
assertEquals(it.hashCode, it.hashCode)
}
}

0 comments on commit adb6dba

Please sign in to comment.