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

Commit 7c47a02

Browse files
committed
Add a MutableEnv
1 parent 0ee2ce6 commit 7c47a02

File tree

4 files changed

+212
-94
lines changed

4 files changed

+212
-94
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
<dependency>
8282
<groupId>com.ckkloverdos</groupId>
8383
<artifactId>maybe</artifactId>
84-
<version>0.6.0-SNAPSHOT</version>
84+
<version>0.5.0</version>
8585
</dependency>
8686
<dependency>
8787
<groupId>junit</groupId>

src/main/scala/com/ckkloverdos/env/Env.scala

+10-93
Original file line numberDiff line numberDiff line change
@@ -19,114 +19,31 @@ package com.ckkloverdos.env
1919
import com.ckkloverdos.maybe.{NoVal, Maybe}
2020
import com.ckkloverdos.key._
2121

22-
class Env private[env](private val map: Map[TypedKey[_], Any]) {
23-
/**
24-
* Get a value or throw an exception if it does not exist.
25-
*/
26-
@inline
27-
@throws(classOf[NoSuchElementException])
28-
@throws(classOf[ClassCastException])
29-
def getEx[T : Manifest](key: TypedKey[T]): T = {
30-
this apply key
31-
}
32-
33-
/**
34-
* Get a value or throw an exception if it does not exist.
35-
*/
36-
@throws(classOf[NoSuchElementException])
37-
@throws(classOf[ClassCastException])
38-
def apply[T : Manifest](key: TypedKey[T]): T = {
39-
map(key).asInstanceOf[T]
40-
}
41-
22+
final class Env private[env](private[env] val map: Map[TypedKey[_], Any]) extends EnvBase[Env] {
4223

43-
def get[T : Manifest](key: TypedKey[T]): Maybe[T] = {
44-
map.get(key) match {
45-
case Some(value)
46-
Maybe(value).asInstanceOf[Maybe[T]]
24+
protected def newEnv(map: Map[TypedKey[_], Any]): Env = new Env(map)
4725

48-
case None
49-
NoVal
50-
}
51-
}
52-
5326
def +[T : Manifest](key: TypedKey[T], value: T): Env = new Env(map + (key -> value))
5427

5528
def +[T : Manifest](kv: (TypedKey[T], T)): Env = new Env(map + kv)
5629

5730
def +(kv: (String, Env)): Env = this + (EnvKey(kv._1), kv._2)
5831

59-
def ++(other: Env): Env = new Env(other.map ++ map)
32+
def ++(other: EnvBase[_]): Env = new Env(other.map ++ map)
6033

61-
override def hashCode() = map.##
34+
def -[T: Manifest](key: TypedKey[T]): Env = new Env(map - key)
6235

63-
override def equals(any: Any): Boolean = {
64-
any match {
65-
case that: Env
66-
this.map == that.map && this.getClass == that.getClass
36+
def getRemove[T: Manifest](key: TypedKey[T]): (Option[T], Env) = {
37+
get(key) match {
38+
case some @ Some(_)
39+
(some, this - key)
6740

68-
case _
69-
false
41+
case None
42+
(None, this)
7043
}
7144
}
7245

7346
override def toString = "Env(%s)".format(map.mkString(", "))
74-
75-
def size = map.size
76-
77-
def keySet = map.keySet
78-
79-
def keysIterator = map.keysIterator
80-
81-
def keysOfName(keyName: String): Set[TypedKey[_]] = {
82-
for {
83-
typedKey <- map.keySet if(typedKey.name == keyName)
84-
} yield {
85-
typedKey
86-
}
87-
}
88-
89-
def selectName(keyName: String): Env =
90-
new Env(Map(keysOfName(keyName).toSeq.map(tk => (tk, map(tk))): _*))
91-
92-
def keysOfType[T : Manifest]: Set[TypedKey[T]] = {
93-
for {
94-
typedKey <- map.keySet if(manifest[T] == typedKey.keyType)
95-
} yield {
96-
typedKey.asInstanceOf[TypedKey[T]]
97-
}
98-
}
99-
100-
def selectType[T : Manifest]: Env =
101-
new Env(Map(keysOfType[T].toSeq.map(tk => (tk, map(tk))): _*))
102-
103-
104-
def contains[T : Manifest](key: TypedKey[T]): Boolean = {
105-
map.contains(key)
106-
}
107-
108-
/**
109-
* Returns a `Map` whose keys are the names of the typed keys. Beware that typed keys may have the same names,
110-
* so this can lead to loss of key-value pairs.
111-
*/
112-
def toMap: Map[String, Any] = {
113-
map.map{case (tk, vt) (tk.name, vt)}
114-
}
115-
116-
private[this] def fillJMap[M <: java.util.Map[String, AnyRef]](jmap: M): M = {
117-
for((tk, vt) map) {
118-
jmap.put(tk.name, vt.asInstanceOf[AnyRef])
119-
}
120-
jmap
121-
}
122-
123-
/**
124-
* Returns a [[java.util.Map]] whose keys are the names of the typed keys. Beware that typed keys may have the
125-
* same names, so this can lead to loss of key-value pairs.
126-
*/
127-
def toJavaMap: java.util.Map[String, AnyRef] = {
128-
fillJMap(new java.util.HashMap[String, AnyRef](size))
129-
}
13047
}
13148

13249
object Env {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright 2012 Christos KK Loverdos
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.ckkloverdos.env
18+
19+
import com.ckkloverdos.key.TypedKey
20+
21+
/**
22+
*
23+
* @author Christos KK Loverdos <loverdos@gmail.com>
24+
*/
25+
26+
abstract class EnvBase[E <: EnvBase[E]] { self: E
27+
private[env] def map: Map[TypedKey[_], Any]
28+
protected def newEnv(map: Map[TypedKey[_], Any]): E
29+
30+
def selectName(keyName: String): E =
31+
newEnv(Map(keysOfName(keyName).toSeq.map(tk => (tk, map(tk))): _*))
32+
33+
def selectType[T : Manifest]: E =
34+
newEnv(Map(keysOfType[T].toSeq.map(tk => (tk, map(tk))): _*))
35+
36+
def contains[T : Manifest](key: TypedKey[T]): Boolean = {
37+
map.contains(key)
38+
}
39+
40+
def keysOfType[T : Manifest]: Set[TypedKey[T]] = {
41+
for {
42+
typedKey <- map.keySet if(manifest[T] == typedKey.keyType)
43+
} yield {
44+
typedKey.asInstanceOf[TypedKey[T]]
45+
}
46+
}
47+
48+
/**
49+
* Returns a `Map` whose keys are the names of the typed keys. Beware that typed keys may have the same names,
50+
* so this can lead to loss of key-value pairs.
51+
*/
52+
def toMap: Map[String, Any] = {
53+
map.map{case (tk, vt) (tk.name, vt)}
54+
}
55+
56+
protected def fillJMap[M <: java.util.Map[String, AnyRef]](jmap: M): M = {
57+
for((tk, vt) map) {
58+
jmap.put(tk.name, vt.asInstanceOf[AnyRef])
59+
}
60+
jmap
61+
}
62+
63+
/**
64+
* Returns a [[java.util.Map]] whose keys are the names of the typed keys. Beware that typed keys may have the
65+
* same names, so this can lead to loss of key-value pairs.
66+
*/
67+
def toJavaMap: java.util.Map[String, AnyRef] = {
68+
fillJMap(new java.util.HashMap[String, AnyRef](size))
69+
}
70+
71+
/**
72+
* Get a value or throw an exception if it does not exist.
73+
*/
74+
@throws(classOf[NoSuchElementException])
75+
@throws(classOf[ClassCastException])
76+
def apply[T : Manifest](key: TypedKey[T]): T = {
77+
map(key).asInstanceOf[T]
78+
}
79+
80+
/**
81+
* Get a value or throw an exception if it does not exist.
82+
*/
83+
@inline
84+
@throws(classOf[NoSuchElementException])
85+
@throws(classOf[ClassCastException])
86+
def getEx[T : Manifest](key: TypedKey[T]): T = {
87+
this apply key
88+
}
89+
90+
def get[T : Manifest](key: TypedKey[T]): Option[T] = {
91+
map.get(key).asInstanceOf[Option[T]]
92+
}
93+
94+
def getOrElse[T : Manifest](key: TypedKey[T], default: T): T = {
95+
map.get(key) match {
96+
case Some(value)
97+
value.asInstanceOf[T]
98+
99+
case None
100+
default
101+
}
102+
}
103+
104+
def size = map.size
105+
106+
def keySet = map.keySet
107+
108+
def keysIterator = map.keysIterator
109+
110+
def keysOfName(keyName: String): Set[TypedKey[_]] = {
111+
for {
112+
typedKey <- map.keySet if(typedKey.name == keyName)
113+
} yield {
114+
typedKey
115+
}
116+
}
117+
118+
119+
override def hashCode() = map.##
120+
121+
override def equals(any: Any): Boolean = {
122+
any match {
123+
case that: EnvBase[_]
124+
this.map == that.map && this.getClass == that.getClass
125+
126+
case _
127+
false
128+
}
129+
}
130+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2012 Christos KK Loverdos
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.ckkloverdos.env
18+
19+
import com.ckkloverdos.key.TypedKey
20+
21+
/**
22+
*
23+
* @author Christos KK Loverdos <loverdos@gmail.com>
24+
*/
25+
26+
final class MutableEnv private[env](private[env] var map: Map[TypedKey[_], Any]) extends EnvBase[MutableEnv] {
27+
protected def newEnv(map: Map[TypedKey[_], Any]): MutableEnv = new MutableEnv(map)
28+
29+
def +[T : Manifest](key: TypedKey[T], value: T): this.type = {
30+
map += key -> value
31+
this
32+
}
33+
34+
def +[T : Manifest](kv: (TypedKey[T], T)): this.type = {
35+
map += kv
36+
this
37+
}
38+
39+
def +(kv: (String, Env)): this.type = {
40+
this + (EnvKey(kv._1), kv._2)
41+
}
42+
43+
def ++(other: EnvBase[_]): this.type = {
44+
map ++= other.map
45+
this
46+
}
47+
48+
def -[T: Manifest](key: TypedKey[T]): this.type = {
49+
map -= key
50+
this
51+
}
52+
53+
def getRemove[T: Manifest](key: TypedKey[T]): Option[T] = {
54+
get(key) match {
55+
case some @ Some(_)
56+
map -= key
57+
some
58+
59+
case None
60+
None
61+
}
62+
}
63+
64+
override def toString = "MutableEnv(%s)".format(map.mkString(", "))
65+
}
66+
67+
68+
object MutableEnv {
69+
def apply() = new MutableEnv(Map())
70+
}
71+

0 commit comments

Comments
 (0)