= Try
:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

The xref:refguide:commons:index/functional/Try.adoc[Try] type represents a value of one of two possible types (a disjoint union) of _Success_ or _Failure_ .

Factory methods _Try#success(Object)_ and _Try#failure(Throwable)_ correspond to the two possible values.

Follows the *Railway Pattern* , that is, once failed, stays failed.

== API

[source,java]
.Try.java
----
interface Try<T> {
  Try<T> call(Callable<T> callable)
  Try<Void> run(ThrowingRunnable runnable)
  Success<T> success(T value)
  Failure<T> failure(Throwable throwable)
  boolean isSuccess()
  boolean isFailure()
  Optional<T> getValue()     // <.>
  Optional<Throwable> getFailure()     // <.>
  Try<T> ifSuccess(Consumer<Optional<T>> valueConsumer)     // <.>
  Try<T> ifFailure(Consumer<Throwable> exceptionConsumer)     // <.>
  Try<T> ifFailureFail()     // <.>
  Try<T> ifAbsentFail()     // <.>
  Try<R> mapSuccess(Function<T, R> successMapper)     // <.>
  Try<T> mapFailure(UnaryOperator<Throwable> failureMapper)     // <.>
  Try<T> mapEmptyToFailure()     // <.>
  Either<L, R> map(Function<Throwable, L> failureMapper, Function<Optional<T>, R> successMapper)     // <.>
  void accept(Consumer<Throwable> failureConsumer, Consumer<Optional<T>> successConsumer)     // <.>
  R fold(Function<Throwable, R> failureMapper, Function<Optional<T>, R> successMapper)     // <.>
  Try<R> thenCall(Callable<R> callable)     // <.>
  Try<Void> thenRun(ThrowingRunnable runnable)     // <.>
}
----

<.> xref:#getValue_[getValue()]
+
--
Optionally returns the contained _value_ based on presence, that is, if this is a _Success_ and the value is not _null_ .
--
<.> xref:#getFailure_[getFailure()]
+
--
Optionally returns the contained _failure_ based on presence, that is, if this is a _Failure_ .
--
<.> xref:#ifSuccess_Consumer[ifSuccess(Consumer)]
+
--
Peeks into the _value_ if this is a _Success_ .
--
<.> xref:#ifFailure_Consumer[ifFailure(Consumer)]
+
--
Peeks into the _failure_ if this is a _Failure_ .
--
<.> xref:#ifFailureFail_[ifFailureFail()]
+
--
Throws the contained failure if any.
--
<.> xref:#ifAbsentFail_[ifAbsentFail()]
+
--
Throws _NoSuchElementException_ if _value_ is _null_ .
--
<.> xref:#mapSuccess_Function[mapSuccess(Function)]
+
--
Maps this xref:refguide:commons:index/functional/Try.adoc[Try] to another if this is a _Success_ . Otherwise if this is a _Failure_ acts as identity operator.
--
<.> xref:#mapFailure_UnaryOperator[mapFailure(UnaryOperator)]
+
--
Maps this xref:refguide:commons:index/functional/Try.adoc[Try] to another if its a _Failure_ . Otherwise if this is a _Success_ acts as identity operator.
--
<.> xref:#mapEmptyToFailure_[mapEmptyToFailure()]
+
--
Maps this xref:refguide:commons:index/functional/Try.adoc[Try] to _Failure_ if this is a _Success_ with an empty _value_ . Otherwise acts as identity operator.
--
<.> xref:#map_Function_Function[map(Function, Function)]
+
--
Maps this xref:refguide:commons:index/functional/Try.adoc[Try] to xref:refguide:commons:index/functional/Either.adoc[Either] using according mapping function _successMapper_ or _failureMapper_ .
--
<.> xref:#accept_Consumer_Consumer[accept(Consumer, Consumer)]
+
--
Either consumes the success or the failure.
--
<.> xref:#fold_Function_Function[fold(Function, Function)]
+
--
Maps the contained _value_ or _failure_ to a new value of type _R_ using according mapping function _successMapper_ or _failureMapper_ .
--
<.> xref:#thenCall_Callable[thenCall(Callable)]
+
--
If this is a _Success_ , maps it to a new xref:refguide:commons:index/functional/Try.adoc[Try] based on given _Callable_ . Otherwise if its a _Failure_ acts as identity operator.
--
<.> xref:#thenRun_ThrowingRunnable[thenRun(ThrowingRunnable)]
+
--
If this is a _Success_ , maps it to new xref:refguide:commons:index/functional/Try.adoc[Try] based on given _ThrowingRunnable_ . Otherwise if its a _Failure_ acts as identity operator.
--

== Members

[#getValue_]
=== getValue()

Optionally returns the contained _value_ based on presence, that is, if this is a _Success_ and the value is not _null_ .

[#getFailure_]
=== getFailure()

Optionally returns the contained _failure_ based on presence, that is, if this is a _Failure_ .

[#ifSuccess_Consumer]
=== ifSuccess(Consumer)

Peeks into the _value_ if this is a _Success_ .

[#ifFailure_Consumer]
=== ifFailure(Consumer)

Peeks into the _failure_ if this is a _Failure_ .

[#ifFailureFail_]
=== ifFailureFail()

Throws the contained failure if any.

[#ifAbsentFail_]
=== ifAbsentFail()

Throws _NoSuchElementException_ if _value_ is _null_ .

[#mapSuccess_Function]
=== mapSuccess(Function)

Maps this xref:refguide:commons:index/functional/Try.adoc[Try] to another if this is a _Success_ . Otherwise if this is a _Failure_ acts as identity operator.

[#mapFailure_UnaryOperator]
=== mapFailure(UnaryOperator)

Maps this xref:refguide:commons:index/functional/Try.adoc[Try] to another if its a _Failure_ . Otherwise if this is a _Success_ acts as identity operator.

[#mapEmptyToFailure_]
=== mapEmptyToFailure()

Maps this xref:refguide:commons:index/functional/Try.adoc[Try] to _Failure_ if this is a _Success_ with an empty _value_ . Otherwise acts as identity operator.

[#map_Function_Function]
=== map(Function, Function)

Maps this xref:refguide:commons:index/functional/Try.adoc[Try] to xref:refguide:commons:index/functional/Either.adoc[Either] using according mapping function _successMapper_ or _failureMapper_ .

[#accept_Consumer_Consumer]
=== accept(Consumer, Consumer)

Either consumes the success or the failure.

[#fold_Function_Function]
=== fold(Function, Function)

Maps the contained _value_ or _failure_ to a new value of type _R_ using according mapping function _successMapper_ or _failureMapper_ .

[#thenCall_Callable]
=== thenCall(Callable)

If this is a _Success_ , maps it to a new xref:refguide:commons:index/functional/Try.adoc[Try] based on given _Callable_ . Otherwise if its a _Failure_ acts as identity operator.

[#thenRun_ThrowingRunnable]
=== thenRun(ThrowingRunnable)

If this is a _Success_ , maps it to new xref:refguide:commons:index/functional/Try.adoc[Try] based on given _ThrowingRunnable_ . Otherwise if its a _Failure_ acts as identity operator.
