Last active
April 18, 2017 19:16
-
-
Save samschlegel/b8e351a8d0f496cfe5d464915128c7de to your computer and use it in GitHub Desktop.
spotify/scio#546 - JMapWrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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