-
Notifications
You must be signed in to change notification settings - Fork 2
/
WrapperWrapping.scala
38 lines (32 loc) · 1.08 KB
/
WrapperWrapping.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package org.gerweck.scala.util.jdbc
import java.sql.Wrapper
/** A parent trait that should be used for all the JDBC wrappers.
*
* JDBC is designed to accommodate wrappers, and has a few special methods
* that can be used to penetrate wrappers to expose hidden interfaces. This
* trait provides standard implementations of those, plus implementations of
* `hashCode` and `equals`.
*
* @author Sarah Gerweck <sarah.a180@gmail.com>
*/
trait WrapperWrapping[+A <: Wrapper] extends Wrapper {
val inner: A
override def isWrapperFor(clazz: Class[_]): Boolean = {
(clazz isInstance inner) || (inner isWrapperFor clazz)
}
override def unwrap[T](clazz: Class[T]): T = {
if (clazz isInstance inner) {
inner.asInstanceOf[T]
} else {
inner.unwrap(clazz)
}
}
override def hashCode: Int = {
/* Randomly flip some of the bits to avoid collisions. */
inner.hashCode ^ 0xb92442c2
}
override def equals(that: Any): Boolean = that match {
case wc: WrapperWrapping[_] => inner equals wc.inner
case _ => inner equals that
}
}