Skip to content
This repository has been archived by the owner on Apr 25, 2020. It is now read-only.

Commit

Permalink
Things are more typesafe now
Browse files Browse the repository at this point in the history
  • Loading branch information
loverdos committed Apr 4, 2012
1 parent e8a30c1 commit 8137684
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 86 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -26,7 +26,7 @@


<groupId>com.ckkloverdos</groupId> <groupId>com.ckkloverdos</groupId>
<artifactId>typedkey</artifactId> <artifactId>typedkey</artifactId>
<version>0.4.0-SNAPSHOT</version> <version>0.5.0-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>


<name>Typedkey</name> <name>Typedkey</name>
Expand Down Expand Up @@ -81,7 +81,7 @@
<dependency> <dependency>
<groupId>com.ckkloverdos</groupId> <groupId>com.ckkloverdos</groupId>
<artifactId>maybe</artifactId> <artifactId>maybe</artifactId>
<version>0.4.0-SNAPSHOT</version> <version>0.5.0-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
Expand Down
27 changes: 18 additions & 9 deletions src/main/scala/com/ckkloverdos/env/Env.scala
Expand Up @@ -16,27 +16,35 @@


package com.ckkloverdos.env package com.ckkloverdos.env


import com.ckkloverdos.key.TypedKey
import com.ckkloverdos.maybe.{NoVal, Maybe} 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. * Get a value or throw an exception if it does not exist.
*/ */
@throws(classOf[NoSuchElementException]) @throws(classOf[NoSuchElementException])
@throws(classOf[ClassCastException])
def getEx[T : Manifest](key: TypedKey[T]): T = { def getEx[T : Manifest](key: TypedKey[T]): T = {
map(key).asInstanceOf[T] map(key).asInstanceOf[T]
} }


def get[T : Manifest](key: TypedKey[T]): Maybe[T] = { def get[T : Manifest](key: TypedKey[T]): Maybe[T] = {
map.get(key) match { map.get(key) match {
case Some(value) Maybe(value.asInstanceOf[T]) case Some(value)
case None NoVal 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 +(kv: (String, Env)): Env = this + (EnvKey(kv._1), kv._2)
def ++(other: Env): Env = new Env(this.map ++ other.map)
def ++(other: Env): Env = new Env(other.map ++ map)


override def hashCode() = map.## override def hashCode() = map.##
override def equals(any: Any): Boolean = { override def equals(any: Any): Boolean = {
Expand Down Expand Up @@ -74,13 +82,14 @@ class Env(val map: Map[TypedKey[_], Any] = Map()) {


def selectType[T : Manifest]: Env = def selectType[T : Manifest]: Env =
new Env(Map(keysOfType[T].toSeq.map(tk => (tk, map(tk))): _*)) new Env(Map(keysOfType[T].toSeq.map(tk => (tk, map(tk))): _*))



def contains[T : Manifest](key: TypedKey[T]): Boolean = { def contains[T : Manifest](key: TypedKey[T]): Boolean = {
map.contains(key) map.contains(key)
} }
}


def toBuilder = new EnvBuilder(map) object Env {

def apply() = new Env(Map())

} }


41 changes: 0 additions & 41 deletions src/main/scala/com/ckkloverdos/env/EnvBuilder.scala

This file was deleted.

26 changes: 20 additions & 6 deletions src/main/scala/com/ckkloverdos/key/typedkey.scala
Expand Up @@ -16,6 +16,10 @@


package com.ckkloverdos.key 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. * A key with a specific type attached.
* *
Expand Down Expand Up @@ -43,12 +47,22 @@ abstract class TypedKeySkeleton[T: Manifest](val name: String) extends TypedKey[
case _ => false case _ => false
} }


override def toString = // override def toString =
{ // {
val cname = getClass.getName // val cname = getClass.getName
val shortName = cname.substring(cname.lastIndexOf('.') + 1) // val shortName = cname.substring(cname.lastIndexOf('.') + 1)
"%s[%s](%s)".format(shortName, keyType, name) // "%s[%s](%s)".format(shortName, keyType, name)
} // }


def compare(that: TypedKey[_]) = this.name compareTo that.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)
}
} }
64 changes: 36 additions & 28 deletions src/test/scala/com/ckkloverdos/env/EnvTest.scala
Expand Up @@ -17,49 +17,57 @@ package com.ckkloverdos.env


import org.junit.Assert import org.junit.Assert
import org.junit.Test import org.junit.Test
import com.ckkloverdos.key.{IntKey, StringKey}
import com.ckkloverdos.maybe.Just import com.ckkloverdos.maybe.Just
import com.ckkloverdos.key.{DoubleKey, IntKey, StringKey}


/** /**
* *
* @author Christos KK Loverdos <loverdos@gmail.com> * @author Christos KK Loverdos <loverdos@gmail.com>
*/ */
class EnvTest { class EnvTest {
val Hello_World = "Hello.World" val KeyName_1 = "it's.key"
val Just_another_Key = "Just.another.Key" val KeyName_2 = "just.another.key"
val KeyName_3 = "day.in.paradise.key"


val strKey1 = StringKey(Hello_World) val key1_str = StringKey(KeyName_1)
val strVal1 = "Value 1" val val1_str = "Value 1"
val intKey1 = IntKey(Just_another_Key) val key2_int = IntKey(KeyName_2)
val intVal1 = 12 val val2_int = 12
val strKey2 = StringKey(Just_another_Key) val key3_str = StringKey(KeyName_2)
val strVal2 = "Value 2" val val3_str = "Value 2"
val intKey2 = IntKey(Hello_World) val key4_int = IntKey(KeyName_1)
val intVal2 = 500 val val4_int = 500
val key5_double = DoubleKey(KeyName_3)
val val5_double = 1.0


lazy val envb = new EnvBuilder() + val env = Env() +
(strKey1, strVal1) + (key1_str, val1_str) +
(intKey1, intVal1) + (key2_int, val2_int) +
(intKey2, intVal2) + (key3_str, val3_str) +
(strKey2, strVal2) (key4_int, val4_int) +

(key5_double, val5_double)
lazy val env = envb.build


@Test @Test
def testStringKey: Unit = { def testStringKey: Unit = {
val strValue = env.get(strKey1) val strValue = env.get(key1_str)
Assert.assertEquals(Just(strVal1), strValue) Assert.assertEquals(Just(val1_str), strValue)
} }


@Test @Test
def testIntKey: Unit = { def testIntKey: Unit = {
val intValue = env.get(intKey1) val intValue = env.get(key2_int)
Assert.assertEquals(Just(intVal1), intValue) Assert.assertEquals(Just(val2_int), intValue)
}

@Test
def testDoubleKey: Unit = {
val doubleValue = env.get(key5_double)
Assert.assertEquals(Just(val5_double), doubleValue)
} }


@Test @Test
def testKeysOfType: Unit = { def testKeysOfType: Unit = {
val expectedSet = Set(strKey1, strKey2) val expectedSet = Set(key1_str, key3_str)
val computedSet = env.keysOfType[String] val computedSet = env.keysOfType[String]
Assert.assertEquals(expectedSet, computedSet) Assert.assertEquals(expectedSet, computedSet)
} }
Expand All @@ -69,15 +77,15 @@ class EnvTest {
val computedEnv = env.selectType[String] val computedEnv = env.selectType[String]


Assert.assertEquals(2, computedEnv.size) Assert.assertEquals(2, computedEnv.size)
Assert.assertFalse(strKey1 == strKey2) Assert.assertFalse(key1_str == key3_str)
Assert.assertTrue(computedEnv.contains(strKey1)) Assert.assertTrue(computedEnv.contains(key1_str))
Assert.assertTrue(computedEnv.contains(strKey2)) Assert.assertTrue(computedEnv.contains(key3_str))
} }


@Test @Test
def testKeysOfName: Unit = { def testKeysOfName: Unit = {
val expectedSet = Set(strKey1, intKey2) val expectedSet = Set(key1_str, key4_int)
val computedSet = env.keysOfName(Hello_World) val computedSet = env.keysOfName(KeyName_1)
Assert.assertEquals(expectedSet, computedSet) Assert.assertEquals(expectedSet, computedSet)
} }
} }

0 comments on commit 8137684

Please sign in to comment.