io.vlingo.common.Failure Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vlingo-common Show documentation
Show all versions of vlingo-common Show documentation
These are just a few common tools shared across various vlingo projects.
// Copyright © 2012-2020 VLINGO LABS. All rights reserved.
//
// This Source Code Form is subject to the terms of the
// Mozilla Public License, v. 2.0. If a copy of the MPL
// was not distributed with this file, You can obtain
// one at https://mozilla.org/MPL/2.0/.
package io.vlingo.common;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
public class Failure implements Outcome {
private final CauseT cause;
private Failure(final CauseT cause) {
this.cause = cause;
}
public static Outcome of(final CauseT cause) {
return new Failure<>(cause);
}
@Override
@SuppressWarnings("unchecked")
public Outcome andThen(final Function action) {
return (Outcome) this;
}
@Override
@SuppressWarnings("unchecked")
public Outcome andThenTo(final Function> action) {
return (Outcome) this;
}
@Override
public void atLeastConsume(final Consumer consumer) {
}
@Override
public Outcome otherwise(final Function action) {
return Success.of(action.apply(cause));
}
@Override
public Outcome otherwiseTo(final Function> action) {
return action.apply(cause);
}
@Override
public ValueT get() throws CauseT {
throw cause;
}
@Override
public ValueT getOrNull() {
return null;
}
public CauseT cause() {
return cause;
}
@Override
public NextSuccessT resolve(final Function onFailedOutcome, final Function onSuccessfulOutcome) {
return onFailedOutcome.apply(cause);
}
@Override
public Optional asOptional() {
return Optional.empty();
}
@Override
public Completes asCompletes() {
return Completes.withFailure();
}
@Override
public Outcome filter(Function filterFunction) {
return Failure.of((NoSuchElementException) new NoSuchElementException().initCause(cause));
}
@Override
@SuppressWarnings("unchecked")
public Outcome> alongWith(Outcome, SecondSuccessT> outcome) {
return (Outcome>) this;
}
@Override
public Outcome otherwiseFail(Function action) {
return Failure.of(action.apply(cause));
}
@Override
@SuppressWarnings("unchecked")
public boolean equals(final Object other) {
if (other == null || !other.getClass().equals(getClass())) {
return false;
}
return this.cause.equals(((Failure) other).cause);
}
@Override
public int hashCode() {
return 31 * cause.hashCode();
}
}