<commit_before>package com.twitter.util

import java.util.concurrent.atomic.AtomicInteger

import com.twitter.conversions.time._
import scala.collection.mutable.ArrayBuffer

object Future {
  val DEFAULT_TIMEOUT = Duration.MaxValue
  val Unit = apply(())
  val Done = Unit

  def value[A](a: A) = Future(a)
  def exception[A](e: Throwable) = Future[A] { throw e }

  def void() = Future[Void] { null }

  /**
   * A factory function to "lift" computations into the Future monad. It will catch
   * exceptions and wrap them in the Throw[_] type. Non-exceptional values are wrapped
   * in the Return[_] type.
   */
  def apply[A](a: => A): Future[A] =
    new Promise[A] {
      update(Try(a))
    }

  def join(fs: Seq[Future[_]]): Future[Unit] = {
    if (fs.isEmpty)
      return value(())

    val count = new AtomicInteger(fs.size)
    val promise = new Promise[Unit]

    fs foreach { f =>
      f respond {
        case Throw(cause) =>
          promise.updateIfEmpty(Throw(cause))

        case Return(_) =>
          if (count.decrementAndGet() == 0)
            promise() = Return(())
      }
    }

    promise
  }
}

/**
 * An alternative interface for handling Future Events. This interface is designed
 * to be friendly to Java users since it does not require closures.
 */
trait FutureEventListener[T] {
  /**
   * Invoked if the computation completes successfully
   */
  def onSuccess(value: T): Unit

  /**
   * Invoked if the computation completes unsuccessfully
   */
  def onFailure(cause: Throwable): Unit
}

/**
 * A computation evaluated asynchronously. This implementation of Future does not
 * assume any concrete implementation. In particular, it does not couple the user
 * to a specific executor or event loop.
 *
 * Note that this class extends Try[_] indicating that the results of the computation
 * may succeed or fail.
 */<commit_msg>semicolon.<commit_after>package com.twitter.util

import java.util.concurrent.atomic.AtomicInteger

import com.twitter.conversions.time._
import scala.collection.mutable.ArrayBuffer

object Future {
  val DEFAULT_TIMEOUT = Duration.MaxValue
  val Unit = apply(())
  val Done = Unit

  def value[A](a: A) = Future(a)
  def exception[A](e: Throwable) = Future[A] { throw e }

  def void() = Future[Void] { null }

  /**
   * A factory function to "lift" computations into the Future monad. It will catch
   * exceptions and wrap them in the Throw[_] type. Non-exceptional values are wrapped
   * in the Return[_] type.
   */
  def apply[A](a: => A): Future[A] =
    new Promise[A] {
      update(Try(a))
    }

  def join(fs: Seq[Future[_]]): Future[Unit] = {
    if (fs.isEmpty)
      return value(())

    val count = new AtomicInteger(fs.size)
    val promise = new Promise[Unit]

    fs foreach { f =>
      f respond {
        case Throw(cause) =>
          promise.updateIfEmpty(Throw(cause))

        case Return(_) =>
          if (count.decrementAndGet() == 0)
            promise() = Return(())
      }
    }

    promise
  }
}

/**
 * An alternative interface for handling Future Events. This interface is designed
 * to be friendly to Java users since it does not require closures.
 */
trait FutureEventListener[T] {
  /**
   * Invoked if the computation completes successfully
   */
  def onSuccess(value: T): Unit

  /**
   * Invoked if the computation completes unsuccessfully
   */
  def onFailure(cause: Throwable): Unit
}

/**
 * A computation evaluated asynchronously. This implementation of Future does not
 * assume any concrete implementation; in particular, it does not couple the user
 * to a specific executor or event loop.
 *
 * Note that this class extends Try[_] indicating that the results of the computation
 * may succeed or fail.
 */
{'fraction_non_alphanumeric': 0.10694183864915573, 'fraction_numerical': 0.0005360493165371214, 'mean_word_length': 3.017222820236814, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4814059', 'n_tokens_mistral': 1105, 'n_tokens_neox': 1072, 'n_words': 384}