@@ -31,8 +31,44 @@ import com.ckkloverdos.maybe.{NoVal, Maybe}
31
31
*/
32
32
class Props (val map : Map [String , String ]) {
33
33
34
+ private def _toBoolean (value : String , falseStrings : Set [String ]): Boolean = {
35
+ if (null eq value) {
36
+ throw new NullPointerException (" Cannot get a Boolean from null" )
37
+ } else {
38
+ ! (falseStrings contains value.toLowerCase)
39
+ }
40
+ }
41
+
42
+ def contains (key : String ) = map contains key
43
+
44
+ def filterKeys (f : String => Boolean ) = new Props (map filterKeys f)
45
+
46
+ def filterValues (f : String => Boolean ) = new Props (map filter {case (k, v) => f(v)})
47
+
48
+ /**
49
+ * Get a value or throw an exception if it doesnot exist.
50
+ */
51
+ def getEx (key : String ): String = map apply key
52
+
34
53
def get (key : String ): Maybe [String ] = map.get(key): Maybe [String ]
35
54
55
+ def getOr (key : String , default : String = null ): String = map.getOrElse(key, default)
56
+
57
+ def getBoolean (key : String , falseStrings : Set [String ] = Props .DefaultFalseStrings ): Maybe [Boolean ] = map.get(key) match {
58
+ case Some (value) => Maybe (_toBoolean(value, falseStrings))
59
+ case None => NoVal
60
+ }
61
+
62
+ def getByte (key : String ): Maybe [Byte ] = map.get(key) match {
63
+ case Some (value) => Maybe (value.toByte)
64
+ case None => NoVal
65
+ }
66
+
67
+ def getShort (key : String ): Maybe [Short ] = map.get(key) match {
68
+ case Some (value) => Maybe (value.toShort)
69
+ case None => NoVal
70
+ }
71
+
36
72
def getInt (key : String ): Maybe [Int ] = map.get(key) match {
37
73
case Some (value) => Maybe (value.toInt)
38
74
case None => NoVal
@@ -47,7 +83,12 @@ class Props(val map: Map[String, String]) {
47
83
case Some (value) => Maybe (value.toDouble)
48
84
case None => NoVal
49
85
}
50
-
86
+
87
+ def getFloat (key : String ): Maybe [Float ] = map.get(key) match {
88
+ case Some (value) => Maybe (value.toFloat)
89
+ case None => NoVal
90
+ }
91
+
51
92
def getProps (key : String ): Maybe [Props ] = map.get(key) match {
52
93
case Some (value) => Props (value)
53
94
case None => NoVal
@@ -60,9 +101,30 @@ class Props(val map: Map[String, String]) {
60
101
61
102
def getTrimmedList (key : String , separatorRegex : String = " \\ s*,\\ s*" ): List [String ] =
62
103
getList(key, separatorRegex).map(_.trim).filter(_.length > 0 )
104
+
105
+ override def equals (any : Any ) = any match {
106
+ case props : Props if (props.getClass == this .getClass) => props.map == this .map
107
+ case _ => false
108
+ }
109
+
110
+ def equalsProps (other : Props ): Boolean = other match {
111
+ case null => false
112
+ case _ => equalsMap(other.map)
113
+ }
114
+
115
+ def equalsMap (other : Map [String , String ]): Boolean = other match {
116
+ case null => false
117
+ case _ => other == this .map
118
+ }
119
+
120
+ override def hashCode () = map.##
121
+
122
+ override def toString = " Props(%s)" .format(map.mkString(" , " ))
63
123
}
64
124
65
125
object Props {
126
+ lazy val DefaultFalseStrings = Set (" false" , " off" , " 0" )
127
+
66
128
lazy val DummyWrappedURL = new URL (" streamresource://wrapped" )
67
129
lazy val DummyWrappedPath = " wrapped"
68
130
@@ -88,4 +150,6 @@ object Props {
88
150
def apply (path : String , rc : StreamResourceContext = DefaultResourceContext ): Maybe [Props ] = {
89
151
rc.getResource(path).flatMap(this (_))
90
152
}
153
+
154
+ def apply (keyvals : (String , String )* ): Props = new Props (Map (keyvals : _* ))
91
155
}
0 commit comments