Last active
December 23, 2015 14:29
-
-
Save thanh-buiviet/6649489 to your computer and use it in GitHub Desktop.
implicit converter from spy-memcached (>= 2.10.0) OperationFuture to scala.concurrent.Future
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 sd.cb | |
import net.spy.memcached.internal._ | |
import scala.concurrent.{Future, Promise} | |
import com.couchbase.client.internal.{HttpCompletionListener, HttpFuture} | |
import collection.JavaConverters._ | |
/** | |
* implicit convert [T extends net.spy.memcached.internal.ListenableFuture] to scala Future | |
* @see http://stackoverflow.com/questions/11529145/how-do-i-wrap-a-java-util-concurrent-future-in-an-akka-future?rq=1 | |
* http://stackoverflow.com/questions/17215421/scala-concurrent-future-wrapper-for-java-util-concurrent-future | |
* FIXME http://www.couchbase.com/issues/browse/JCBC-343#comment-68524 | |
*/ | |
object CbFutureAsScala{ | |
import scala.language.implicitConversions | |
implicit def operationFutureAsScala[T](underlying: OperationFuture[T]): Future[T] = { | |
val p = Promise[T]() | |
underlying.addListener(new OperationCompletionListener{ | |
def onComplete(f: OperationFuture[_]) { | |
val status = f.getStatus //f is underlying | |
if(status.isSuccess) | |
p success underlying.get | |
else | |
p failure new Exception(status.getMessage) | |
} | |
}) | |
p.future | |
} | |
implicit def getFutureAsScala[T](underlying: GetFuture[T]): Future[T] = { | |
val p = Promise[T]() | |
underlying.addListener(new GetCompletionListener{ | |
def onComplete(f: GetFuture[_]) { | |
val status = f.getStatus //f is underlying | |
if(status.isSuccess) | |
p success underlying.get | |
else | |
p failure new Exception(status.getMessage) | |
} | |
}) | |
p.future | |
} | |
/** | |
* @note we don't need implicit converter from ViewFuture. Use HttpFuture[ViewResponse] instead | |
*/ | |
implicit def httpFutureAsScala[T](underlying: HttpFuture[T]): Future[T] = { | |
val p = Promise[T]() | |
underlying.addListener(new HttpCompletionListener{ | |
def onComplete(f: HttpFuture[_]) { | |
val status = f.getStatus //f is underlying | |
if(status.isSuccess) | |
p success underlying.get | |
else | |
p failure new Exception(status.getMessage) | |
} | |
}) | |
p.future | |
} | |
implicit def bulkGetFutureAsScala[T](underlying: BulkGetFuture[T]): Future[Map[String, T]] = { | |
val p = Promise[Map[String, T]]() | |
underlying.addListener(new BulkGetCompletionListener{ | |
def onComplete(f: BulkGetFuture[_]) { | |
val status = f.getStatus //f is underlying | |
if(status.isSuccess) | |
p success underlying.get.asScala.toMap //java.util.Map -> mutable.Map -> immutable.Map | |
else | |
p failure new Exception(status.getMessage) | |
} | |
}) | |
p.future | |
} | |
// implicit def replicaGetFutureAsScala[T](underlying: ReplicaGetFuture[T]): Future[T] = ??? //we don't need now | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment