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

io.github.olib963.javatest.fixtures.internal.Failure Maven / Gradle / Ivy

package io.github.olib963.javatest.fixtures.internal;

import io.github.olib963.javatest.fixtures.Try;

import java.util.Objects;
import java.util.function.Function;

public class Failure implements Try {
    private final Exception error;

    public Failure(Exception error) {
        this.error = error;
    }

    @Override
    public Try mapError(Function f) {
        return new Failure<>(f.apply(error));
    }

    @Override
    @SuppressWarnings("unchecked") // Failure can be a Try of any type safely
    public  Try map(Function f) {
        return (Try) this;
    }


    @Override
    @SuppressWarnings("unchecked") // Failure can be a Try of any type safely
    public  Try flatMap(Function> f) {
        return (Try) this;
    }

    @Override
    public A recoverWith(Function f) {
        return f.apply(error);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Failure failure = (Failure) o;
        return Objects.equals(error, failure.error);
    }

    @Override
    public int hashCode() {
        return Objects.hash(Failure.class, error);
    }

    @Override
    public String toString() {
        return "Failure(" + error + ')';
    }
}