Permalink
Browse files

Things are more typesafe now

  • Loading branch information...
loverdos committed Apr 4, 2012
1 parent e8a30c1 commit 81376848ef050d33dcab39b284e5e8c393684771
View
@@ -26,7 +26,7 @@
<groupId>com.ckkloverdos</groupId>
<artifactId>typedkey</artifactId>
- <version>0.4.0-SNAPSHOT</version>
+ <version>0.5.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Typedkey</name>
@@ -81,7 +81,7 @@
<dependency>
<groupId>com.ckkloverdos</groupId>
<artifactId>maybe</artifactId>
- <version>0.4.0-SNAPSHOT</version>
+ <version>0.5.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
@@ -16,27 +16,35 @@
package com.ckkloverdos.env
-import com.ckkloverdos.key.TypedKey
import com.ckkloverdos.maybe.{NoVal, Maybe}
+import com.ckkloverdos.key._
-class Env(val map: Map[TypedKey[_], Any] = Map()) {
+class Env private[env](private val map: Map[TypedKey[_], Any]) {
/**
* Get a value or throw an exception if it does not exist.
*/
@throws(classOf[NoSuchElementException])
+ @throws(classOf[ClassCastException])
def getEx[T : Manifest](key: TypedKey[T]): T = {
map(key).asInstanceOf[T]
}
def get[T : Manifest](key: TypedKey[T]): Maybe[T] = {
map.get(key) match {
- case Some(value) Maybe(value.asInstanceOf[T])
- case None NoVal
+ case Some(value)
+ Maybe(value).castTo[T]
+ case None
+ NoVal
}
}
+
+ def +[T : Manifest](key: TypedKey[T], value: T): Env = new Env(map + (key -> value))
+
+ def +[T : Manifest](kv: (TypedKey[T], T)): Env = new Env(map + kv)
- def ++(other: Map[TypedKey[_], Any]): Env = new Env(map ++ other)
- def ++(other: Env): Env = new Env(this.map ++ other.map)
+ def +(kv: (String, Env)): Env = this + (EnvKey(kv._1), kv._2)
+
+ def ++(other: Env): Env = new Env(other.map ++ map)
override def hashCode() = map.##
override def equals(any: Any): Boolean = {
@@ -74,13 +82,14 @@ class Env(val map: Map[TypedKey[_], Any] = Map()) {
def selectType[T : Manifest]: Env =
new Env(Map(keysOfType[T].toSeq.map(tk => (tk, map(tk))): _*))
+
def contains[T : Manifest](key: TypedKey[T]): Boolean = {
map.contains(key)
}
+}
- def toBuilder = new EnvBuilder(map)
-
-
+object Env {
+ def apply() = new Env(Map())
}
@@ -1,41 +0,0 @@
-/*
- * Copyright 2011 Christos KK Loverdos
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.ckkloverdos.env
-
-import com.ckkloverdos.key.TypedKey
-
-class EnvBuilder(__map: Map[TypedKey[_], Any] = Map()) {
- @volatile
- private[this] var _map = __map
-
- def +[T : Manifest](key: TypedKey[T], value: T): this.type = {
- _map += key -> value
- this
- }
-
- def +[T: Manifest](kv: (TypedKey[T], T)): this.type = {
- _map += kv
- this
- }
-
- def ++(other: Env): this.type = {
- _map = _map ++ other.map
- this
- }
-
- def build: Env = new Env(_map)
-}
@@ -16,6 +16,10 @@
package com.ckkloverdos.key
+import com.ckkloverdos.env.Env
+import com.ckkloverdos.maybe.Maybe
+import com.ckkloverdos.key.TypedKey.FromEnvTypedKey
+
/**
* A key with a specific type attached.
*
@@ -43,12 +47,22 @@ abstract class TypedKeySkeleton[T: Manifest](val name: String) extends TypedKey[
case _ => false
}
- override def toString =
- {
- val cname = getClass.getName
- val shortName = cname.substring(cname.lastIndexOf('.') + 1)
- "%s[%s](%s)".format(shortName, keyType, name)
- }
+// override def toString =
+// {
+// val cname = getClass.getName
+// val shortName = cname.substring(cname.lastIndexOf('.') + 1)
+// "%s[%s](%s)".format(shortName, keyType, name)
+// }
def compare(that: TypedKey[_]) = this.name compareTo that.name
+}
+
+object TypedKey {
+ final class FromEnvTypedKey[T: Manifest](key: TypedKey[T]) {
+ def from(env: Env): Maybe[T] = env.get(key)
+ }
+
+ implicit def typedKeyWithFrom[T: Manifest](key: TypedKey[T]): FromEnvTypedKey[T] = {
+ new FromEnvTypedKey(key)
+ }
}
@@ -17,49 +17,57 @@ package com.ckkloverdos.env
import org.junit.Assert
import org.junit.Test
-import com.ckkloverdos.key.{IntKey, StringKey}
import com.ckkloverdos.maybe.Just
+import com.ckkloverdos.key.{DoubleKey, IntKey, StringKey}
/**
*
* @author Christos KK Loverdos <loverdos@gmail.com>
*/
class EnvTest {
- val Hello_World = "Hello.World"
- val Just_another_Key = "Just.another.Key"
+ val KeyName_1 = "it's.key"
+ val KeyName_2 = "just.another.key"
+ val KeyName_3 = "day.in.paradise.key"
- val strKey1 = StringKey(Hello_World)
- val strVal1 = "Value 1"
- val intKey1 = IntKey(Just_another_Key)
- val intVal1 = 12
- val strKey2 = StringKey(Just_another_Key)
- val strVal2 = "Value 2"
- val intKey2 = IntKey(Hello_World)
- val intVal2 = 500
+ val key1_str = StringKey(KeyName_1)
+ val val1_str = "Value 1"
+ val key2_int = IntKey(KeyName_2)
+ val val2_int = 12
+ val key3_str = StringKey(KeyName_2)
+ val val3_str = "Value 2"
+ val key4_int = IntKey(KeyName_1)
+ val val4_int = 500
+ val key5_double = DoubleKey(KeyName_3)
+ val val5_double = 1.0
- lazy val envb = new EnvBuilder() +
- (strKey1, strVal1) +
- (intKey1, intVal1) +
- (intKey2, intVal2) +
- (strKey2, strVal2)
-
- lazy val env = envb.build
+ val env = Env() +
+ (key1_str, val1_str) +
+ (key2_int, val2_int) +
+ (key3_str, val3_str) +
+ (key4_int, val4_int) +
+ (key5_double, val5_double)
@Test
def testStringKey: Unit = {
- val strValue = env.get(strKey1)
- Assert.assertEquals(Just(strVal1), strValue)
+ val strValue = env.get(key1_str)
+ Assert.assertEquals(Just(val1_str), strValue)
}
@Test
def testIntKey: Unit = {
- val intValue = env.get(intKey1)
- Assert.assertEquals(Just(intVal1), intValue)
+ val intValue = env.get(key2_int)
+ Assert.assertEquals(Just(val2_int), intValue)
+ }
+
+ @Test
+ def testDoubleKey: Unit = {
+ val doubleValue = env.get(key5_double)
+ Assert.assertEquals(Just(val5_double), doubleValue)
}
@Test
def testKeysOfType: Unit = {
- val expectedSet = Set(strKey1, strKey2)
+ val expectedSet = Set(key1_str, key3_str)
val computedSet = env.keysOfType[String]
Assert.assertEquals(expectedSet, computedSet)
}
@@ -69,15 +77,15 @@ class EnvTest {
val computedEnv = env.selectType[String]
Assert.assertEquals(2, computedEnv.size)
- Assert.assertFalse(strKey1 == strKey2)
- Assert.assertTrue(computedEnv.contains(strKey1))
- Assert.assertTrue(computedEnv.contains(strKey2))
+ Assert.assertFalse(key1_str == key3_str)
+ Assert.assertTrue(computedEnv.contains(key1_str))
+ Assert.assertTrue(computedEnv.contains(key3_str))
}
@Test
def testKeysOfName: Unit = {
- val expectedSet = Set(strKey1, intKey2)
- val computedSet = env.keysOfName(Hello_World)
+ val expectedSet = Set(key1_str, key4_int)
+ val computedSet = env.keysOfName(KeyName_1)
Assert.assertEquals(expectedSet, computedSet)
}
}

0 comments on commit 8137684

Please sign in to comment.