Skip to content
This repository was archived by the owner on Aug 17, 2019. It is now read-only.

Commit 0ac87b1

Browse files
committed
Add a few type-specific converters that throw exceptions
1 parent 7613371 commit 0ac87b1

File tree

1 file changed

+128
-26
lines changed

1 file changed

+128
-26
lines changed

src/main/scala/com/ckkloverdos/props/Props.scala

+128-26
Original file line numberDiff line numberDiff line change
@@ -43,66 +43,168 @@ class Props(val map: Map[String, String])(implicit conv: Converters = Converters
4343
}
4444

4545
def getBoolean(key: String): Maybe[Boolean] = map.get(key) match {
46-
case Some(value) conv.convertToBoolean(value)
47-
case None NoVal
46+
case Some(value)
47+
conv.convertToBoolean(value)
48+
49+
case None
50+
NoVal
51+
}
52+
53+
def getBooleanEx(key: String): Boolean = map.get(key) match {
54+
case Some(value)
55+
conv.convertToBooleanEx(value)
56+
57+
case None
58+
throw new IllegalArgumentException("Unknown key %s".format(key))
4859
}
4960

5061
def getByte(key: String): Maybe[Byte] = map.get(key) match {
51-
case Some(value) conv.convertToByte(value)
52-
case None NoVal
62+
case Some(value)
63+
conv.convertToByte(value)
64+
65+
case None
66+
NoVal
67+
}
68+
69+
def getByteEx(key: String): Byte = map.get(key) match {
70+
case Some(value)
71+
conv.convertToByteEx(value)
72+
73+
case None
74+
throw new IllegalArgumentException("Unknown key %s".format(key))
5375
}
5476

5577
def getShort(key: String): Maybe[Short] = map.get(key) match {
56-
case Some(value) conv.convertToShort(value)
57-
case None NoVal
78+
case Some(value)
79+
conv.convertToShort(value)
80+
81+
case None
82+
NoVal
83+
}
84+
85+
def getShortEx(key: String): Short = map.get(key) match {
86+
case Some(value)
87+
conv.convertToShortEx(value)
88+
89+
case None
90+
throw new IllegalArgumentException("Unknown key %s".format(key))
5891
}
5992

6093
def getInt(key: String): Maybe[Int] = map.get(key) match {
61-
case Some(value) conv.convertToInt(value)
62-
case None NoVal
94+
case Some(value)
95+
conv.convertToInt(value)
96+
97+
case None
98+
NoVal
99+
}
100+
101+
def getIntEx(key: String): Int = map.get(key) match {
102+
case Some(value)
103+
conv.convertToIntEx(value)
104+
105+
case None
106+
throw new IllegalArgumentException("Unknown key %s".format(key))
63107
}
64108

65109
def getLong(key: String): Maybe[Long] = map.get(key) match {
66-
case Some(value) conv.convertToLong(value)
67-
case None NoVal
110+
case Some(value)
111+
conv.convertToLong(value)
112+
113+
case None
114+
NoVal
68115
}
69-
116+
117+
def getLongEx(key: String): Long = map.get(key) match {
118+
case Some(value)
119+
conv.convertToLongEx(value)
120+
121+
case None
122+
throw new IllegalArgumentException("Unknown key %s".format(key))
123+
}
124+
70125
def getDouble(key: String): Maybe[Double] = map.get(key) match {
71-
case Some(value) conv.convertToDouble(value)
72-
case None NoVal
126+
case Some(value)
127+
conv.convertToDouble(value)
128+
129+
case None
130+
NoVal
131+
}
132+
133+
def getDoubleEx(key: String): Double = map.get(key) match {
134+
case Some(value)
135+
conv.convertToDoubleEx(value)
136+
137+
case None
138+
throw new IllegalArgumentException("Unknown key %s".format(key))
73139
}
74140

75141
def getFloat(key: String): Maybe[Float] = map.get(key) match {
76-
case Some(value) conv.convertToFloat(value)
77-
case None NoVal
142+
case Some(value)
143+
conv.convertToFloat(value)
144+
145+
case None
146+
NoVal
147+
}
148+
149+
def getFloatEx(key: String): Float = map.get(key) match {
150+
case Some(value)
151+
conv.convertToFloatEx(value)
152+
153+
case None
154+
throw new IllegalArgumentException("Unknown key %s".format(key))
78155
}
79156

80157
def getProps(key: String): Maybe[Props] = map.get(key) match {
81-
case Some(value) conv.convert[Props](value)
82-
case None NoVal
158+
case Some(value)
159+
conv.convert[Props](value)
160+
161+
case None
162+
NoVal
83163
}
164+
165+
def getPropsEx(key: String): Props = map.get(key) match {
166+
case Some(value)
167+
conv.convertEx[Props](value)
168+
169+
case None
170+
throw new IllegalArgumentException("Unknown key %s".format(key))
171+
}
172+
84173

85174
def getList(key: String, separatorRegex: String = "\\s*,\\s*") = map.get(key) match {
86-
case Some(value) value.split(separatorRegex).toList
87-
case None Nil
175+
case Some(value)
176+
value.split(separatorRegex).toList
177+
178+
case None
179+
Nil
88180
}
89181

90-
def getTrimmedList(key: String, separatorRegex: String = "\\s*,\\s*"): List[String] =
182+
def getTrimmedList(key: String, separatorRegex: String = "\\s*,\\s*"): List[String] = {
91183
getList(key, separatorRegex).map(_.trim).filter(_.length > 0)
184+
}
92185

93186
override def equals(any: Any) = any match {
94-
case props: Props if(props.getClass == this.getClass) props.map == this.map
95-
case _ false
187+
case props: Props if(props.getClass == this.getClass)
188+
props.map == this.map
189+
190+
case _
191+
false
96192
}
97193

98194
def equalsProps(other: Props): Boolean = other match {
99-
case null false
100-
case _ equalsMap(other.map)
195+
case null
196+
false
197+
198+
case _
199+
equalsMap(other.map)
101200
}
102201

103202
def equalsMap(other: Map[String, String]): Boolean = other match {
104-
case null false
105-
case _ other == this.map
203+
case null
204+
false
205+
206+
case _
207+
other == this.map
106208
}
107209

108210
def toEnv: Env = {

0 commit comments

Comments
 (0)