Created
February 12, 2015 20:13
-
-
Save fedesilva/cf5d96135423dd54c578 to your computer and use it in GitHub Desktop.
Java 8 Interop
This file contains hidden or 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.batanga.eng.interop | |
import java.util.function.{BiFunction, BiPredicate, Function => JFunction, Predicate => JPredicate} | |
import java.util.stream.{Stream => JStream} | |
import scala.collection.JavaConverters._ | |
/** Interoperability with java functional interfaces and stream api. | |
* Created by f on 10/30/14. | |
*/ | |
object Implicits { | |
//usage example: `i: Int ⇒ 42` | |
implicit def toJavaFunction[A, B](f: (A) => B): JFunction[A, B] = new JFunction[A, B] { | |
override def apply(a: A): B = f(a) | |
} | |
implicit def toJavaBiFunction[A, B, C](f: (A, B) => C): BiFunction[A, B, C] = new BiFunction[A, B, C] { | |
override def apply(a: A, b: B): C = f(a,b) | |
} | |
//usage example: `i: Int ⇒ true` | |
implicit def toJavaPredicate[A](f: (A) => Boolean): JPredicate[A] = new JPredicate[A] { | |
override def test(a: A): Boolean = f(a) | |
} | |
//usage example: `(i: Int, s: String) ⇒ true` | |
implicit def toJavaBiPredicate[A, B](predicate: (A, B) => Boolean): BiPredicate[A, B] = | |
new BiPredicate[A, B] { | |
def test(a: A, b: B) = predicate(a, b) | |
} | |
implicit def iteratorFromJavaStream[T](s: JStream[T]): Iterator[T] = | |
s.iterator().asScala | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment