Copyright
[aquarium] / src / main / scala / gr / grnet / aquarium / util / package.scala
1 /*
2  * Copyright 2011-2012 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials
15  *      provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * The views and conclusions contained in the software and
31  * documentation are those of the authors and should not be
32  * interpreted as representing official policies, either expressed
33  * or implied, of GRNET S.A.
34  */
35
36 package gr.grnet.aquarium
37
38 import com.ckkloverdos.maybe.{Failed, MaybeOption, Just, NoVal, Maybe}
39
40
41 /**
42  * Utility definitions.
43  *
44  * @author Christos KK Loverdos <loverdos@gmail.com>.
45  */
46 package object util {
47   def tryOption[A](f: => A): Option[A] = {
48     try Some(f)
49     catch {
50       case _: Exception => None
51     }
52   }
53
54   /**
55    * Compute the class name excluding any leading packages.
56    *
57    * This is basically the name after the last dot.
58    */
59   def shortNameOfClass(theClass: Class[_]): String = {
60     val cname = theClass.getName
61     cname.substring(cname.lastIndexOf(".") + 1)
62   }
63
64   /**
65    * For the class of the provided object, compute the class name excluding any leading packages.
66    *
67    * This is basically the name after the last dot.
68    *
69    * The `null` value is mapped to string `"null"`.
70    */
71   def shortClassNameOf(anyRef: AnyRef): String = {
72     anyRef match {
73       case null =>
74         "<null>"
75       case clz: Class[_] =>
76         shortNameOfClass(clz)
77       case obj =>
78         shortNameOfClass(obj.getClass)
79     }
80   }
81
82   def safeToStringOrNull(obj: AnyRef): String = obj match {
83     case null => null
84     case _ => obj.toString
85   }
86
87   def displayableObjectInfo(obj: AnyRef): String = {
88     "[%s] %s".format(obj.getClass, obj)
89   }
90
91   /**
92    * This basically turns an [[scala.Option]] into a [[com.ckkloverdos.maybe.Maybe]] when asking a
93    * [[scala.collection.Map]] for a key.
94    *
95    * @param map The input map.
96    * @param key The key we are interested in.
97    * @tparam A The type of keys.
98    * @tparam B The type of values.
99    *
100    * @return A [[com.ckkloverdos.maybe.Just]] if a value was found, a
101    *           [[com.ckkloverdos.maybe.NoVal]] if nothing was found and a
102    *           [[com.ckkloverdos.maybe.Failed]] if some error happened.
103    */
104   @inline
105   def findFromMapAsMaybe[A, B <: AnyRef](map: scala.collection.Map[A, B], key: A): Maybe[B] = Maybe {
106     map.get(key) match {
107       case Some(value) ⇒
108         value
109       case None ⇒
110         null.asInstanceOf[B]
111     }
112   }
113
114   @inline
115   def findAndRemoveFromMap[A, B <: AnyRef](map: scala.collection.mutable.Map[A, B], key: A): Maybe[B] = Maybe {
116     map.get(key) match {
117       case Some(value) ⇒
118         map -= key
119         value
120       case None ⇒
121         null.asInstanceOf[B]
122     }
123   }
124
125   // Dear scalac. Optimize this.
126   def nspaces(n: Int): String = {
127     ("" /: (1 to n)) ((string, _) => string + " ")
128   }
129
130   def rpad(s: String, size: Int) = {
131     s + nspaces((size - s.length()) max 0)
132   }
133   
134   def maxStringSize[A](trav: Traversable[A]): Int = {
135     (0 /: trav)(_ max _.toString.length)
136   }
137
138   /**
139    * Given a [[com.ckkloverdos.maybe.Maybe]] that is actually a [[com.ckkloverdos.maybe.Failed]], return the latter.
140    *
141    * Use this only when you are sure what the `maybe` contains, since the methods can break type safety.
142    *
143    * @param maybe
144    * @tparam A
145    * @return
146    */
147   def failedForSure[A](maybe: Maybe[A]): Failed = {
148     maybe.asInstanceOf[Failed]
149   }
150
151   /**
152    * Given a [[com.ckkloverdos.maybe.Maybe]] that is actually a [[com.ckkloverdos.maybe.Just]], return the latter.
153    *
154    * Use this only when you are sure what the `maybe` contains, since the methods can break type safety.
155    *
156    * @param maybe
157    * @tparam A
158    * @return
159    */
160   def justForSure[A](maybe: Maybe[A]): Just[A] = {
161     maybe.asInstanceOf[Just[A]]
162   }
163 }