<commit_before>package org.openmole.plugin.environment.batch.environment

import org.openmole.core.exception._
import scala.concurrent.stm._

case class AccessToken() {
  def access[T](op: ⇒ T): T = synchronized(op)
}

object UsageControl {

  def faucetToken = AccessToken()

  def mapToken[B](usageControl: UsageControl)(f: AccessToken ⇒ B) = {
    val t = tryGetToken(usageControl)
    try t.map(f)
    finally t.foreach(releaseToken(usageControl, _))
  }

  def tryWithToken[B](usageControl: UsageControl)(f: Option[AccessToken] ⇒ B) = {
    val t = tryGetToken(usageControl)
    try f(t)
    finally t.foreach(releaseToken(usageControl, _))
  }

  def withToken[B](usageControl: UsageControl)(f: AccessToken ⇒ B) = {
    val t = getToken(usageControl)
    try f(t)
    finally releaseToken(usageControl, t)
  }

  def releaseToken(usageControl: UsageControl, token: AccessToken) = atomic { implicit txn ⇒
    usageControl.usedToken -= token
    usageControl.tokens() = token :: usageControl.tokens()
  }

  def tryGetToken(usageControl: UsageControl): Option[AccessToken] = atomic { implicit txn ⇒
    if (usageControl.stopped()) None
    else
      usageControl.tokens() match {
        case h :: t ⇒
          usageControl.tokens() = t
          usageControl.usedToken += h
          Some(h)
        case Nil ⇒ None
      }
  }

  private def getToken(usageControl: UsageControl) = atomic { implicit txn ⇒
    if (usageControl.stopped()) throw new InternalProcessingError("Service has been stopped")
    tryGetToken(usageControl) match {
      case Some(t) ⇒ t
      case None    ⇒ retry
    }
  }

  def freeze(usageControl: UsageControl) = {
    usageControl.stopped.single() = true<commit_msg>[Plugin] enh: add private modifier<commit_after>package org.openmole.plugin.environment.batch.environment

import org.openmole.core.exception._
import scala.concurrent.stm._

case class AccessToken() {
  def access[T](op: ⇒ T): T = synchronized(op)
}

object UsageControl {

  def faucetToken = AccessToken()

  def mapToken[B](usageControl: UsageControl)(f: AccessToken ⇒ B) = {
    val t = tryGetToken(usageControl)
    try t.map(f)
    finally t.foreach(releaseToken(usageControl, _))
  }

  def tryWithToken[B](usageControl: UsageControl)(f: Option[AccessToken] ⇒ B) = {
    val t = tryGetToken(usageControl)
    try f(t)
    finally t.foreach(releaseToken(usageControl, _))
  }

  def withToken[B](usageControl: UsageControl)(f: AccessToken ⇒ B) = {
    val t = getToken(usageControl)
    try f(t)
    finally releaseToken(usageControl, t)
  }

  private def releaseToken(usageControl: UsageControl, token: AccessToken) = atomic { implicit txn ⇒
    usageControl.usedToken -= token
    usageControl.tokens() = token :: usageControl.tokens()
  }

  private def tryGetToken(usageControl: UsageControl): Option[AccessToken] = atomic { implicit txn ⇒
    if (usageControl.stopped()) None
    else
      usageControl.tokens() match {
        case h :: t ⇒
          usageControl.tokens() = t
          usageControl.usedToken += h
          Some(h)
        case Nil ⇒ None
      }
  }

  private def getToken(usageControl: UsageControl) = atomic { implicit txn ⇒
    if (usageControl.stopped()) throw new InternalProcessingError("Service has been stopped")
    tryGetToken(usageControl) match {
      case Some(t) ⇒ t
      case None    ⇒ retry
    }
  }

  def freeze(usageControl: UsageControl) = {
    usageControl.stopped.single() = true
{'fraction_non_alphanumeric': 0.11124019750217834, 'fraction_numerical': 0.0, 'mean_word_length': 3.490221642764016, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2283741', 'n_tokens_mistral': 1089, 'n_tokens_neox': 1082, 'n_words': 269}