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

Commit a134cdf

Browse files
committed
Make a few handy additions to Props
1 parent 33e5cca commit a134cdf

File tree

4 files changed

+120
-3
lines changed

4 files changed

+120
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ streamresource
33

44
Convenient abstractions related to the getResource() pattern. Introduces the idea of resource contexts, which are responsible for resource resolving.
55

6-
Latest release is `"com.ckkloverdos" % "streamresource_2.9.1" % "0.1.1"` from [scala-tools.org](http://scala-tools.org/repo-releases).
6+
Latest release is `"com.ckkloverdos" % "streamresource_2.9.1" % "0.2.0"` from [scala-tools.org](http://scala-tools.org/repo-releases).

project/build.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
project.organization=com.ckkloverdos
2020
project.name=streamresource
2121
sbt.version=0.7.7
22-
project.version=0.1.1
22+
project.version=0.2.0
2323
publish.remote=false
2424
build.scala.versions=2.9.1
2525
project.initialize=false

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

+65-1
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,44 @@ import com.ckkloverdos.maybe.{NoVal, Maybe}
3131
*/
3232
class Props(val map: Map[String, String]) {
3333

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+
3453
def get(key: String): Maybe[String] = map.get(key): Maybe[String]
3554

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+
3672
def getInt(key: String): Maybe[Int] = map.get(key) match {
3773
case Some(value) => Maybe(value.toInt)
3874
case None => NoVal
@@ -47,7 +83,12 @@ class Props(val map: Map[String, String]) {
4783
case Some(value) => Maybe(value.toDouble)
4884
case None => NoVal
4985
}
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+
5192
def getProps(key: String): Maybe[Props] = map.get(key) match {
5293
case Some(value) => Props(value)
5394
case None => NoVal
@@ -60,9 +101,30 @@ class Props(val map: Map[String, String]) {
60101

61102
def getTrimmedList(key: String, separatorRegex: String = "\\s*,\\s*"): List[String] =
62103
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(", "))
63123
}
64124

65125
object Props {
126+
lazy val DefaultFalseStrings = Set("false", "off", "0")
127+
66128
lazy val DummyWrappedURL = new URL("streamresource://wrapped")
67129
lazy val DummyWrappedPath = "wrapped"
68130

@@ -88,4 +150,6 @@ object Props {
88150
def apply(path: String, rc: StreamResourceContext = DefaultResourceContext): Maybe[Props] = {
89151
rc.getResource(path).flatMap(this(_))
90152
}
153+
154+
def apply(keyvals: (String, String)*): Props = new Props(Map(keyvals: _*))
91155
}

src/test/scala/com/ckkloverdos/resource/ResourceTest.scala

+53
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import org.junit.Assert
2020
import org.junit.Test
2121
import com.ckkloverdos.runmode.{StageMode, RunMode, TestMode, StandardRunModeContext}
2222
import com.ckkloverdos.props.Props
23+
import com.ckkloverdos.maybe.Just
2324

2425
/**
2526
*
@@ -201,4 +202,56 @@ class ResourceTest {
201202
val listColon = props.getTrimmedList(rcKeyListColon, "\\s*:\\s*")
202203
Assert.assertEquals(List("one", "two", "three"), listColon)
203204
}
205+
206+
@Test
207+
def testPropsBoolean {
208+
val key1val = ("key1", "on", true)
209+
val key2val = ("key2", "off", false)
210+
val key3val = ("key3", "0", false)
211+
val all = Seq(key1val, key2val, key3val)
212+
val keyvals = all map { case (k, v, _) => (k, v) }
213+
val props = Props(keyvals: _*)
214+
215+
all foreach { case (k, _, b) =>
216+
Assert.assertEquals("Testing boolean property: %s".format(k), Just(b), props.getBoolean(k))
217+
}
218+
}
219+
220+
@Test
221+
def testPropsEqual1 {
222+
val props1 = Props("1" -> "one", "2" -> "two")
223+
val props2 = Props("2" -> "two", "1" -> "one")
224+
Assert.assertEquals(props1, props2)
225+
Assert.assertEquals(props2, props1)
226+
}
227+
228+
@Test
229+
def testPropsEqual2 {
230+
class SuperProps(map: Map[String, String]) extends Props(map)
231+
val props1 = new SuperProps(Map("1" -> "one", "2" -> "two"))
232+
val props2 = Props("2" -> "two", "1" -> "one")
233+
Assert.assertTrue(props1.equalsProps(props2))
234+
Assert.assertTrue(props2.equalsProps(props1))
235+
}
236+
237+
@Test
238+
def testPropsNotEqual1 {
239+
val props1 = Props("1" -> "one", "2" -> "two")
240+
val props2 = Props("2" -> "two", "1" -> "one1")
241+
Assert.assertFalse(props1 == props2)
242+
}
243+
244+
@Test
245+
def testPropsNotEqual2 {
246+
val props1 = Props("1" -> "one", "2" -> "two")
247+
Assert.assertFalse(props1 == null)
248+
}
249+
250+
@Test
251+
def testPropsNotEqual3 {
252+
class SuperProps(map: Map[String, String]) extends Props(map)
253+
val props1 = new SuperProps(Map("1" -> "one", "2" -> "two"))
254+
val props2 = Props("2" -> "two", "1" -> "one")
255+
Assert.assertFalse(props1 == props2)
256+
}
204257
}

0 commit comments

Comments
 (0)