
sbt.testing.OptionalThrowable.scala Maven / Gradle / Ivy
/*
* Scala.js (https://www.scala-js.org/)
*
* Copyright EPFL.
*
* Licensed under Apache License 2.0
* (https://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/
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