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

com.mle.util.package.scala Maven / Gradle / Ivy

The newest version!
package com.mle

import scala.util.control.NonFatal
import scala.util.{Failure, Success, Try}

/**
 * @author Michael
 */
package object util {

  implicit class TryOps[T](orig: Try[T]) {
    def recoverNonFatal[U >: T](fix: Throwable => U): Try[U] = orig.recover {
      case NonFatal(t) => fix(t)
    }

    def recoverAll[U >: T](fix: Throwable => U): Try[U] = orig.recover {
      case t: Throwable => fix(t)
    }

    def recoverWithAll[U >: T](fix: Throwable => Try[U]): Try[U] = orig.recoverWith {
      case t: Throwable => fix(t)
    }

    def fold[U](ifFailure: Throwable => U)(ifSuccess: T => U): U = orig match {
      case Success(s) => ifSuccess(s)
      case Failure(t) => ifFailure(t)
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy