Skip to content

Instantly share code, notes, and snippets.

@samschlegel
Last active April 18, 2017 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samschlegel/b8e351a8d0f496cfe5d464915128c7de to your computer and use it in GitHub Desktop.
Save samschlegel/b8e351a8d0f496cfe5d464915128c7de to your computer and use it in GitHub Desktop.
spotify/scio#546 - JMapWrapper
package com.spotify.scio.util
import java.lang.{Iterable => JIterable}
import java.util.{Map => JMap}
import scala.collection.JavaConverters._
private[scio] object JMapWrapper {
def of[A, B](j: JMap[A, JIterable[B]]): Map[A, Iterable[B]] = {
new Map[A, Iterable[B]] {
// scalastyle:off method.name
override def +[B1 >: Iterable[B]](kv: (A, B1)): Map[A, B1] =
j.asScala.mapValues(_.asScala).toMap + kv
override def -(key: A): Map[A, Iterable[B]] =
j.asScala.mapValues(_.asScala).toMap - key
// scalastyle:on method.name
override def get(key: A): Option[Iterable[B]] = Option(j.get(key)).map(_.asScala)
override def iterator: Iterator[(A, Iterable[B])] =
j.asScala.iterator.map(kv => (kv._1, kv._2.asScala))
}
}
def of[K, V](j: JMap[K, V]): Map[K, V] = {
new Map[K, V] {
// scalastyle:off method.name
override def +[B1 >: V](kv: (K, B1)): Map[K, B1] = j.asScala.toMap + kv
override def -(key: K): Map[K, V] = j.asScala.toMap - key
// scalastyle:on method.name
override def get(key: K): Option[V] = Option(j.get(key))
override def iterator: Iterator[(K, V)] = j.asScala.iterator
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment