org.reactfx.util.Try Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of richtextfx Show documentation
Show all versions of richtextfx Show documentation
FX-Text-Area for formatted text and other special effects.
package org.reactfx.util;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
public interface Try extends Either {
static Try success(T value) {
return new Success<>(value);
}
static Try failure(Throwable thrown) {
return new Failure<>(thrown);
}
static Try tryGet(Callable extends T> f) {
try {
return success(f.call());
} catch(Throwable t) {
return failure(t);
}
}
default boolean isSuccess() { return isRight(); }
default boolean isFailure() { return isLeft(); }
default T get() { return getRight(); }
default Throwable getFailure() { return getLeft(); }
default Optional toOptional() { return asRight(); }
default void ifSuccess(Consumer super T> f) { ifRight(f); }
default void ifFailure(Consumer super Throwable> f) { ifLeft(f); }
T getOrElse(T fallback);
T getOrElse(Supplier fallback);
Try orElse(Try fallback);
Try orElse(Supplier> fallback);
Try orElseTry(Callable extends T> fallback);
Try recover(Function> f);
Try map(Function super T, ? extends U> f);
Try flatMap(Function super T, Try> f);
}
class Success extends Right implements Try {
public Success(T value) { super(value); }
@Override
public T getOrElse(T fallback) { return getRight(); }
@Override
public T getOrElse(Supplier fallback) { return getRight(); }
@Override
public Try orElse(Try fallback) { return this; }
@Override
public Try orElse(Supplier> fallback) { return this; }
@Override
public Try orElseTry(Callable extends T> fallback) { return this; }
@Override
public Try recover(Function> f) { return this; }
@Override
public Try map(Function super T, ? extends U> f) {
return new Success<>(f.apply(get()));
}
@Override
public Try flatMap(Function super T, Try> f) {
return f.apply(get());
}
@Override
public String toString() {
return "success(" + get() + ")";
}
}
class Failure extends Left implements Try {
public Failure(Throwable thrown) { super(thrown); }
@Override
public T getOrElse(T fallback) { return fallback; }
@Override
public T getOrElse(Supplier fallback) { return fallback.get(); }
@Override
public Try orElse(Try fallback) { return fallback; }
@Override
public Try orElse(Supplier> fallback) { return fallback.get(); }
@Override
public Try orElseTry(Callable extends T> fallback) { return Try.tryGet(fallback); }
@Override
public Try recover(Function> f) {
Optional recovered = f.apply(getFailure());
if(recovered.isPresent()) {
return new Success<>(recovered.get());
} else {
return this;
}
}
@Override
public Try map(Function super T, ? extends U> f) {
return new Failure<>(getFailure());
}
@Override
public Try flatMap(Function super T, Try> f) {
return new Failure<>(getFailure());
}
@Override
public String toString() {
return "failure(" + getFailure() + ")";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy