feat: extend EvaluationResult to be usable internally as well

Signed-off-by: Calum Murray <cmurray@redhat.com>
This commit is contained in:
Calum Murray 2024-06-19 09:28:28 -04:00
parent 9b8c9df85c
commit 1c895dd07f
No known key found for this signature in database
GPG Key ID: D9837BD1D90C1512
1 changed files with 64 additions and 5 deletions

View File

@ -2,20 +2,79 @@ package io.cloudevents.sql.impl.runtime;
import io.cloudevents.sql.EvaluationException;
import io.cloudevents.sql.Result;
import io.cloudevents.sql.Type;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class EvaluationResult implements Result {
private final Object value;
private final List<EvaluationException> exceptions;
private final EvaluationException latestException;
public EvaluationResult(Object value, List<EvaluationException> exceptions) {
this.value = value;
this.exceptions = exceptions == null ? Collections.emptyList() : Collections.unmodifiableList(exceptions);
this.exceptions = exceptions == null ? new ArrayList<>() : exceptions;
this.latestException = null;
}
public EvaluationResult(Object value, EvaluationException exception, EvaluationResult left, EvaluationResult right) {
this.exceptions = Stream.concat(Stream.of(left, right).filter(Objects::nonNull).map(r -> r.exceptions).flatMap(Collection::stream), Stream.of(exception))
.filter(Objects::nonNull)
.collect(Collectors.toList());
this.latestException = exception;
this.value = value;
}
public EvaluationResult(Object value, EvaluationException exception) {
this(value, exception, null, null);
}
public EvaluationResult(Object value) {
this.value = value;
this.exceptions = new ArrayList<>();
this.latestException = null;
}
public EvaluationResult wrap(EvaluationResult other) {
if (other != null && other.exceptions != null) {
return this.wrap(other.exceptions);
}
return this;
}
public EvaluationResult wrap(List<EvaluationException> exceptions) {
if (!exceptions.isEmpty()) {
this.exceptions.addAll(exceptions);
}
return this;
}
public EvaluationResult copyWithValue(Object value) {
return new EvaluationResult(value, this.exceptions);
}
public EvaluationResult copyWithDefaultValueForType(Type type) {
Object value;
switch (type) {
case STRING:
value = "";
break;
case INTEGER:
value = 0;
break;
default:
value = false;
break;
}
return new EvaluationResult(value, this.exceptions);
}
// returns true is the most recent exception was a MISSING attribute exception
public boolean isMissingAttributeException() {
return (this.latestException != null && this.latestException.getKind() == EvaluationException.ErrorKind.MISSING_ATTRIBUTE);
}
/**