pekko/akka-actor/src/main/scala/akka/util/Helpers.scala

61 lines
1.7 KiB
Scala
Raw Normal View History

2009-03-22 17:26:42 +01:00
/**
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
2009-03-22 17:26:42 +01:00
*/
package akka.util
2009-03-22 17:26:42 +01:00
2011-09-08 11:02:17 +02:00
import java.io.{ PrintWriter, StringWriter }
import java.util.Comparator
import scala.annotation.tailrec
2009-03-22 17:26:42 +01:00
/**
* @author <a href="http://jonasboner.com">Jonas Bon&#233;r</a>
*/
2011-02-28 22:54:32 +01:00
object Helpers {
2009-03-22 17:26:42 +01:00
def compareIdentityHash(a: AnyRef, b: AnyRef): Int = {
/*
* make sure that there is no overflow or underflow in comparisons, so
* that the ordering is actually consistent and you cannot have a
* sequence which cyclically is monotone without end.
*/
val diff = ((System.identityHashCode(a) & 0xffffffffL) - (System.identityHashCode(b) & 0xffffffffL))
if (diff > 0) 1 else if (diff < 0) -1 else 0
}
val IdentityHashComparator = new Comparator[AnyRef] {
def compare(a: AnyRef, b: AnyRef): Int = compareIdentityHash(a, b)
}
final val base64chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789*?"
@tailrec
def base64(l: Long, sb: StringBuilder = new StringBuilder("$")): String = {
sb += base64chars.charAt(l.toInt & 63)
val next = l >>> 6
if (next == 0) sb.toString
else base64(next, sb)
}
def ignore[E: Manifest](body: Unit) {
try {
body
} catch {
case e if manifest[E].erasure.isAssignableFrom(e.getClass) ()
}
}
def withPrintStackTraceOnError(body: Unit) {
try {
body
} catch {
case e: Throwable
2011-09-08 11:02:17 +02:00
val sw = new java.io.StringWriter()
var root = e
while (root.getCause ne null) root = e.getCause
root.printStackTrace(new java.io.PrintWriter(sw))
System.err.println(sw.toString)
throw e
}
}
}