@@ -20,102 +20,97 @@ import com.ckkloverdos.resource.{StreamResource, WrappingStreamResource, StreamR
20
20
import java .io .InputStream
21
21
import java .net .URL
22
22
import java .util .Properties
23
- import com .ckkloverdos .maybe .{NoVal , Maybe }
24
23
import com .ckkloverdos .convert .Converters
24
+ import com .ckkloverdos .maybe .{NoVal , Maybe }
25
+ import com .ckkloverdos .key .StringKey
26
+ import com .ckkloverdos .env .Env
25
27
26
28
/**
27
29
* Properties with conversion methods.
28
30
*
29
31
* @author Christos KK Loverdos <loverdos@gmail.com>.
30
32
*/
31
- class Props (val map : Map [String , String ])(implicit conv : Converters = Converters .DefaultConverters ) {
32
-
33
- private def _toBoolean (value : String , falseStrings : Set [String ]): Boolean = {
34
- if (null eq value) {
35
- throw new NullPointerException (" Cannot get a Boolean from null" )
36
- } else {
37
- ! (falseStrings contains value.toLowerCase)
38
- }
39
- }
40
-
41
- def converters = conv
42
-
43
- def contains (key : String ) = map contains key
44
-
45
- def filterKeys (f : String => Boolean ) = new Props (map filterKeys f)
46
-
47
- def filterValues (f : String => Boolean ) = new Props (map filter {case (k, v) => f(v)})
33
+ class Props (val map : Map [String , String ])(implicit conv : Converters = Converters .DefaultConverters ) extends PropsBase [String , String , Props ]{
34
+ def newProps (map : Props # MapType ) = new Props (map)
48
35
49
- /**
50
- * Get a value or throw an exception if it doesnot exist.
51
- */
52
- def getEx (key : String ): String = map apply key
36
+ def toAnyProps : AnyProps = new AnyProps (map)
53
37
54
- def get ( key : String ) : Maybe [ String ] = map.get(key) : Maybe [ String ]
38
+ val converters = conv
55
39
56
- def getOr (key : String , default : String = null ): String = map.getOrElse(key, default)
57
-
58
- def getBoolean (key : String , falseStrings : Set [String ] = Props .DefaultFalseStrings ): Maybe [Boolean ] = map.get(key) match {
59
- case Some (value) => Maybe (_toBoolean(value, falseStrings))
60
- case None => NoVal
40
+ def getBoolean (key : String ): Maybe [Boolean ] = map.get(key) match {
41
+ case Some (value) ⇒ conv.convertToBoolean(value)
42
+ case None ⇒ NoVal
61
43
}
62
44
63
45
def getByte (key : String ): Maybe [Byte ] = map.get(key) match {
64
- case Some (value) => conv.convertValueToByte (value)
65
- case None => NoVal
66
- }
46
+ case Some (value) ⇒ conv.convertToByte (value)
47
+ case None ⇒ NoVal
48
+ }
67
49
68
50
def getShort (key : String ): Maybe [Short ] = map.get(key) match {
69
- case Some (value) => conv.convertValueToShort (value)
70
- case None => NoVal
51
+ case Some (value) ⇒ conv.convertToShort (value)
52
+ case None ⇒ NoVal
71
53
}
72
54
73
55
def getInt (key : String ): Maybe [Int ] = map.get(key) match {
74
- case Some (value) => conv.convertValueToInt(conv )
75
- case None => NoVal
56
+ case Some (value) ⇒ conv.convertToInt(value )
57
+ case None ⇒ NoVal
76
58
}
77
59
78
60
def getLong (key : String ): Maybe [Long ] = map.get(key) match {
79
- case Some (value) => conv.convertValueToLong (value)
80
- case None => NoVal
61
+ case Some (value) ⇒ conv.convertToLong (value)
62
+ case None ⇒ NoVal
81
63
}
82
64
83
65
def getDouble (key : String ): Maybe [Double ] = map.get(key) match {
84
- case Some (value) => conv.convertValueToDouble (value)
85
- case None => NoVal
66
+ case Some (value) ⇒ conv.convertToDouble (value)
67
+ case None ⇒ NoVal
86
68
}
87
69
88
70
def getFloat (key : String ): Maybe [Float ] = map.get(key) match {
89
- case Some (value) => conv.convertValueToFloat (value)
90
- case None => NoVal
71
+ case Some (value) ⇒ conv.convertToFloat (value)
72
+ case None ⇒ NoVal
91
73
}
92
74
93
75
def getProps (key : String ): Maybe [Props ] = map.get(key) match {
94
- case Some (value) => conv.convertValue(value, manifest [Props ])
95
- case None => NoVal
76
+ case Some (value) ⇒ conv.convert [Props ](value )
77
+ case None ⇒ NoVal
96
78
}
97
79
98
80
def getList (key : String , separatorRegex : String = " \\ s*,\\ s*" ) = map.get(key) match {
99
- case Some (value) => value.split(separatorRegex).toList
100
- case None => Nil
81
+ case Some (value) ⇒ value.split(separatorRegex).toList
82
+ case None ⇒ Nil
101
83
}
102
84
103
85
def getTrimmedList (key : String , separatorRegex : String = " \\ s*,\\ s*" ): List [String ] =
104
86
getList(key, separatorRegex).map(_.trim).filter(_.length > 0 )
105
87
106
88
override def equals (any : Any ) = any match {
107
- case props : Props if (props.getClass == this .getClass) => props.map == this .map
108
- case _ => false
89
+ case props : Props if (props.getClass == this .getClass) ⇒ props.map == this .map
90
+ case _ ⇒ false
109
91
}
110
92
111
93
def equalsProps (other : Props ): Boolean = other match {
112
- case null => false
113
- case _ => equalsMap(other.map)
94
+ case null ⇒ false
95
+ case _ ⇒ equalsMap(other.map)
114
96
}
115
97
116
98
def equalsMap (other : Map [String , String ]): Boolean = other match {
117
- case null => false
118
- case _ => other == this .map
99
+ case null ⇒ false
100
+ case _ ⇒ other == this .map
101
+ }
102
+
103
+ def toEnv : Env = new Env (map map { case (k, v) ⇒ (StringKey (k), v)})
104
+
105
+ def group (keyPrefix : String ): Props = {
106
+ val dottedPrefix = keyPrefix + " ."
107
+ val newPairs = for {
108
+ key <- map.keysIterator if (key.startsWith(dottedPrefix))
109
+ } yield {
110
+ key.substring(dottedPrefix.length) -> map(key)
111
+ }
112
+
113
+ new Props (Map (newPairs.toSeq: _* ))
119
114
}
120
115
121
116
override def hashCode () = map.##
@@ -124,15 +119,13 @@ class Props(val map: Map[String, String])(implicit conv: Converters = Converters
124
119
}
125
120
126
121
object Props {
127
- lazy val DefaultFalseStrings = Set (" false" , " off" , " 0" )
128
-
129
122
lazy val DummyWrappedURL = new URL (" streamresource://wrapped" )
130
123
lazy val DummyWrappedPath = " wrapped"
131
124
132
125
lazy val empty = new Props (Map ())
133
126
134
127
def apply (rc : StreamResource )(implicit conv : Converters ): Maybe [Props ] = {
135
- rc.mapInputStream { in =>
128
+ rc.mapInputStream { in ⇒
136
129
val props = new java.util.Properties
137
130
props.load(in)
138
131
import collection .JavaConversions ._
0 commit comments