javascalasecuresocial

Trying to convert a HashMap into a scala ListMap


So, there is a particular class, which I am overriding in a certain framework (not relevant, but it is the SecureSocial framework for Play). The method I am overriding has the signature:

In Scala:

override val providers : scala.collection.immutable.ListMap[scala.Predef.String, securesocial.core.IdentityProvider] = { /* compiled code */ }

So, I simply wrote it as:

@Override
ListMap<String, IdentityProvider> providers() { ... }

The problem is that the only thing I can do with ListMap from Java is:

new ListMap<>();

Which is not very useful. The Map that I want to override is being injected via a DI, and what I have is a java.util.HashMap. So, my method is looking like this:

@Override
ListMap<String, IdentityProvider> providers() {
    Map<String, IdentityProvider> providerMap = this.providerMap;
    toScalaListMap(providerMap);
}

Of course, toScalaListMap does not exist. How do I go about writing it? I've tried searching online and looking at the scala documentation, but given my zero knowledge of Scala as of now, I don't know where to begin. I am adding the securesocial tag in this question just in case.


Solution

  • The way I got around this is to use something like this:

    return (ListMap<String, IdentityProvider>)new ListMap<String, IdentityProvider>()
                .newBuilder()
                .$plus$eq(new Tuple2<>("provider1", provider1))
                .$plus$eq(new Tuple2<>("provider2", provider2))
                .result();
    

    Where Tuple2 is essentially scala.Tuple2. It is ugly, but it works!