Browse Source

Merge pull request #56 from jochenberger/optimizations

more tiny performance improvements
pull/57/head
kallestenflo 10 years ago
parent
commit
4e0ac4c89a
  1. 38
      json-path/src/main/java/com/jayway/jsonpath/Filter.java

38
json-path/src/main/java/com/jayway/jsonpath/Filter.java

@ -1,26 +1,27 @@
package com.jayway.jsonpath; package com.jayway.jsonpath;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import static java.util.Arrays.asList;
/** /**
* *
*/ */
public class Filter implements Predicate { public class Filter implements Predicate {
protected List<Predicate> criteriaList = new ArrayList<Predicate>(); protected final List<Predicate> criteriaList;
private Filter() { private Filter() {
criteriaList = Collections.emptyList();
} }
private Filter(Predicate criteria) { private Filter(Predicate criteria) {
this.criteriaList.add(criteria); criteriaList = Collections.singletonList(criteria);
} }
private Filter(List<Predicate> criteriaList) { private Filter(List<Predicate> criteriaList) {
this.criteriaList = criteriaList; this.criteriaList = new ArrayList<Predicate>(criteriaList);
} }
@ -63,22 +64,27 @@ public class Filter implements Predicate {
} }
public Filter or(final Predicate other){ public Filter or(final Predicate other){
return new Filter(){ return new OrFilter(this, other);
@Override
public boolean apply(PredicateContext ctx) {
boolean a = Filter.this.apply(ctx);
return a || other.apply(ctx);
}
};
} }
public Filter and(final Predicate other){ public Filter and(final Predicate other){
return new Filter(){ return filter(Arrays.asList(this, other));
}
private static final class OrFilter extends Filter {
private final Predicate left;
private final Predicate right;
private OrFilter(Predicate left, Predicate right) {
this.left = left;
this.right = right;
}
@Override @Override
public boolean apply(PredicateContext ctx) { public boolean apply(PredicateContext ctx) {
boolean a = Filter.this.apply(ctx); boolean a = left.apply(ctx);
return a && other.apply(ctx); return a || right.apply(ctx);
} }
};
} }
} }

Loading…
Cancel
Save