@Test public void right_A$() throws Exception { Either<String, Integer> target = Left.apply("foo"); Option<Integer> actual = target.right(); assertThat(actual.isDefined(), is(false)); assertThat(actual.getOrNull(), is(nullValue())); }
@Test public void left_A$() throws Exception { Either<String, Integer> target = Left.apply("foo"); Option<String> actual = target.left(); assertThat(actual.isDefined(), is(true)); assertThat(actual.getOrNull(), is(equalTo("foo"))); }
@Test public void toString_A$() throws Exception { Either<String, Integer> target = Left.apply("foo"); String actual = target.toString(); String expected = "Left(foo)"; assertThat(actual, is(equalTo(expected))); }
public <X> Either<T, X> toLeft(X right) { if (isDefined()) { return Either.eitherLeft(get()); } else { return Either.eitherRight(right); } }
@Test public void shouldBimapRight() { final Either<Integer, String> actual = Either.<Integer, String>right("1").bimap(i -> i + 1, s -> s + "1"); final Either<Integer, String> expected = Either.right("11"); assertThat(actual).isEqualTo(expected); }
public static Either<CommandResult> runCommand( final Context context, final String command, final File path, final List<String> arguments) { try { final List<String> commandAndArgs = new ArrayList<String>(); commandAndArgs.add(command); commandAndArgs.addAll(arguments); final ProcessBuilder pb = new ProcessBuilder(commandAndArgs); if (path != null) { pb.directory(path); } logCommand(context, pb); final Process compilation = pb.start(); final ConsumeStream result = ConsumeStream.start(compilation.getInputStream(), context); final ConsumeStream error = ConsumeStream.start(compilation.getErrorStream(), context); final int exitCode = compilation.waitFor(); result.join(); error.join(); if (result.exception != null) { return Either.fail(result.exception); } if (error.exception != null) { return Either.fail(error.exception); } return Either.success( new CommandResult(result.output.toString(), error.output.toString(), exitCode)); } catch (IOException ex) { return Either.fail(ex); } catch (InterruptedException ex) { return Either.fail(ex); } }
@Test public void shouldBimapLeft() { final Either<Integer, String> actual = Either.<Integer, String>left(1).bimap(i -> i + 1, s -> s + "1"); final Either<Integer, String> expected = Either.left(2); assertThat(actual).isEqualTo(expected); }
@Test public void isRight_A$() throws Exception { Either<String, Integer> target = Left.apply("foo"); boolean actual = target.isRight(); boolean expected = false; assertThat(actual, is(equalTo(expected))); }
public <X> Either<X, T> toRight(X left) { if (isDefined()) { return Either.eitherRight(get()); } else { return Either.eitherLeft(left); } }
@Test public void shouldPeekOnRightProjectionOfLeft() { final List<Integer> actual = new ArrayList<>(); final Either<String, Integer> testee = Either.<String, Integer>left("1").right().peek(actual::add).toEither(); assertThat(actual.isEmpty()).isTrue(); assertThat(testee).isEqualTo(Either.<String, Integer>left("1")); }
@Test public void shouldPeekOnRightProjectionOfRight() { final List<Integer> actual = new ArrayList<>(); final Either<String, Integer> testee = Either.<String, Integer>right(1).right().peek(actual::add).toEither(); assertThat(actual).isEqualTo(Collections.singletonList(1)); assertThat(testee).isEqualTo(Either.right(1)); }
@Test public void swap_A$() throws Exception { Either<String, Integer> target = Left.apply("foo"); Either<Integer, String> swapped = target.swap(); Option<String> right = swapped.right(); assertThat(right.isDefined(), is(true)); assertThat(right.getOrNull(), is(equalTo("foo"))); }
public Either<Object, Bitmap> getBitmap(String path) { return Either.collapse( Either.collapse( execNormalRequest(path, null, HTTPMethod.GET, CONTENT_TYPE_ANY) .bindRight(expectCodes(200))) .bindRight(getResultInputStream)) .bindRight(decodeBitmap); }
@Test public void shouldFlatMapOnRightProjectionOfLeft() { final Either<String, Integer> actual = Either.<String, Integer>left("1") .right() .flatMap(i -> Either.<String, Integer>right(i + 1).right()) .toEither(); assertThat(actual).isEqualTo(Either.left("1")); }
@Test public void shouldFlatMapOnLeftProjectionOfLeft() { final Either<Integer, String> actual = Either.<Integer, String>left(1) .left() .flatMap(i -> Either.<Integer, String>left(i + 1).left()) .toEither(); assertThat(actual).isEqualTo(Either.left(2)); }
public Either<Object, JSONArray> getJsonArrayExpectCodes( String path, Map<String, String> params, HTTPMethod method, int... codes) { return Either.collapse( Either.collapse( Either.collapse( execNormalRequest(path, params, method, CONTENT_TYPE_JSON) .bindRight(expectCodes(codes))) .bindRight(getBody)) .bindRight(Util.parseJsonArray)); }
@Override public void run(final Context context) throws ExitException { if (context.contains(INSTANCE)) { context.show("Validating DSL ..."); final Either<Boolean> result = DslCompiler.parse(context, DslPath.getDslPaths(context)); if (result.isSuccess()) { context.show("Parse successful."); } else { context.error(result.whyNot()); throw new ExitException(); } } }
public void onReceive(Object o) { Function f = (Function) (((Tuple3) o)._1); Object a = (Object) (((Tuple3) o)._2); play.mvc.Http.Context context = (play.mvc.Http.Context) (((Tuple3) o)._3); try { play.mvc.Http.Context.current.set(context); getSender().tell(Either.Right(f.apply(a))); } catch (Throwable t) { getSender().tell(Either.Left(t)); } finally { play.mvc.Http.Context.current.remove(); } }
public static Either<String> readFile(final File file) { try { final FileInputStream stream = new FileInputStream(file); try { final String content = read(stream); return Either.success(content); } finally { stream.close(); } } catch (IOException ex) { return Either.fail(ex); } }
public static synchronized Either<Document> readXml(final InputStream stream) { try { final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); return Either.success(dBuilder.parse(stream)); } catch (IOException ex) { return Either.fail(ex); } catch (ParserConfigurationException ex) { return Either.fail(ex); } catch (SAXException ex) { return Either.fail(ex); } }
@Test public void mergeToRight_A$Function1() throws Exception { String value = "xxx"; Either<String, Integer> target = Left.apply(value); Function1<String, Integer> leftToRight = new F1<String, Integer>() { public Integer apply(String v) throws Exception { return v.length(); } }; Integer actual = target.mergeToRight(leftToRight); Integer expected = 3; assertThat(actual, is(equalTo(expected))); }
@Test public void mergeToLeft_A$Function1() throws Exception { String value = "xxx"; Either<String, Integer> target = Left.apply(value); Function1<Integer, String> rightToLeft = new F1<Integer, String>() { public String apply(Integer v) { return v.toString(); } }; String actual = target.mergeToLeft(rightToLeft); String expected = "xxx"; assertThat(actual, is(equalTo(expected))); }
@Test public void transformingJustYieldsRight() { final Function<Optional<O>, Either<O, O>> maybeToEither = new OptionalToEither<O, O>(new ConstantSupplier<O>(O.ANOTHER)); final Either<O, O> got = maybeToEither.apply(Optional.<O>of(O.ONE)); Assert.assertEquals(Either.right(O.ONE), got); }
@Test public void transformingNothingYieldsLeft() { final Function<Optional<O>, Either<O, O>> maybeToEither = new OptionalToEither<O, O>(new ConstantSupplier<O>(O.ANOTHER)); final Either<O, O> got = maybeToEither.apply(Optional.<O>empty()); Assert.assertEquals(Either.left(O.ANOTHER), got); }
@SuppressWarnings("unchecked") @Test public void left() throws Exception { new Random() .ints(20, 0, 2) .mapToObj( i -> i == 0 ? Either.<String, Integer>left("left value (String)") : Either.<String, Integer>right(42)) .forEach( either -> either.apply( left -> System.out.println("received left value: " + left.substring(11)), right -> System.out.println( "received right value: 0x" + Integer.toHexString(right)))); }
@Test public void shouldFlatMapRightProjectionOfLeftOnRightProjectionOfRight() { final Either<String, String> good = Either.right("good"); final Either<String, String> bad = Either.left("bad"); final RightProjection<String, Tuple2<String, String>> actual = good.right().flatMap(g -> bad.right().map(b -> Tuple.of(g, b))); assertThat(actual.toEither()).isEqualTo(Either.left("bad")); }
@Test public void shouldReturnOtherWhenOrElseRunOnRightProjectionOfLeft() { final boolean[] actual = new boolean[] {false}; Either.<String, Integer>left("1") .right() .orElseRun( s -> { actual[0] = true; }); assertThat(actual[0]).isTrue(); }
@Test public void shouldReturnRightWhenOrElseRunOnRightProjectionOfRight() { final boolean[] actual = new boolean[] {true}; Either.<String, Integer>right(1) .right() .orElseRun( s -> { actual[0] = false; }); assertThat(actual[0]).isTrue(); }
public static Either<String> findCommand( final Context context, final String path, final String name, final String contains) { final String simple = path != null ? new File(path, name).getAbsolutePath() : name; if (testCommand(context, simple, contains, new ArrayList<String>())) { context.log("Found " + name + " in " + simple); return Either.success(simple); } if (isWindows()) { final String bat = path != null ? new File(path, name + ".bat").getAbsolutePath() : name + ".bat"; if (testCommand(context, bat, contains, new ArrayList<String>())) { context.log("Found " + name + " in " + bat); return Either.success(bat); } final String cmd = path != null ? new File(path, name + ".cmd").getAbsolutePath() : name + ".cmd"; if (testCommand(context, cmd, contains, new ArrayList<String>())) { context.log("Found " + name + " in " + cmd); return Either.success(cmd); } } return Either.fail("File not found: " + name); }
private static boolean parse( final String[] args, final Context context, final List<CompileParameter> parameters) { if (args.length == 1 && ("/?".equals(args[0]) || "-?".equals(args[0]) || "?".equals(args[0]))) { showHelpAndExit(context, true, parameters); return false; } final List<ParameterParser> customParsers = new ArrayList<ParameterParser>(); for (final CompileParameter cp : parameters) { if (cp instanceof ParameterParser) { customParsers.add((ParameterParser) cp); } } final List<String> errors = new ArrayList<String>(); for (String a : args) { if (a.startsWith("-") || a.startsWith("/")) a = a.substring(1); final int eq = a.indexOf('='); final String name = a.substring(0, eq != -1 ? eq : a.length()); final String value = eq == -1 ? null : a.substring(eq + 1); final CompileParameter cp = from(name, parameters); if (cp == null) { boolean matched = false; for (final ParameterParser parser : customParsers) { final Either<Boolean> tryParse = parser.tryParse(name, value, context); if (!tryParse.isSuccess()) { errors.add(tryParse.explainError()); matched = true; break; } else if (tryParse.get()) { matched = true; break; } } if (!matched) { errors.add("Unknown parameter: " + name); } } else { if (eq == -1 && cp.getUsage() != null) { if (cp instanceof ParameterParser) { Either<Boolean> tryParse = ((ParameterParser) cp).tryParse(name, null, context); if (tryParse.isSuccess() && tryParse.get()) { context.put(cp, null); } else { errors.add("Expecting " + cp.getUsage() + " after = for " + a); } } else { errors.add("Expecting " + cp.getUsage() + " after = for " + a); } } else { context.put(cp, value); } } } if (args.length == 0 || errors.size() > 0) { for (final String err : errors) { context.error(err); } showHelpAndExit(context, args.length == errors.size(), parameters); return false; } return true; }