io.vlingo.common.Success 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.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
public class Success implements Outcome {
private final ValueT value;
private Success(final ValueT value) {
this.value = value;
}
public static Outcome of(final ValueT value) {
return new Success<>(value);
}
@Override
public Outcome andThen(final Function action) {
return Success.of(action.apply(value));
}
@Override
public Outcome andThenTo(final Function> action) {
return action.apply(value);
}
@Override
public void atLeastConsume(final Consumer consumer) {
consumer.accept(value);
}
@Override
public Outcome otherwise(final Function action) {
return this;
}
@Override
@SuppressWarnings("unchecked")
public Outcome otherwiseTo(final Function> action) {
return (Outcome) this;
}
@Override
public ValueT get() throws CauseT {
return value;
}
@Override
public ValueT getOrNull() {
return value;
}
@Override
public NextSuccessT resolve(final Function onFailedOutcome, final Function onSuccessfulOutcome) {
return onSuccessfulOutcome.apply(value);
}
@Override
public Optional asOptional() {
return Optional.of(value);
}
@Override
public Completes asCompletes() {
return Completes.withSuccess(value);
}
@Override
public Outcome filter(Function filterFunction) {
if (filterFunction.apply(value)) {
return Success.of(value);
}
return Failure.of(new NoSuchElementException(Objects.toString(value, "null")));
}
@Override
public Outcome> alongWith(Outcome, SecondSuccessT> outcome) {
return outcome.andThenTo(secondOutcome -> Success.of(Tuple2.from(value, secondOutcome)));
}
@Override
@SuppressWarnings("unchecked")
public Outcome otherwiseFail(Function action) {
return (Outcome) this;
}
@Override
@SuppressWarnings("unchecked")
public boolean equals(final Object other) {
if (other == null || !other.getClass().equals(getClass())) {
return false;
}
return this.value.equals(((Success) other).value);
}
@Override
public int hashCode() {
return 31 * value.hashCode();
}
}