<commit_before>package jp.t2v.lab.play2.stackc

import play.api.mvc._
import scala.collection.concurrent.TrieMap
import scala.concurrent.ExecutionContext
import scala.util.{Try, Failure, Success}

trait StackableController {
    self: Controller =>

  implicit def executionContext: ExecutionContext = ExecutionContext.Implicits.global

  final def StackAction[A](p: BodyParser[A], params: (RequestAttributeKey[_], Any)*)(f: RequestWithAttributes[A] => Result): Action[A] = Action(p) { req =>
    val request = new RequestWithAttributes(req, new TrieMap[RequestAttributeKey[_], Any] ++= params)
    try {
      cleanup(request, proceed(request)(f))
    } catch {
      case e: Exception => cleanupOnFailed(request, e); throw e
    }
  }

  def StackAction(params: (RequestAttributeKey[_], Any)*)(f: RequestWithAttributes[AnyContent] => Result): Action[AnyContent] = StackAction(parse.anyContent, params: _*)(f)

  def StackAction(f: RequestWithAttributes[AnyContent] => Result): Action[AnyContent] = StackAction()(f)

  def proceed[A](request: RequestWithAttributes[A])(f: RequestWithAttributes[A] => Result): Result = f(request)

  def cleanupOnSucceeded[A](request: RequestWithAttributes[A]): Unit = ()

  def cleanupOnFailed[A](request: RequestWithAttributes[A], e: Exception): Unit = ()

  private def cleanup[A](request: RequestWithAttributes[A], result: Result): Result = result match {
    case p: PlainResult => {cleanupOnSucceeded(request); p}
    case AsyncResult(f) => AsyncResult {
      f andThen {
        case Success(r)            => cleanup(request, r)
        case Failure(e: Exception) => cleanupOnFailed(request, e)
      }
    }
  }

}

trait RequestAttributeKey[A]

class RequestWithAttributes[A](underlying: Request[A], attributes: TrieMap[RequestAttributeKey[_], Any]) extends WrappedRequest[A](underlying) {

  def get[B](key: RequestAttributeKey[B]): Option[B] = {
    attributes.get(key).flatMap { item =>
      Try(item.asInstanceOf[B]).toOption
    }
  }

  /** side effect! */
  def set[B](key: RequestAttributeKey[B], value: B): RequestWithAttributes[A] = {
    attributes.put(key, value)
    this
  }

}<commit_msg>Removed Try object creation and handling only CCE<commit_after>package jp.t2v.lab.play2.stackc

import play.api.mvc._
import scala.collection.concurrent.TrieMap
import scala.concurrent.ExecutionContext
import scala.util.{Failure, Success}

trait StackableController {
    self: Controller =>

  implicit def executionContext: ExecutionContext = ExecutionContext.Implicits.global

  final def StackAction[A](p: BodyParser[A], params: (RequestAttributeKey[_], Any)*)(f: RequestWithAttributes[A] => Result): Action[A] = Action(p) { req =>
    val request = new RequestWithAttributes(req, new TrieMap[RequestAttributeKey[_], Any] ++= params)
    try {
      cleanup(request, proceed(request)(f))
    } catch {
      case e: Exception => cleanupOnFailed(request, e); throw e
    }
  }

  def StackAction(params: (RequestAttributeKey[_], Any)*)(f: RequestWithAttributes[AnyContent] => Result): Action[AnyContent] = StackAction(parse.anyContent, params: _*)(f)

  def StackAction(f: RequestWithAttributes[AnyContent] => Result): Action[AnyContent] = StackAction()(f)

  def proceed[A](request: RequestWithAttributes[A])(f: RequestWithAttributes[A] => Result): Result = f(request)

  def cleanupOnSucceeded[A](request: RequestWithAttributes[A]): Unit = ()

  def cleanupOnFailed[A](request: RequestWithAttributes[A], e: Exception): Unit = ()

  private def cleanup[A](request: RequestWithAttributes[A], result: Result): Result = result match {
    case p: PlainResult => {cleanupOnSucceeded(request); p}
    case AsyncResult(f) => AsyncResult {
      f andThen {
        case Success(r)            => cleanup(request, r)
        case Failure(e: Exception) => cleanupOnFailed(request, e)
      }
    }
  }

}

trait RequestAttributeKey[A]

class RequestWithAttributes[A](underlying: Request[A], attributes: TrieMap[RequestAttributeKey[_], Any]) extends WrappedRequest[A](underlying) {

  def get[B](key: RequestAttributeKey[B]): Option[B] =
    attributes.get(key).flatMap { item =>
      try Some(item.asInstanceOf[B]) catch {
        case _: ClassCastException => None
      }
    }

  /** side effect! */
  def set[B](key: RequestAttributeKey[B], value: B): RequestWithAttributes[A] = {
    attributes.put(key, value)
    this
  }

}
{'fraction_non_alphanumeric': 0.13511659807956103, 'fraction_numerical': 0.0009144947416552355, 'mean_word_length': 4.7947019867549665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 28, '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': '6273101', 'n_tokens_mistral': 1343, 'n_tokens_neox': 1312, 'n_words': 293}