当前位置: 首页>>代码示例>>Scala>>正文


Scala ListenableFuture类代码示例

本文整理汇总了Scala中com.google.common.util.concurrent.ListenableFuture的典型用法代码示例。如果您正苦于以下问题:Scala ListenableFuture类的具体用法?Scala ListenableFuture怎么用?Scala ListenableFuture使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了ListenableFuture类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: GuavaFutures

//设置package包名称以及导入依赖的类
package akka.stream.alpakka.cassandra

import com.google.common.util.concurrent.{FutureCallback, Futures, ListenableFuture}

import scala.concurrent.{Future, Promise}

private[cassandra] object GuavaFutures {
  implicit final class GuavaFutureOpts[A](val guavaFut: ListenableFuture[A]) extends AnyVal {
    def asScala(): Future[A] = {
      val p = Promise[A]()
      val callback = new FutureCallback[A] {
        override def onSuccess(a: A): Unit = p.success(a)
        override def onFailure(err: Throwable): Unit = p.failure(err)
      }
      Futures.addCallback(guavaFut, callback)
      p.future
    }
  }
} 
开发者ID:akka,项目名称:alpakka,代码行数:20,代码来源:GuavaFutures.scala

示例2: toAsyncF

//设置package包名称以及导入依赖的类
package spinoco.fs2
import com.google.common.util.concurrent.{FutureCallback, Futures, ListenableFuture}
import fs2.util.Async


package object cassandra {

  implicit def toAsyncF[F[_],A](f:ListenableFuture[A])(implicit F:Async[F]):F[A] = {
    F.async { cb =>
      F.pure(Futures.addCallback(f, new FutureCallback[A] {
        def onFailure(t: Throwable): Unit = { cb(Left(t))}
        def onSuccess(result: A): Unit = { cb(Right(result)) }
      }))
    }
  }


} 
开发者ID:Spinoco,项目名称:fs2-cassandra,代码行数:19,代码来源:cassandra.scala

示例3: point

//设置package包名称以及导入依赖的类
package deaktator.advtut.futures.instances

import java.util.concurrent.Callable

import com.google.common.util.concurrent.{AsyncFunction, Futures, ListeningExecutorService, ListenableFuture}
import deaktator.advtut.futures.cats.Monad
import com.google.common.base.{Function => GFunction}
import deaktator.advtut.futures.instances.ListenableFutureInstances._


  override def point[A](asyncExecutedValue: => A): ListenableFuture[A] = {
    exSvc.submit(new Callable[A]{
      override def call(): A = asyncExecutedValue
    })
  }

  override def flatMap[A, B](ma: ListenableFuture[A])(f: A => ListenableFuture[B]): ListenableFuture[B] =
    Futures.transform(ma, GAsyncFn(f))

  override def map[A, B](fa: ListenableFuture[A])(f: A => B): ListenableFuture[B] =
    Futures.transform(fa, GFn(f))            // flatMap(fa)(a => point(f(a)))

  override def ap[A, B](x: ListenableFuture[A])(f: ListenableFuture[A => B]): ListenableFuture[B] =
    flatMap(f)(g => map(x)(y => g(y)))       // map2(x, f)((y, g) => g(y))

  override def map2[A, B, C](x: ListenableFuture[A], y: ListenableFuture[B])(f: (A, B) => C): ListenableFuture[C] =
    flatMap(y)(b => map(x)(a => f(a, b)))    // ap(y)(map(x)(f.curried))
}

object ListenableFutureInstances {
  object Implicits {
    implicit def listenableFutureInstances(implicit exSvc: ListeningExecutorService): ListenableFutureInstances =
      ListenableFutureInstances(exSvc)
  }

  case class GFn[A, B](f: A => B) extends GFunction[A, B] {
    def apply(a: A): B = f(a)
  }

  case class GAsyncFn[A, B](f: A => ListenableFuture[B]) extends AsyncFunction[A, B] {
    def apply(a: A): ListenableFuture[B] = f(a)
  }
} 
开发者ID:deaktator,项目名称:advanced-scala-tutorial,代码行数:44,代码来源:ListenableFutureInstances.scala

示例4: RichListenableFuture

//设置package包名称以及导入依赖的类
package recipestore.misc

import com.google.common.util.concurrent.{FutureCallback, Futures, ListenableFuture}

import scala.concurrent.{Future, Promise}

object RichListenableFuture {

  implicit class RichListenableFuture[T](lf: ListenableFuture[T]) {
    def asScala: Future[T] = {
      val p = Promise[T]()
      Futures.addCallback(lf, new FutureCallback[T] {
        def onFailure(t: Throwable): Unit = p failure t

        def onSuccess(result: T): Unit = p success result
      })
      p.future
    }
  }

} 
开发者ID:prad-a-RuntimeException,项目名称:semantic-store,代码行数:22,代码来源:RichListenableFuture.scala


注:本文中的com.google.common.util.concurrent.ListenableFuture类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。