All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.undefinedlabs.scope.javapi.JavaPartialFunction.scala Maven / Gradle / Ivy

package com.undefinedlabs.scope.javapi

import scala.runtime.AbstractPartialFunction
import scala.util.control.NoStackTrace

object JavaPartialFunction {

  sealed abstract class NoMatchException extends RuntimeException with NoStackTrace

  case object NoMatch extends NoMatchException

  final def noMatch(): RuntimeException = NoMatch
}

abstract class JavaPartialFunction[A, B] extends AbstractPartialFunction[A, B] {

  import JavaPartialFunction._

  @throws(classOf[Exception])
  def apply(x: A, isCheck: Boolean): B

  final def isDefinedAt(x: A): Boolean =
    try {
      apply(x, true);
      true
    } catch {
      case NoMatch => false
    }

  final override def apply(x: A): B =
    try apply(x, false)
    catch {
      case NoMatch => throw new MatchError(x)
    }

  final override def applyOrElse[A1 <: A, B1 >: B](x: A1, default: A1 => B1): B1 =
    try apply(x, false)
    catch {
      case NoMatch => default(x)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy