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

sbt.testing.OptionalThrowable.scala Maven / Gradle / Ivy

There is a newer version: 1.0.0-M2
Show newest version
package sbt.testing

/** An optional Throwable. */
final class OptionalThrowable(
    private val exception: Throwable) extends Serializable {

  def this() = this(null)

  /** Indicates whether this OptionalThrowable is "defined,"
   *  i.e., contains a Throwable.
   *
   * @return true if this OptionalThrowable contains a
   *         Throwable
   */
  def isDefined(): Boolean = exception != null

  /** Indicates whether this OptionalThrowable is "empty,"
   *  i.e., contains no Throwable.
   *
   *  @return true if this OptionalThrowable contains no
   *          Throwable
   */
  def isEmpty(): Boolean = exception == null

  /** Returns the Throwable contained in this
   *  OptionalThrowable if defined, else throws
   *  IllegalStateException.
   *
   *  To avoid the IllegalStateException, ensure
   *  isDefined returns true before calling this
   *  method.
   *
   * @return the contained Throwable, if this
   *         OptionalThrowable is defined
   * @throws java.lang.IllegalStateException if this
   *     OptionalThrowable is not defined.
   */
  def get(): Throwable = {
    if (exception == null)
      throw new IllegalStateException("This OptionalThrowable is not defined")
    else
      exception
  }

  override def equals(that: Any): Boolean = that match {
    case that: OptionalThrowable =>
      this.exception eq that.exception
    case _ => false
  }

  override def hashCode(): Int =
    if (exception == null) 0 else exception.hashCode()

  override def toString(): String = {
    if (exception == null)
      "OptionalThrowable()"
    else
      s"OptionalThrowable($exception)"
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy