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

io.vlingo.common.Failure Maven / Gradle / Ivy

There is a newer version: 1.7.5
Show newest version
// 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;
    }

    @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 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();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy