@SuppressWarnings("unchecked") default <T> Either<T, R> mapLeft(Function<? super L, ? extends T> mapper) { if (isLeft()) { return Either.createLeft(mapper.apply(getLeft())); } else { return (Either<T, R>) this; } }
@SuppressWarnings("unchecked") public final <T> Either<L, T> mapRight(Function<? super R, ? extends T> mapper) { if (isLeft()) { return (Either<L, T>) this; } else { return Either.createRight(mapper.apply(getRight())); } }
/** Accepts both the left and right consumers, using the default values to set the empty side. */ default void acceptBoth(Consumer<? super L> left, Consumer<? super R> right, L defaultLeft, R defaultRight) { left.accept(isLeft() ? getLeft() : defaultLeft); right.accept(isRight() ? getRight() : defaultRight); }
@Test public void testLeft() { Either<TimeUnit, String> left = Either.createLeft(TimeUnit.DAYS); Assert.assertTrue(left.isLeft()); Assert.assertFalse(left.isRight()); Assert.assertEquals(TimeUnit.DAYS, left.getLeft()); Assert.assertEquals(Optional.of(TimeUnit.DAYS), left.asOptionalLeft()); Assert.assertEquals(Optional.empty(), left.asOptionalRight()); try { left.getRight(); Assert.fail(); } catch (Unhandled e) {} Assert.assertEquals("DAYS", left.fold(TimeUnit::toString, Function.identity())); Box.Nullable<TimeUnit> leftSide = Box.Nullable.ofNull(); Box.Nullable<String> rightSide = Box.Nullable.ofNull(); left.acceptBoth(leftSide, rightSide, TimeUnit.HOURS, "wahoo"); Assert.assertEquals(TimeUnit.DAYS, leftSide.get()); Assert.assertEquals("wahoo", rightSide.get()); }
public PureFrpBased(Composite parent, int initialValue) { super(parent, initialValue); Observable<Either<Integer, Exception>> observable = SwtRx.addListener(inputField, SWT.Modify).map(event -> { try { return Either.createLeft(Integer.parseInt(inputField.getText())); } catch (Exception error) { return Either.createRight(error); } }); Rx.subscribe(observable, either -> { String msg = either.fold(this::msgForValue, this::msgForError); outputField.setText(msg); }); } }
/** Creates a left or right, depending on which element is non-null. Precisely one element should be non-null. */ public static <L, R> Either<L, R> create(L l, R r) { if (l == null && r != null) { return createRight(r); } else if (l != null && r == null) { return createLeft(l); } else { if (l == null) { throw new IllegalArgumentException("Both arguments were null."); } else { throw new IllegalArgumentException("Both arguments were non-null: " + l + " " + r); } } }
/** Returns the left side as an Optional. */ default Optional<L> asOptionalLeft() { return fold(Optional::of, val -> Optional.<L> empty()); }
/** True if it's right. */ public final boolean isRight() { return !isLeft(); }
@Test public void testRight() { Either<TimeUnit, String> right = Either.createRight("word"); Assert.assertTrue(right.isRight()); Assert.assertFalse(right.isLeft()); Assert.assertEquals("word", right.getRight()); Assert.assertEquals(Optional.of("word"), right.asOptionalRight()); Assert.assertEquals(Optional.empty(), right.asOptionalLeft()); try { right.getLeft(); Assert.fail(); } catch (Unhandled e) {} Assert.assertEquals("word", right.fold(TimeUnit::toString, Function.identity())); Box.Nullable<TimeUnit> leftSide = Box.Nullable.ofNull(); Box.Nullable<String> rightSide = Box.Nullable.ofNull(); right.acceptBoth(leftSide, rightSide, TimeUnit.HOURS, "wahoo"); Assert.assertEquals(TimeUnit.HOURS, leftSide.get()); Assert.assertEquals("word", rightSide.get()); }
/** Creates a left or right, depending on which element is non-null. Precisely one element should be non-null. */ public static <L, R> Either<L, R> create(L l, R r) { if (l == null && r != null) { return createRight(r); } else if (l != null && r == null) { return createLeft(l); } else { if (l == null) { throw new IllegalArgumentException("Both arguments were null."); } else { throw new IllegalArgumentException("Both arguments were non-null: " + l + " " + r); } } }
/** Returns the right side as an Optional. */ default Optional<R> asOptionalRight() { return fold(val -> Optional.<R> empty(), Optional::of); }
/** True if it's right. */ public final boolean isRight() { return !isLeft(); }
/** Accepts both the left and right consumers, using the default values to set the empty side. */ public final void acceptBoth(Consumer<? super L> left, Consumer<? super R> right, L defaultLeft, R defaultRight) { left.accept(isLeft() ? getLeft() : defaultLeft); right.accept(isRight() ? getRight() : defaultRight); }