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

Commit 65bdcb1

Browse files
committed
Add conversion to Map (Scala and Java)
1 parent 8514fbf commit 65bdcb1

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

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

+24-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Env private[env](private val map: Map[TypedKey[_], Any]) {
3333
map.get(key) match {
3434
case Some(value)
3535
Maybe(value).asInstanceOf[Maybe[T]]
36-
36+
3737
case None
3838
NoVal
3939
}
@@ -48,12 +48,17 @@ class Env private[env](private val map: Map[TypedKey[_], Any]) {
4848
def ++(other: Env): Env = new Env(other.map ++ map)
4949

5050
override def hashCode() = map.##
51+
5152
override def equals(any: Any): Boolean = {
5253
any match {
53-
case that: Env this.map == that.map && this.getClass == that.getClass
54-
case _ false
54+
case that: Env
55+
this.map == that.map && this.getClass == that.getClass
56+
57+
case _
58+
false
5559
}
5660
}
61+
5762
override def toString = "Env(%s)".format(map.mkString(", "))
5863

5964
def size = map.size
@@ -88,6 +93,22 @@ class Env private[env](private val map: Map[TypedKey[_], Any]) {
8893
def contains[T : Manifest](key: TypedKey[T]): Boolean = {
8994
map.contains(key)
9095
}
96+
97+
/**
98+
* Returns a `Map` whose keys are the names of the typed keys. Beware that typed keys may have the same names,
99+
* so this can lead to loss of key-value pairs.
100+
*/
101+
def toMap: Map[String, Any] = {
102+
map.map{case (tk, vt) (tk.name, vt)}
103+
}
104+
105+
/**
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.
108+
*/
109+
def toJavaMap: java.util.Map[String, Any] = {
110+
scala.collection.JavaConversions.mapAsJavaMap(toMap)
111+
}
91112
}
92113

93114
object Env {

0 commit comments

Comments
 (0)