Browse Source

Set1 refactoring

pull/968/head
AdultCensusSalaryPrediction 6 months ago
parent
commit
39428515f5
  1. 35
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/EvaluatorFactory.java
  2. 34
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java
  3. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/function/PassthruPathFunction.java
  4. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunction.java
  5. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/function/json/Append.java
  6. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/function/json/KeySetFunction.java
  7. 30
      json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/AbstractAggregation.java
  8. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/Average.java
  9. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/Max.java
  10. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/Min.java
  11. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/StandardDeviation.java
  12. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/Sum.java
  13. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/function/sequence/AbstractSequenceAggregation.java
  14. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/function/text/Concatenate.java
  15. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/function/text/Length.java
  16. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/path/FunctionPathToken.java
  17. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/path/PathToken.java

35
json-path/src/main/java/com/jayway/jsonpath/internal/filter/EvaluatorFactory.java

@ -375,27 +375,28 @@ public class EvaluatorFactory {
private static class NoneOfEvaluator implements Evaluator {
@Override
public boolean evaluate(ValueNode left, ValueNode right, Predicate.PredicateContext ctx) {
ValueListNode rightValueListNode;
if (right.isJsonNode()) {
ValueNode vn = right.asJsonNode().asValueListNode(ctx);
ValueListNode rightValueListNode = extractValueListNode(right, ctx);
ValueListNode leftValueListNode = extractValueListNode(left, ctx);
return areAllElementsDifferent(leftValueListNode, rightValueListNode);
}
private ValueListNode extractValueListNode(ValueNode node, Predicate.PredicateContext ctx) {
if (node.isJsonNode()) {
ValueNode vn = node.asJsonNode().asValueListNode(ctx);
if (vn.isUndefinedNode()) {
return false;
return null; // or handle undefined case accordingly
} else {
rightValueListNode = vn.asValueListNode();
return vn.asValueListNode();
}
} else {
rightValueListNode = right.asValueListNode();
return node.asValueListNode();
}
ValueListNode leftValueListNode;
if (left.isJsonNode()) {
ValueNode vn = left.asJsonNode().asValueListNode(ctx);
if (vn.isUndefinedNode()) {
return false;
} else {
leftValueListNode = vn.asValueListNode();
}
} else {
leftValueListNode = left.asValueListNode();
}
private boolean areAllElementsDifferent(ValueListNode leftValueListNode, ValueListNode rightValueListNode) {
if (leftValueListNode == null || rightValueListNode == null) {
return false; // or handle the case where one of the lists is null
}
for (ValueNode leftValueNode : leftValueListNode) {
@ -405,7 +406,9 @@ public class EvaluatorFactory {
}
}
}
return true;
}
}
}

34
json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java

@ -135,26 +135,44 @@ public abstract class ValueNode {
}
private static boolean isJson(Object o) {
if(o == null || !(o instanceof String)){
if (o == null || !(o instanceof String)) {
return false;
}
String str = o.toString().trim();
if (str.length() <= 1) {
return false;
}
char c0 = str.charAt(0);
char c1 = str.charAt(str.length() - 1);
if ((c0 == '[' && c1 == ']') || (c0 == '{' && c1 == '}')){
try {
new JSONParser(JSONParser.MODE_PERMISSIVE).parse(str);
return true;
} catch(Exception e){
return false;
}
if (isJsonArray(c0, c1) || isJsonObject(c0, c1)) {
return isValidJson(str);
}
return false;
}
private static boolean isJsonArray(char c0, char c1) {
return c0 == '[' && c1 == ']';
}
private static boolean isJsonObject(char c0, char c1) {
return c0 == '{' && c1 == '}';
}
private static boolean isValidJson(String str) {
try {
new JSONParser(JSONParser.MODE_PERMISSIVE).parse(str);
return true;
} catch (Exception e) {
return false;
}
}
//----------------------------------------------------

2
json-path/src/main/java/com/jayway/jsonpath/internal/function/PassthruPathFunction.java

@ -13,7 +13,7 @@ import java.util.List;
public class PassthruPathFunction implements PathFunction {
@Override
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
public Object aggregateAndInvoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
return model;
}
}

2
json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunction.java

@ -32,5 +32,5 @@ public interface PathFunction {
* @param parameters
* @return result
*/
Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters);
Object aggregateAndInvoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters);
}

2
json-path/src/main/java/com/jayway/jsonpath/internal/function/json/Append.java

@ -16,7 +16,7 @@ import java.util.List;
*/
public class Append implements PathFunction {
@Override
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
public Object aggregateAndInvoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
JsonProvider jsonProvider = ctx.configuration().jsonProvider();
if (parameters != null && parameters.size() > 0) {
for (Parameter param : parameters) {

2
json-path/src/main/java/com/jayway/jsonpath/internal/function/json/KeySetFunction.java

@ -14,7 +14,7 @@ import java.util.List;
public class KeySetFunction implements PathFunction {
@Override
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
public Object aggregateAndInvoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
if (ctx.configuration().jsonProvider().isMap(model)) {
return ctx.configuration().jsonProvider().getPropertyKeys(model);
}

30
json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/AbstractAggregation.java

@ -23,7 +23,7 @@ public abstract class AbstractAggregation implements PathFunction {
* @param value
* The numerical value to process next
*/
protected abstract void next(Number value);
protected abstract void processNumericValue(Number value);
/**
* Obtains the value generated via the series of next value calls
@ -31,31 +31,37 @@ public abstract class AbstractAggregation implements PathFunction {
* @return
* A numerical answer based on the input value provided
*/
protected abstract Number getValue();
protected abstract Number getAggregatedValue();
@Override
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
public Object aggregateAndInvoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
int count = 0;
if(ctx.configuration().jsonProvider().isArray(model)){
Iterable<?> objects = ctx.configuration().jsonProvider().toIterable(model);
for (Object obj : objects) {
if (obj instanceof Number) {
Number value = (Number) obj;
if (ctx.configuration().jsonProvider().isArray(model)) {
Iterable<?> arrayElements = ctx.configuration().jsonProvider().toIterable(model);
for (Object arrayElement : arrayElements) {
if (arrayElement instanceof Number) {
Number numericValue = (Number) arrayElement;
count++;
next(value);
processNumericValue(numericValue);
}
}
}
if (parameters != null) {
for (Number value : Parameter.toList(Number.class, ctx, parameters)) {
for (Number parameterValue : Parameter.toList(Number.class, ctx, parameters)) {
count++;
next(value);
processNumericValue(parameterValue);
}
}
if (count != 0) {
return getValue();
return getAggregatedValue();
}
throw new JsonPathException("Aggregation function attempted to calculate value using empty array");
}
}

4
json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/Average.java

@ -11,13 +11,13 @@ public class Average extends AbstractAggregation {
private Double count = 0d;
@Override
protected void next(Number value) {
protected void processNumericValue(Number value) {
count++;
summation += value.doubleValue();
}
@Override
protected Number getValue() {
protected Number getAggregatedValue() {
if (count != 0d) {
return summation / count;
}

4
json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/Max.java

@ -9,14 +9,14 @@ public class Max extends AbstractAggregation {
private Double max = Double.MIN_VALUE;
@Override
protected void next(Number value) {
protected void processNumericValue(Number value) {
if (max < value.doubleValue()) {
max = value.doubleValue();
}
}
@Override
protected Number getValue() {
protected Number getAggregatedValue() {
return max;
}
}

4
json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/Min.java

@ -9,14 +9,14 @@ public class Min extends AbstractAggregation {
private Double min = Double.MAX_VALUE;
@Override
protected void next(Number value) {
protected void processNumericValue(Number value) {
if (min > value.doubleValue()) {
min = value.doubleValue();
}
}
@Override
protected Number getValue() {
protected Number getAggregatedValue() {
return min;
}
}

4
json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/StandardDeviation.java

@ -11,14 +11,14 @@ public class StandardDeviation extends AbstractAggregation {
private Double count = 0d;
@Override
protected void next(Number value) {
protected void processNumericValue(Number value) {
sum += value.doubleValue();
sumSq += value.doubleValue() * value.doubleValue();
count++;
}
@Override
protected Number getValue() {
protected Number getAggregatedValue() {
return Math.sqrt(sumSq/count - sum*sum/count/count);
}
}

4
json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/Sum.java

@ -9,12 +9,12 @@ public class Sum extends AbstractAggregation {
private Double summation = 0d;
@Override
protected void next(Number value) {
protected void processNumericValue(Number value) {
summation += value.doubleValue();
}
@Override
protected Number getValue() {
protected Number getAggregatedValue() {
return summation;
}
}

2
json-path/src/main/java/com/jayway/jsonpath/internal/function/sequence/AbstractSequenceAggregation.java

@ -19,7 +19,7 @@ public abstract class AbstractSequenceAggregation implements PathFunction {
protected abstract int targetIndex(EvaluationContext ctx, List<Parameter> parameters);
@Override
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
public Object aggregateAndInvoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
if(ctx.configuration().jsonProvider().isArray(model)){
Iterable<?> objects = ctx.configuration().jsonProvider().toIterable(model);

2
json-path/src/main/java/com/jayway/jsonpath/internal/function/text/Concatenate.java

@ -14,7 +14,7 @@ import java.util.List;
*/
public class Concatenate implements PathFunction {
@Override
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
public Object aggregateAndInvoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
StringBuilder result = new StringBuilder();
if(ctx.configuration().jsonProvider().isArray(model)){
Iterable<?> objects = ctx.configuration().jsonProvider().toIterable(model);

2
json-path/src/main/java/com/jayway/jsonpath/internal/function/text/Length.java

@ -40,7 +40,7 @@ public class Length implements PathFunction {
* @return
*/
@Override
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
public Object aggregateAndInvoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
if (null != parameters && parameters.size() > 0) {
// Set the tail of the first parameter, when its not a function path parameter (which wouldn't make sense

2
json-path/src/main/java/com/jayway/jsonpath/internal/path/FunctionPathToken.java

@ -38,7 +38,7 @@ public class FunctionPathToken extends PathToken {
public void evaluate(String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx) {
PathFunction pathFunction = PathFunctionFactory.newFunction(functionName);
evaluateParameters(currentPath, parent, model, ctx);
Object result = pathFunction.invoke(currentPath, parent, model, ctx, functionParams);
Object result = pathFunction.aggregateAndInvoke(currentPath, parent, model, ctx, functionParams);
ctx.addResult(currentPath + "." + functionName, parent, result);
cleanWildcardPathToken();
if (!isLeaf()) {

2
json-path/src/main/java/com/jayway/jsonpath/internal/path/PathToken.java

@ -215,7 +215,7 @@ public abstract class PathToken {
}
public void invoke(PathFunction pathFunction, String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx) {
ctx.addResult(currentPath, parent, pathFunction.invoke(currentPath, parent, model, ctx, null));
ctx.addResult(currentPath, parent, pathFunction.aggregateAndInvoke(currentPath, parent, model, ctx, null));
}
public abstract void evaluate(String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx);

Loading…
Cancel
Save