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

Commit fc86641

Browse files
committed
Add apply() and change signature of toJavaMap()
1 parent 65bdcb1 commit fc86641

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.gradle/
12
target/
23
*.iml
34
lib_managed/

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

+22-4
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,23 @@ class Env private[env](private val map: Map[TypedKey[_], Any]) {
2323
/**
2424
* Get a value or throw an exception if it does not exist.
2525
*/
26+
@inline
2627
@throws(classOf[NoSuchElementException])
2728
@throws(classOf[ClassCastException])
2829
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 = {
2939
map(key).asInstanceOf[T]
3040
}
3141

42+
3243
def get[T : Manifest](key: TypedKey[T]): Maybe[T] = {
3344
map.get(key) match {
3445
case Some(value)
@@ -102,12 +113,19 @@ class Env private[env](private val map: Map[TypedKey[_], Any]) {
102113
map.map{case (tk, vt) (tk.name, vt)}
103114
}
104115

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+
105123
/**
106-
* Returns a [[java.util.Map]] whose keys are the names of the typed keys. Beware that typed keys may have the same
107-
* names, so this can lead to loss of key-value pairs.
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.
108126
*/
109-
def toJavaMap: java.util.Map[String, Any] = {
110-
scala.collection.JavaConversions.mapAsJavaMap(toMap)
127+
def toJavaMap: java.util.Map[String, AnyRef] = {
128+
fillJMap(new java.util.HashMap[String, AnyRef](size))
111129
}
112130
}
113131

0 commit comments

Comments
 (0)