tags:

views:

223

answers:

1

Hello

I was trying to pull environment variables into a scala script using java Iterators and / or Enumerations and realised that Dr Frankenstein might claim parentage, so I hacked the following from the ugly tree instead:

import java.util.Map.Entry
import System._

val propSet = getProperties().entrySet().toArray()
val props   = (0 until propSet.size).foldLeft(Map[String, String]()){(m, i) =>
  val e = propSet(i).asInstanceOf[Entry[String, String]]
  m + (e.getKey() -> e.getValue())
}

For example to print the said same environment

props.keySet.toList.sortWith(_ < _).foreach{k =>
  println(k+(" " * (30 - k.length))+" = "+props(k))
}

Please, please don't set about polishing this t$#d, just show me the scala gem that I'm convinced exists for this situation (i.e java Properties --> scala.Map), thanks in advance ;@)

+5  A: 

Scala 2.7:

val props = Map() ++ scala.collection.jcl.Conversions.convertMap(System.getProperties).elements

Though that needs some typecasting. Let me work on it a bit more.

val props = Map() ++ scala.collection.jcl.Conversions.convertMap(System.getProperties).elements.asInstanceOf[Iterator[(String, String)]]

Ok, that was easy. Let me work on 2.8 now...

import scala.collection.JavaConversions.asMap
val props = System.getProperties() : scala.collection.mutable.Map[AnyRef, AnyRef] // or
val props = System.getProperties().asInstanceOf[java.util.Map[String, String]] : scala.collection.mutable.Map[String, String] // way too many repetitions of types
val props = asMap(System.getProperties().asInstanceOf[java.util.Map[String, String]])

The verbosity, of course, can be decreased with a couple of imports. First of all, note that Map will be a mutable map on 2.8. On the bright side, if you convert back the map, you'll get the original object.

Now, I have no clue why Properties implements Map<Object, Object>, given that the javadocs clearly state that key and value are String, but there you go. Having to typecast this makes the implicit option much less attractive. This being the case, the alternative is the most concise of them.

EDIT

Scala 2.8 just acquired an implicit conversion from Properties to mutable.Map[String,String], which makes most of that code moot.

Daniel
As a side note, this is the first time I do Java <-> Scala collection conversions. The last time I tried I wasn't experienced enough to get it to work. And not only did I get the chance to overcome my previous defeat, but I find this particular problem of much interest, as I find myself now and then perusing the system properties!
Daniel
Thanks Daniel, I'll give your answer a proper look later. I'm pleased to have provoked your interest in this topic.
Don Mackenzie
@Daniel, I suspect that this is the gem I was looking for, just a shame about the cast, especially as Properties should implement Map[String, String]. These conversions are a great find for me.
Don Mackenzie
Just to make clear, the cast is Java's implementation of Properties' fault, not Scala's.
Daniel
Daniel, the Chaotic Good Elf Mage defeats the old, gnarly, villainous beast which is the Java Properties construct. Sorry, but your first comment reminded me of an RPG...
Travis
No problem, but I'm more of a lawful good dwarf fighter, even if I do look like an elf. :-)
Daniel