Skip to content

Commit 983aed8

Browse files
committed
Define equality between converter wrappers
When converter destination collection does not have equality defined, we should override equals/hashcode. 2 wrappers with same underlying collection should be equal.
1 parent 93528af commit 983aed8

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

library/src/scala/collection/convert/JavaCollectionWrappers.scala

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,33 @@ private[collection] object JavaCollectionWrappers extends Serializable {
3434
def hasMoreElements = underlying.hasNext
3535
def nextElement() = underlying.next()
3636
override def remove() = throw new UnsupportedOperationException
37+
override def equals(other: Any): Boolean = other match {
38+
case that: IteratorWrapper[_] => this.underlying == that.underlying
39+
case _ => false
40+
}
41+
override def hashCode: Int = underlying.hashCode()
3742
}
3843

3944
@SerialVersionUID(3L)
4045
class JIteratorWrapper[A](val underlying: ju.Iterator[A]) extends AbstractIterator[A] with Iterator[A] with Serializable {
4146
def hasNext = underlying.hasNext
4247
def next() = underlying.next
48+
override def equals(other: Any): Boolean = other match {
49+
case that: JIteratorWrapper[_] => this.underlying == that.underlying
50+
case _ => false
51+
}
52+
override def hashCode: Int = underlying.hashCode()
4353
}
4454

4555
@SerialVersionUID(3L)
4656
class JEnumerationWrapper[A](val underlying: ju.Enumeration[A]) extends AbstractIterator[A] with Iterator[A] with Serializable {
4757
def hasNext = underlying.hasMoreElements
4858
def next() = underlying.nextElement
59+
override def equals(other: Any): Boolean = other match {
60+
case that: JEnumerationWrapper[_] => this.underlying == that.underlying
61+
case _ => false
62+
}
63+
override def hashCode: Int = underlying.hashCode()
4964
}
5065

5166
trait IterableWrapperTrait[A] extends ju.AbstractCollection[A] {
@@ -57,13 +72,11 @@ private[collection] object JavaCollectionWrappers extends Serializable {
5772

5873
@SerialVersionUID(3L)
5974
class IterableWrapper[A](val underlying: Iterable[A]) extends ju.AbstractCollection[A] with IterableWrapperTrait[A] with Serializable {
60-
import scala.runtime.Statics._
61-
override def equals(other: Any): Boolean =
62-
other match {
63-
case other: IterableWrapper[_] => underlying.equals(other.underlying)
64-
case _ => false
65-
}
66-
override def hashCode = finalizeHash(mix(mix(0xcafebabe, "IterableWrapper".hashCode), anyHash(underlying)), 1)
75+
override def equals(other: Any): Boolean = other match {
76+
case that: IterableWrapper[_] => this.underlying == that.underlying
77+
case _ => false
78+
}
79+
override def hashCode: Int = underlying.hashCode()
6780
}
6881

6982
@SerialVersionUID(3L)
@@ -74,6 +87,11 @@ private[collection] object JavaCollectionWrappers extends Serializable {
7487
def iterator = underlying.iterator.asScala
7588
override def iterableFactory = mutable.ArrayBuffer
7689
override def isEmpty: Boolean = !underlying.iterator().hasNext
90+
override def equals(other: Any): Boolean = other match {
91+
case that: JIterableWrapper[_] => this.underlying == that.underlying
92+
case _ => false
93+
}
94+
override def hashCode: Int = underlying.hashCode()
7795
}
7896

7997
@SerialVersionUID(3L)
@@ -86,6 +104,11 @@ private[collection] object JavaCollectionWrappers extends Serializable {
86104
override def knownSize: Int = if (underlying.isEmpty) 0 else super.knownSize
87105
override def isEmpty = underlying.isEmpty
88106
override def iterableFactory = mutable.ArrayBuffer
107+
override def equals(other: Any): Boolean = other match {
108+
case that: JCollectionWrapper[_] => this.underlying == that.underlying
109+
case _ => false
110+
}
111+
override def hashCode: Int = underlying.hashCode()
89112
}
90113

91114
@SerialVersionUID(3L)
@@ -254,7 +277,7 @@ private[collection] object JavaCollectionWrappers extends Serializable {
254277
def getKey = k
255278
def getValue = v
256279
def setValue(v1 : V) = self.put(k, v1)
257-
280+
258281
// It's important that this implementation conform to the contract
259282
// specified in the javadocs of java.util.Map.Entry.hashCode
260283
//

0 commit comments

Comments
 (0)