scala.util.Left Java Examples

The following examples show how to use scala.util.Left. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LimitRateByRejection.java    From ditto with Eclipse Public License 2.0 7 votes vote down vote up
@Override
public Iterable<Either<E, A>> apply(final A element) {
    final long currentTime = System.currentTimeMillis();
    if (currentTime - previousWindow >= windowSizeMillis) {
        previousWindow = currentTime;
        counter = 1;
    } else {
        counter++;
    }
    final Either<E, A> result;
    if (counter <= maxElements) {
        result = Right.apply(element);
    } else {
        result = Left.apply(errorReporter.apply(element));
    }
    return Collections.singletonList(result);
}
 
Example #2
Source File: ScalaFeelEngine.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
public <T> T evaluateSimpleExpression(String expression, VariableContext variableContext) {

    CustomContext context = new CustomContext() {
      public VariableProvider variableProvider() {
        return new ContextVariableWrapper(variableContext);
      }
    };

    Either either = feelEngine.evalExpression(expression, context);

    if (either instanceof Right) {
      Right right = (Right) either;

      return (T) right.value();

    } else {
      Left left = (Left) either;
      Failure failure = (Failure) left.value();
      String message = failure.message();

      throw LOGGER.evaluationException(message);

    }
  }
 
Example #3
Source File: MessageMappingProcessorActor.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
private static Either<RuntimeException, AuthorizationContext> getAuthorizationContextAsEither(
        final ExternalMessage externalMessage) {

    return externalMessage.getAuthorizationContext()
            .filter(authorizationContext -> !authorizationContext.isEmpty())
            .<Either<RuntimeException, AuthorizationContext>>map(authorizationContext -> {
                try {
                    return new Right<>(PlaceholderFilter.applyHeadersPlaceholderToAuthContext(authorizationContext,
                            externalMessage.getHeaders()));
                } catch (final RuntimeException e) {
                    return new Left<>(e);
                }
            })
            .orElseGet(() ->
                    new Left<>(new IllegalArgumentException("No nonempty authorization context is available")));

}
 
Example #4
Source File: GDALJavaUtils.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static void saveRaster(Dataset dataset, String filename, Bounds bounds, double nodata, String format)
{
  GDALUtils.saveRaster(
      dataset,
      new Left<>(filename),
      bounds,
      nodata,
      format,
      new String[]{}
  );
}
 
Example #5
Source File: GDALJavaUtils.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static void saveRasterTile(Dataset dataset, String filename, long tx, long ty, int zoom, double nodata)
{
  GDALUtils.saveRasterTile(
      dataset,
      new Left<>(filename),
      tx, ty, zoom,
      nodata,
      "GTiff",
      new String[]{}
  );
}
 
Example #6
Source File: GDALJavaUtils.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static void saveRasterTile(Dataset dataset, String filename, long tx, long ty, int zoom, double nodata,
    String format)
{
  GDALUtils.saveRasterTile(
      dataset,
      new Left<>(filename),
      tx, ty, zoom,
      nodata,
      format,
      new String[]{}
  );
}
 
Example #7
Source File: GDALJavaUtils.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static void saveRasterTile(Dataset dataset, String filename, long tx, long ty, int zoom, String[] options)
{
  GDALUtils.saveRasterTile(
      dataset,
      new Left<>(filename),
      tx, ty, zoom,
      Double.NEGATIVE_INFINITY,
      "GTiff",
      options
  );
}
 
Example #8
Source File: GDALJavaUtils.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static void saveRasterTile(Dataset dataset, String filename, long tx, long ty, int zoom, String format,
    String[] options)
{
  GDALUtils.saveRasterTile(
      dataset,
      new Left<>(filename),
      tx, ty, zoom,
      Double.NEGATIVE_INFINITY,
      format,
      options
  );
}
 
Example #9
Source File: GDALJavaUtils.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static void saveRasterTile(Dataset dataset, String filename, long tx, long ty, int zoom, double nodata,
    String[] options)
{
  GDALUtils.saveRasterTile(
      dataset,
      new Left<>(filename),
      tx, ty, zoom,
      nodata,
      "GTiff",
      options
  );
}
 
Example #10
Source File: GDALJavaUtils.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static void saveRasterTile(Dataset dataset, String filename, long tx, long ty, int zoom, double nodata,
    String format, String[] options)
{
  GDALUtils.saveRasterTile(
      dataset,
      new Left<>(filename),
      tx, ty, zoom,
      nodata,
      format,
      options
  );
}
 
Example #11
Source File: GDALJavaUtils.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static void saveRaster(Dataset dataset, String filename, Bounds bounds)
{
  GDALUtils.saveRaster(
      dataset,
      new Left<>(filename),
      bounds,
      Double.NEGATIVE_INFINITY,
      "GTiff",
      new String[]{}
  );
}
 
Example #12
Source File: GDALJavaUtils.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static void saveRaster(Dataset dataset, String filename, Bounds bounds, String format)
{
  GDALUtils.saveRaster(
      dataset,
      new Left<>(filename),
      bounds,
      Double.NEGATIVE_INFINITY,
      format,
      new String[]{}
  );
}
 
Example #13
Source File: GDALJavaUtils.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static void saveRaster(Dataset dataset, String filename, Bounds bounds, double nodata)
{
  GDALUtils.saveRaster(
      dataset,
      new Left<>(filename),
      bounds,
      nodata,
      "GTiff",
      new String[]{}
  );
}
 
Example #14
Source File: GDALJavaUtils.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static void saveRasterTile(Dataset dataset, String filename, long tx, long ty, int zoom)
{
  GDALUtils.saveRasterTile(
      dataset,
      new Left<>(filename),
      tx, ty, zoom,
      Double.NEGATIVE_INFINITY,
      "GTiff",
      new String[]{}
  );
}
 
Example #15
Source File: GDALJavaUtils.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static void saveRaster(Dataset dataset, String filename, Bounds bounds, String[] options)
{
  GDALUtils.saveRaster(
      dataset,
      new Left<>(filename),
      bounds,
      Double.NEGATIVE_INFINITY,
      "GTiff",
      options
  );
}
 
Example #16
Source File: GDALJavaUtils.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static void saveRaster(Dataset dataset, String filename, Bounds bounds, String format, String[] options)
{
  GDALUtils.saveRaster(
      dataset,
      new Left<>(filename),
      bounds,
      Double.NEGATIVE_INFINITY,
      format,
      options
  );
}
 
Example #17
Source File: GDALJavaUtils.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static void saveRaster(Dataset dataset, String filename, Bounds bounds, double nodata, String[] options)
{
  GDALUtils.saveRaster(
      dataset,
      new Left<>(filename),
      bounds,
      nodata,
      "GTiff",
      options
  );
}
 
Example #18
Source File: GDALJavaUtils.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static void saveRaster(Dataset dataset, String filename, Bounds bounds, double nodata, String format,
    String[] options)
{
  GDALUtils.saveRaster(
      dataset,
      new Left<>(filename),
      bounds,
      nodata,
      format,
      options
  );
}
 
Example #19
Source File: ExceptionWrapperFunction.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Tuple2<K, ? extends Either<Exception, V>> next() {
    if (caught != null) {
        consumed = true;
        return new Tuple2<>(null, new Left<Exception, V>(caught));
    }
    try {
        Tuple2<K, V> result = delegate.next();
        return new Tuple2<>(result._1(), new Right<Exception, V>(result._2()));
    } catch (Exception e) {
        consumed = true;
        return new Tuple2<>(null, new Left<Exception, V>(e));
    }
}
 
Example #20
Source File: ScalaFeelEngine.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public boolean evaluateSimpleUnaryTests(String expression,
                                        String inputVariable,
                                        VariableContext variableContext) {
  Map inputVariableMap = new Map.Map1(UnaryTests$.MODULE$.inputVariable(), inputVariable);

  StaticVariableProvider inputVariableContext = new StaticVariableProvider(inputVariableMap);

  ContextVariableWrapper contextVariableWrapper = new ContextVariableWrapper(variableContext);

  CustomContext context = new CustomContext() {
    public VariableProvider variableProvider() {
      return new CompositeVariableProvider(toScalaList(inputVariableContext, contextVariableWrapper));
    }
  };

  Either either = feelEngine.evalUnaryTests(expression, context);

  if (either instanceof Right) {
    Right right = (Right) either;
    Object value = right.value();

    return BoxesRunTime.unboxToBoolean(value);

  } else {
    Left left = (Left) either;
    Failure failure = (Failure) left.value();
    String message = failure.message();

    throw LOGGER.evaluationException(message);

  }
}
 
Example #21
Source File: GDALJavaUtils.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static void saveRaster(Dataset dataset, String filename, String format)
{
  GDALUtils.saveRaster(
      dataset,
      new Left<>(filename),
      null,
      Double.NEGATIVE_INFINITY,
      format,
      new String[]{}
  );
}
 
Example #22
Source File: ClusterStatusStage.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private static Either<CreditDecision, Integer> countMembersOfExpectedRoles(final ClusterStatus clusterStatus) {
    final List<Integer> reachableMembersOfExpectedRoles = clusterStatus.getRoles()
            .stream()
            .filter(role -> EXPECTED_ROLES.contains(role.getRole()))
            .map(role -> role.getReachable().size())
            .collect(Collectors.toList());

    if (reachableMembersOfExpectedRoles.size() == EXPECTED_ROLES.size()) {
        return new Right<>(reachableMembersOfExpectedRoles.stream().mapToInt(Integer::intValue).sum());
    } else {
        // some expected roles are not reachable; do not return members count.
        return new Left<>(
                CreditDecision.no("Not all expected roles " + EXPECTED_ROLES + " are present: " + clusterStatus));
    }
}
 
Example #23
Source File: ResumeSource.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Iterable<Either<FailureWithLookBehind<E>, E>> apply(final Envelope<E> param) {
    if (param instanceof Element) {
        final E element = ((Element<E>) param).element;
        enqueue(element);
        return Collections.singletonList(new Right<>(element));
    } else if (param instanceof Error) {
        final List<E> finalElementsCopy = new ArrayList<>(finalElements);
        final Throwable error = ((Error<E>) param).error;
        return Collections.singletonList(new Left<>(new FailureWithLookBehind<>(finalElementsCopy, error)));
    } else {
        // param is EOS; this is not supposed to happen.
        throw new IllegalStateException("End-of-stream encountered; stream should be complete already!");
    }
}
 
Example #24
Source File: WebSocketRoute.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private Graph<FanOutShape2<String, Either<StreamControlMessage, Signal>, DittoRuntimeException>, NotUsed>
selectStreamControlOrSignal(
        final JsonSchemaVersion version,
        final CharSequence connectionCorrelationId,
        final AuthorizationContext connectionAuthContext,
        final DittoHeaders additionalHeaders,
        final ProtocolAdapter adapter) {

    final ProtocolMessageExtractor protocolMessageExtractor =
            new ProtocolMessageExtractor(connectionAuthContext, connectionCorrelationId);

    return Filter.multiplexByEither(cmdString -> {
        final Optional<StreamControlMessage> streamControlMessage = protocolMessageExtractor.apply(cmdString);
        if (streamControlMessage.isPresent()) {
            return Right.apply(Left.apply(streamControlMessage.get()));
        } else {
            try {
                final Signal<?> signal = buildSignal(cmdString, version, connectionCorrelationId,
                        connectionAuthContext, additionalHeaders, adapter, headerTranslator);
                return Right.apply(Right.apply(signal));
            } catch (final DittoRuntimeException dre) {
                // This is a client error usually; log at level DEBUG without stack trace.
                LOGGER.withCorrelationId(dre)
                        .debug("DittoRuntimeException building signal from <{}>: <{}>", cmdString, dre);
                return Left.apply(dre);
            } catch (final Exception throwable) {
                LOGGER.warn("Error building signal from <{}>: {}: <{}>", cmdString,
                        throwable.getClass().getSimpleName(), throwable.getMessage());
                final DittoRuntimeException dittoRuntimeException = GatewayInternalErrorException.newBuilder()
                        .cause(throwable)
                        .build();
                return Left.apply(dittoRuntimeException);
            }
        }
    });
}
 
Example #25
Source File: CucumberReportAdapterTest.java    From openCypher with Apache License 2.0 5 votes vote down vote up
@Override
public Either<ExecutionFailed, CypherValueRecords> cypher(String query, Map<String, CypherValue> params, QueryType meta) {
    if (query.contains("foo()")) {
        return new Left<>(new ExecutionFailed("SyntaxError", "compile time", "UnknownFunction", null));
    } else if (query.startsWith("RETURN ")) {
        String result = query.replace("RETURN ", "");
        System.out.println("Producing some output " + result);
        List<String> header = new Set.Set1<>(result).toList();
        Map<String, String> row = new Map.Map1<>(result, result);
        List<Map<String, String>> rows = new Set.Set1<>(row).toList();
        return new Right<>(new StringRecords(header, rows).asValueRecords());
    } else {
        return new Right<>(CypherValueRecords.empty());
    }
}
 
Example #26
Source File: GDALJavaUtils.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static void saveRaster(Dataset dataset, String filename)
{
  GDALUtils.saveRaster(
      dataset,
      new Left<>(filename),
      null,
      Double.NEGATIVE_INFINITY,
      "GTiff",
      new String[]{}
  );
}
 
Example #27
Source File: GDALJavaUtils.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static void saveRasterTile(Dataset dataset, String filename, long tx, long ty, int zoom, String format)
{
  GDALUtils.saveRasterTile(
      dataset,
      new Left<>(filename),
      tx, ty, zoom,
      Double.NEGATIVE_INFINITY,
      format,
      new String[]{}
  );
}
 
Example #28
Source File: GDALJavaUtils.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static void saveRaster(Dataset dataset, String filename, double nodata)
{
  GDALUtils.saveRaster(
      dataset,
      new Left<>(filename),
      null,
      nodata,
      "GTiff",
      new String[]{}
  );
}
 
Example #29
Source File: GDALJavaUtils.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static void saveRaster(Dataset dataset, String filename, double nodata, String format)
{
  GDALUtils.saveRaster(
      dataset,
      new Left<>(filename),
      null,
      nodata,
      format,
      new String[]{}
  );
}
 
Example #30
Source File: GDALJavaUtils.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static void saveRaster(Dataset dataset, String filename, String[] options)
{
  GDALUtils.saveRaster(
      dataset,
      new Left<>(filename),
      null,
      Double.NEGATIVE_INFINITY,
      "GTiff",
      options
  );
}