import com.google.common.annotations.Beta;
import com.google.common.util.concurrent.FluentFuture;
import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
-import javax.annotation.concurrent.ThreadSafe;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
*/
@Beta
@NonNullByDefault
-@ThreadSafe
-public class CheckedValue<T, E extends Exception> extends Variant<T, E> {
+public class CheckedValue<T, E extends Exception> extends Either<T, E> {
protected CheckedValue(final T value) {
super(value);
}
* Create a new instance containing an {@link Exception}.
*
* @param cause Throwable
+ * @param <T> Value type
+ * @param <E> Exception type
* @return A new instance
* @throws NullPointerException if {@code cause} is null
*/
* Create a new instance containing specified value.
*
* @param value Value
+ * @param <T> Value type
+ * @param <E> Exception type
* @return A new instance
* @throws NullPointerException if {@code value} is null
*/
* Convert a Variant into a {@link CheckedValue}, converting the second value into an exception.
*
* @param variant Input variant
+ * @param mapper Mapping function from second alternative to an exception
+ * @param <T> First alternative type
+ * @param <U> Second alternative type
+ * @param <E> Exception type
* @return Resulting {@link CheckedValue}
*/
- public static <T, U, E extends Exception> CheckedValue<T, E> ofVariant(final Variant<T, U> variant,
+ public static <T, U, E extends Exception> CheckedValue<T, E> ofVariant(final Either<T, U> variant,
final Function<U, E> mapper) {
requireNonNull(mapper);
- return variant.isFirst() ? new CheckedValue(variant.first())
- : new CheckedValue(mapper.apply(variant.second()), null);
+ return variant.isFirst() ? new CheckedValue<>(variant.first())
+ : new CheckedValue<>(mapper.apply(variant.second()), null);
}
/**
@SuppressWarnings("unchecked")
public <X extends Exception> CheckedValue<T, X> mapException(final Function<? super E, X> mapper) {
requireNonNull(mapper);
- if (isFirst()) {
- return (CheckedValue<T, X>) this;
- }
- return new CheckedValue<>(mapper.apply(second()), null);
+ return isFirst() ? (CheckedValue<T, X>) this : new CheckedValue<>(mapper.apply(second()), null);
}
-
@SuppressWarnings("unchecked")
public <U> CheckedValue<U, E> flatMap(final Function<? super T, CheckedValue<U, E>> mapper) {
requireNonNull(mapper);
* @return Contained value
* @throws E When there is no contained value
*/
- public final <X extends Throwable> T orElseThrow() throws E {
+ public final T orElseThrow() throws E {
if (isFirst()) {
return first();
}
* Return contained value if present or throw the exception alternative mapped through provided mapper.
*
* @param exceptionMapper Exception mapper
+ * @param <X> Thrown exception type
* @return Contained value
* @throws NullPointerException if {@code exceptionMapper} is null
* @throws X When there is no contained value
* Return contained value if present or throw the exception supplied by supplier.
*
* @param supplier Exception supplier
+ * @param <X> Thrown exception type
* @return Contained value
* @throws NullPointerException if {@code exceptionMapper} is null
* @throws X When there is no contained value
* @return A {@link CompletableFuture}.
*/
public final CompletableFuture<T> toCompletableFuture() {
- if (isFirst()) {
- return CompletableFuture.completedFuture(first());
- }
- // FIXME: Java 9: use CompletableFuture.failedFuture()
- final CompletableFuture<T> future = new CompletableFuture<>();
- future.completeExceptionally(second());
- return future;
+ return isFirst() ? CompletableFuture.completedFuture(first()) : CompletableFuture.failedFuture(second());
}
/**
* @return A {@link FluentFuture}.
*/
public final FluentFuture<T> toFluentFuture() {
- final ListenableFuture<T> future;
- if (isFirst()) {
- future = Futures.immediateFuture(first());
- } else {
- future = Futures.immediateFailedFuture(second());
- }
- return FluentFuture.from(future);
+ return FluentFuture.from(isFirst() ? Futures.immediateFuture(first())
+ : Futures.immediateFailedFuture(second()));
}
}