Eithers { public static <A, B> Either<A, B> flatten(Either<A, Either<A, B>> e) { return e.right().bind(new Function<Either<A, B>, Either<A, B>>() { @Override public Either<A, B> apply(Either<A, B> e) { return e.right().either(); } }); } private  Eithers(); static Either<A, B> flatten(Either<A, Either<A, B>> e);  }
@Test public void testFlatten() throws Exception { { Either<String, Either<String, Integer>> e = left("error"); assertEquals("error", e.left().value()); } { Either<String, Either<String, Integer>> e = right(Either.<String, Integer>left("error")); assertTrue(flatten(e).isLeft()); assertEquals("error", flatten(e).left().value()); } { Either<String, Either<String, Integer>> e = right(Either.<String, Integer>right(1)); assertTrue(flatten(e).isRight()); assertEquals(new Integer(1), flatten(e).right().value()); } }