scala.util.Either.isRight()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(8.0k)|赞(0)|评价(0)|浏览(136)

本文整理了Java中scala.util.Either.isRight()方法的一些代码示例,展示了Either.isRight()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Either.isRight()方法的具体详情如下:
包路径:scala.util.Either
类名称:Either
方法名:isRight

Either.isRight介绍

暂无

代码示例

代码示例来源:origin: com.yahoo.maha/maha-par-request-2

/**
 * Maps either by applying function doWithRight if the Either isRight(), otherwise returns the input either with
 * cast to <A,U>
 */
public static <E, A, U> Either<E, U> flatMap(Function<A, Either<E, U>> doWithRight, Either<E, A> either) {
  if (either.isRight()) {
    return doWithRight.apply(either.right().get());
  } else {
    return ((Either<E, U>) either);
  }
}

代码示例来源:origin: com.yahoo.maha/maha-par-request-2

/**
 * Maps either by applying function doWithRight if the Either isRight(), otherwise, it passes the Either through
 * with cast to <E,U>
 */
public static <U, E, A> Either<E, U> map(Function<A, U> doWithRight, Either<E,A> either) {
  if (either.isRight()) {
    return new Right<>(doWithRight.apply(either.right().get()));
  } else {
    return ((Either<E, U>) either);
  }
}

代码示例来源:origin: srikalyc/Sql4D

/**
 * Left is error Right is Tuple <dimensions, metrics>
 *
 * @param name
 * @param reqHeaders
 * @return
 */
public Either<String, Tuple2<List<String>, List<String>>> aboutDataSource(String name, Map<String, String> reqHeaders) {
  Either<String, Either<JSONArray, JSONObject>> resp = fireCommand("druid/coordinator/v1/metadata/datasources/" + name, null, reqHeaders);
  if (resp.isLeft()) {
    return new Left<>(resp.left().get());
  }
  Either<JSONArray, JSONObject> goodResp = resp.right().get();
  if (goodResp.isRight()) {
    JSONObject data = goodResp.right().get();
    if (data.has("segments")) {
      JSONArray segmentsArray = data.getJSONArray("segments");
      if (segmentsArray.length() == 0) {
        return new Left<>("No segments received..");
      }
      JSONObject firstItem = segmentsArray.getJSONObject(0);
      String dims = firstItem.getString("dimensions");
      String metrics = firstItem.getString("metrics");
      return new Right<>(new Tuple2<>(Arrays.asList(dims.split(",")), Arrays.asList(metrics.split(","))));
    } else {
      return new Left<>("No segments key in the response..");
    }
  }
  return new Left<>("Unexpected response " + goodResp.left().get().toString());
}

代码示例来源:origin: com.yahoo.sql4d/Sql4Ddriver

/**
 * Left is error Right is Tuple <dimensions, metrics>
 *
 * @param name
 * @param reqHeaders
 * @return
 */
public Either<String, Tuple2<List<String>, List<String>>> aboutDataSource(String name, Map<String, String> reqHeaders) {
  Either<String, Either<JSONArray, JSONObject>> resp = fireCommand("druid/coordinator/v1/metadata/datasources/" + name, null, reqHeaders);
  if (resp.isLeft()) {
    return new Left<>(resp.left().get());
  }
  Either<JSONArray, JSONObject> goodResp = resp.right().get();
  if (goodResp.isRight()) {
    JSONObject data = goodResp.right().get();
    if (data.has("segments")) {
      JSONArray segmentsArray = data.getJSONArray("segments");
      if (segmentsArray.length() == 0) {
        return new Left<>("No segments received..");
      }
      JSONObject firstItem = segmentsArray.getJSONObject(0);
      String dims = firstItem.getString("dimensions");
      String metrics = firstItem.getString("metrics");
      return new Right<>(new Tuple2<>(Arrays.asList(dims.split(",")), Arrays.asList(metrics.split(","))));
    } else {
      return new Left<>("No segments key in the response..");
    }
  }
  return new Left<>("Unexpected response " + goodResp.left().get().toString());
}

代码示例来源:origin: eclipse/ditto

@Override
public SSLContext clientCertificate(final ClientCertificateCredentials credentials) {
  final String clientKeyPem = credentials.getClientKey().orElse(null);
  final String clientCertificatePem = credentials.getClientCertificate().orElse(null);
  final Supplier<KeyManager[]> keyManagerSupplier = newKeyManagerFactory(clientKeyPem, clientCertificatePem);
  final Supplier<TrustManager[]> trustManagerFactory = trust.isRight()
      ? newTrustManagerFactory(trust.right().get())
      : () -> new TrustManager[]{trust.left().get()};
  return newTLSContext(keyManagerSupplier, trustManagerFactory);
}

代码示例来源:origin: org.eclipse.ditto/ditto-services-connectivity-messaging

@Override
public SSLContext clientCertificate(final ClientCertificateCredentials credentials) {
  final String clientKeyPem = credentials.getClientKey().orElse(null);
  final String clientCertificatePem = credentials.getClientCertificate().orElse(null);
  final Supplier<KeyManager[]> keyManagerSupplier = newKeyManagerFactory(clientKeyPem, clientCertificatePem);
  final Supplier<TrustManager[]> trustManagerFactory = trust.isRight()
      ? newTrustManagerFactory(trust.right().get())
      : () -> new TrustManager[]{trust.left().get()};
  return newTLSContext(keyManagerSupplier, trustManagerFactory);
}

代码示例来源:origin: kframework/k

private Stream<? extends K> performParse(Map<String, ParsedSentence> cache, ParseInModule parser, Scanner scanner, Bubble b) {
    int startLine = b.att().get("contentStartLine", Integer.class);
    int startColumn = b.att().get("contentStartColumn", Integer.class);
    Source source = b.att().get(Source.class);
    Tuple2<Either<java.util.Set<ParseFailedException>, K>, java.util.Set<ParseFailedException>> result;
    if (cache.containsKey(b.contents())) {
      ParsedSentence parse = cache.get(b.contents());
      Optional<Source> cacheSource = parse.getParse().source();
      //Cache might contain content from an identical file but another source path.
      //The content will have wrong Source attribute and must be invalidated.
      if (cacheSource.isPresent() && cacheSource.get().equals(source)) {
        cachedBubbles.getAndIncrement();
        kem.addAllKException(parse.getWarnings().stream().map(e -> e.getKException()).collect(Collectors.toList()));
        return Stream.of(parse.getParse());
      }
    }
    result = parser.parseString(b.contents(), START_SYMBOL, scanner, source, startLine, startColumn, !b.att().contains("macro") && !b.att().contains("alias"));
    parsedBubbles.getAndIncrement();
    kem.addAllKException(result._2().stream().map(e -> e.getKException()).collect(Collectors.toList()));
    if (result._1().isRight()) {
      KApply k = (KApply) new TreeNodesToKORE(Outer::parseSort, isStrict).down(result._1().right().get());
      k = KApply(k.klabel(), k.klist(), k.att().addAll(b.att().remove("contentStartLine").remove("contentStartColumn").remove(Source.class).remove(Location.class)));
      cache.put(b.contents(), new ParsedSentence(k, new HashSet<>(result._2())));
      return Stream.of(k);
    } else {
      errors.addAll(result._1().left().get());
      return Stream.empty();
    }
  }
}

代码示例来源:origin: kframework/k

public void throwFirstLeftException(Term input) {
    Either<Set<ParseFailedException>, Term> result = treeCleanerVisitor.apply(input);
    if (result.isRight()) {
      fail("Expected an exception but got:" + result.right().get());
    } else {
      throw result.left().get().iterator().next();
    }
  }
}

代码示例来源:origin: kframework/k

/**
   * Remove KList artifacts from parsing only when it contains a single element.
   */
  @Override
  public Either<Set<ParseFailedException>, Term> apply(KList node) {
    Either<Set<ParseFailedException>, Term> res = super.apply(node);

    if (res.isRight() && ((KList) res.right().get()).items().size() == 1)
      return Right.apply(((KList) res.right().get()).items().get(0));
    else
      return res;
  }
}

代码示例来源:origin: kframework/k

private void printout(Tuple2<Either<Set<ParseFailedException>, K>, Set<ParseFailedException>> rule, int warnings, boolean expectedError) {
  if (false) { // true to print detailed results
    KExceptionManager kem = new KExceptionManager(new GlobalOptions(true, Warnings.ALL, true));
    if (rule._1().isLeft()) {
      for (ParseFailedException x : rule._1().left().get()) {
        kem.addKException(x.getKException());
      }
    } else {
      System.err.println("rule = " + rule._1().right().get());
    }
    for (ParseFailedException x : rule._2()) {
      kem.addKException(x.getKException());
    }
    kem.print();
  }
  if (expectedError)
    Assert.assertTrue("Expected error here: ", rule._1().isLeft());
  else
    Assert.assertTrue("Expected no errors here: ", rule._1().isRight());
  Assert.assertEquals("Expected " + warnings + " warnings: ", warnings, rule._2().size());
}

相关文章

微信公众号

最新文章

更多