Scala Library
|
|
scala/util/Hashable.scala
]
trait
Hashable
extends
AnyRef
A convenience trait for simplifying hashCode creation.
Mix this into a class and define val hashValues = Seq(x1, x2, ...)
and your hashCode
will be derived from those values.
If you define equals
in terms of equalHashValues
then your hashCode
and equals
methods will
never be out of sync. Something like:
override def equals(other: Any) = other match { case x: YourClass => this equalHashValues x case _ => false }
Method Summary | |
protected def
|
equalHashValues (other : Any) : Boolean |
override def
|
hashCode
: Int
Returns a hash code value for the object.
|
protected def
|
hashSeed : Int |
protected abstract def
|
hashValues : Seq[Any] |
Methods inherited from AnyRef | |
getClass, equals, clone, toString, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized |
Methods inherited from Any | |
==, !=, isInstanceOf, asInstanceOf |
Method Details |
protected
def
hashSeed : Int
override
def
hashCode : Int
The default hashing algorithm is platform dependent.
Note that it is allowed for two objects to have identical hash
codes (o1.hashCode.equals(o2.hashCode)
) yet not be
equal (o1.equals(o2)
returns false
). A
degenerate implementation could always return 0
.
However, it is required that if two objects are equal
(o1.equals(o2)
returns true
) that they
have identical hash codes
(o1.hashCode.equals(o2.hashCode)
). Therefore, when
overriding this method, be sure to verify that the behavior is
consistent with the equals
method.
Scala Library
|
|