diff --git a/src/main/java/org/springframework/data/couchbase/core/query/QueryCriteria.java b/src/main/java/org/springframework/data/couchbase/core/query/QueryCriteria.java index 241d11e9d..06571c68f 100644 --- a/src/main/java/org/springframework/data/couchbase/core/query/QueryCriteria.java +++ b/src/main/java/org/springframework/data/couchbase/core/query/QueryCriteria.java @@ -38,6 +38,7 @@ * @author Michael Nitschinger * @author Michael Reiche * @author Mauro Monti + * @author Shubham Mishra */ public class QueryCriteria implements QueryCriteriaDefinition { @@ -148,13 +149,7 @@ public QueryCriteria and(N1QLExpression key) { } public QueryCriteria and(QueryCriteria criteria) { - if (this.criteriaChain != null && !this.criteriaChain.contains(this)) { - throw new RuntimeException("criteria chain does not include this"); - } - if (this.criteriaChain == null) { - this.criteriaChain = new LinkedList<>(); - this.criteriaChain.add(this); - } + checkAndAddToCriteriaChain(); QueryCriteria newThis = wrap(this); QueryCriteria qc = wrap(criteria); newThis.criteriaChain.add(qc); @@ -189,13 +184,7 @@ public QueryCriteria or(N1QLExpression key) { } public QueryCriteria or(QueryCriteria criteria) { - if (this.criteriaChain != null && !this.criteriaChain.contains(this)) { - throw new RuntimeException("criteria chain does not include this"); - } - if (this.criteriaChain == null) { - this.criteriaChain = new LinkedList<>(); - this.criteriaChain.add(this); - } + checkAndAddToCriteriaChain(); QueryCriteria newThis = wrap(this); QueryCriteria qc = wrap(criteria); qc.criteriaChain = newThis.criteriaChain; @@ -719,4 +708,14 @@ public String toString() { sb.append("}"); return sb.toString(); } + + private void checkAndAddToCriteriaChain() { + if (this.criteriaChain != null && !this.criteriaChain.contains(this)) { + throw new RuntimeException("criteria chain does not include this"); + } + if (this.criteriaChain == null) { + this.criteriaChain = new LinkedList<>(); + this.criteriaChain.add(this); + } + } }