String and class-name utilities
authorChristos KK Loverdos <loverdos@gmail.com>
Fri, 27 Apr 2012 11:38:50 +0000 (14:38 +0300)
committerChristos KK Loverdos <loverdos@gmail.com>
Fri, 27 Apr 2012 11:38:50 +0000 (14:38 +0300)
src/main/scala/gr/grnet/aquarium/util/package.scala

index 6ce1645..286f560 100644 (file)
@@ -64,14 +64,25 @@ package object util {
     }
   }
 
+  def afterLastIndexOf(separator: String, input: String) = {
+    input.substring(input.lastIndexOf(separator) + 1)
+  }
   /**
    * Compute the class name excluding any leading packages.
    *
    * This is basically the name after the last dot.
    */
   def shortNameOfClass(theClass: Class[_]): String = {
-    val cname = theClass.getName
-    cname.substring(cname.lastIndexOf(".") + 1)
+    afterLastIndexOf(".", theClass.getName)
+  }
+
+  /**
+   * Compute the class name excluding any leading packages and any `$` prefixes.
+   *
+   * This is basically the name after the last dot and after any dollar sign.
+   */
+  def simpleNameOfClass(theClass: Class[_]): String = {
+    afterLastIndexOf("$", simpleNameOfClass(theClass))
   }
 
   /**
@@ -92,6 +103,15 @@ package object util {
     }
   }
 
+  /**
+   * Compute the class name excluding any leading packages and any `$` prefixes.
+   *
+   * This is basically the name after the last dot and after any dollar sign.
+   */
+  def simpleClassNameOf(anyRef: AnyRef) = {
+    afterLastIndexOf("$", shortClassNameOf(anyRef))
+  }
+
   def safeToStringOrNull(obj: AnyRef): String = obj match {
     case null => null
     case _ => obj.toString