/** Performs the given action if this is a Right. */ public final void ifRight(Consumer<? super R> consumer) { if (isRight()) { consumer.accept(getRight()); } }
/** Performs the given action if this is a Right. */ public final void ifRight(Consumer<? super R> consumer) { requireNonNull(consumer); if (isRight()) { consumer.accept(getRight()); } }
/** Performs the given action if this is a Right. */ default void ifRight(Consumer<? super R> consumer) { if (isRight()) { consumer.accept(getRight()); } }
/** Applies either the left or the right function as appropriate. */ public final <T> T fold(Function<? super L, ? extends T> left, Function<? super R, ? extends T> right) { requireNonNull(left); requireNonNull(right); if (isLeft()) { return left.apply(getLeft()); } else { return right.apply(getRight()); } }
/** Applies either the left or the right function as appropriate. */ default <T> T fold(Function<? super L, ? extends T> left, Function<? super R, ? extends T> right) { if (isLeft()) { return left.apply(getLeft()); } else { return right.apply(getRight()); } }
/** Accepts either the left or the right consumer as appropriate. */ public final void accept(Consumer<? super L> left, Consumer<? super R> right) { requireNonNull(left); requireNonNull(right); if (isLeft()) { left.accept(getLeft()); } else { right.accept(getRight()); } }
/** Accepts either the left or the right consumer as appropriate. */ public final void accept(Consumer<? super L> left, Consumer<? super R> right) { if (isLeft()) { left.accept(getLeft()); } else { right.accept(getRight()); } }
/** Applies either the left or the right function as appropriate. */ public final <T> T fold(Function<? super L, ? extends T> left, Function<? super R, ? extends T> right) { if (isLeft()) { return left.apply(getLeft()); } else { return right.apply(getRight()); } }
@SuppressWarnings("unchecked") public final <T> Either<L, T> mapRight(Function<? super R, ? extends T> mapper) { requireNonNull(mapper); if (isLeft()) { return (Either<L, T>) this; } else { return Either.createRight(mapper.apply(getRight())); } }
/** Accepts either the left or the right consumer as appropriate. */ default void accept(Consumer<? super L> left, Consumer<? super R> right) { if (isLeft()) { left.accept(getLeft()); } else { right.accept(getRight()); } }
/** 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, @Nullable L defaultLeft, @Nullable R defaultRight) { left.accept(isLeft() ? getLeft() : defaultLeft); right.accept(isRight() ? getRight() : defaultRight); }
@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())); } }
@SuppressWarnings("unchecked") default <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. */ 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); }
/** 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()); }
@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()); }