EMMA Coverage Report (generated Tue Mar 05 16:36:55 GMT 2013)
[all classes][org.springframework.data.elasticsearch.core.query]

COVERAGE SUMMARY FOR SOURCE FILE [Criteria.java]

nameclass, %method, %block, %line, %
Criteria.java100% (4/4)78%  (38/49)77%  (460/596)81%  (82.9/103)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class Criteria$OrCriteria100% (1/1)33%  (2/6)30%  (7/23)27%  (3/11)
Criteria$OrCriteria (): void 0%   (0/1)0%   (0/3)0%   (0/2)
Criteria$OrCriteria (Field): void 0%   (0/1)0%   (0/4)0%   (0/2)
Criteria$OrCriteria (List, String): void 0%   (0/1)0%   (0/5)0%   (0/2)
Criteria$OrCriteria (String): void 0%   (0/1)0%   (0/4)0%   (0/2)
Criteria$OrCriteria (List, Field): void 100% (1/1)100% (5/5)100% (2/2)
getConjunctionOperator (): String 100% (1/1)100% (2/2)100% (1/1)
     
class Criteria100% (1/1)83%  (30/36)75%  (345/460)86%  (72/84)
and (Criteria []): Criteria 0%   (0/1)0%   (0/8)0%   (0/2)
and (Field): Criteria 0%   (0/1)0%   (0/7)0%   (0/1)
fuzzy (String): Criteria 0%   (0/1)0%   (0/11)0%   (0/2)
isAnd (): boolean 0%   (0/1)0%   (0/8)0%   (0/1)
where (Field): Criteria 0%   (0/1)0%   (0/5)0%   (0/1)
where (String): Criteria 0%   (0/1)0%   (0/6)0%   (0/1)
assertNoBlankInWildcardedQuery (String, boolean, boolean): void 100% (1/1)14%  (5/35)67%  (2/3)
in (Object []): Criteria 100% (1/1)36%  (17/47)67%  (2/3)
boost (float): Criteria 100% (1/1)64%  (9/14)75%  (3/4)
between (Object, Object): Criteria 100% (1/1)83%  (24/29)75%  (3/4)
Criteria (): void 100% (1/1)100% (20/20)100% (6/6)
Criteria (Field): void 100% (1/1)100% (35/35)100% (10/10)
Criteria (List, Field): void 100% (1/1)100% (43/43)100% (12/12)
Criteria (List, String): void 100% (1/1)100% (8/8)100% (2/2)
Criteria (String): void 100% (1/1)100% (7/7)100% (2/2)
and (Criteria): Criteria 100% (1/1)100% (7/7)100% (2/2)
and (String): Criteria 100% (1/1)100% (7/7)100% (1/1)
contains (String): Criteria 100% (1/1)100% (16/16)100% (3/3)
endsWith (String): Criteria 100% (1/1)100% (16/16)100% (3/3)
expression (String): Criteria 100% (1/1)100% (11/11)100% (2/2)
getBoost (): float 100% (1/1)100% (3/3)100% (1/1)
getConjunctionOperator (): String 100% (1/1)100% (2/2)100% (1/1)
getCriteriaChain (): List 100% (1/1)100% (4/4)100% (1/1)
getCriteriaEntries (): Set 100% (1/1)100% (4/4)100% (1/1)
getField (): Field 100% (1/1)100% (3/3)100% (1/1)
greaterThanEqual (Object): Criteria 100% (1/1)100% (7/7)100% (2/2)
in (Iterable): Criteria 100% (1/1)100% (14/14)100% (3/3)
is (Object): Criteria 100% (1/1)100% (11/11)100% (2/2)
isNegating (): boolean 100% (1/1)100% (3/3)100% (1/1)
isOr (): boolean 100% (1/1)100% (8/8)100% (1/1)
lessThanEqual (Object): Criteria 100% (1/1)100% (7/7)100% (2/2)
not (): Criteria 100% (1/1)100% (5/5)100% (2/2)
or (Criteria): Criteria 100% (1/1)100% (19/19)100% (4/4)
or (Field): Criteria 100% (1/1)100% (7/7)100% (1/1)
or (String): Criteria 100% (1/1)100% (7/7)100% (1/1)
startsWith (String): Criteria 100% (1/1)100% (16/16)100% (3/3)
     
class Criteria$OperationKey100% (1/1)75%  (3/4)95%  (93/98)97%  (1.9/2)
valueOf (String): Criteria$OperationKey 0%   (0/1)0%   (0/5)0%   (0/1)
<static initializer> 100% (1/1)100% (84/84)100% (2/2)
Criteria$OperationKey (String, int): void 100% (1/1)100% (5/5)100% (1/1)
values (): Criteria$OperationKey [] 100% (1/1)100% (4/4)100% (1/1)
     
class Criteria$CriteriaEntry100% (1/1)100% (3/3)100% (15/15)100% (6/6)
Criteria$CriteriaEntry (Criteria$OperationKey, Object): void 100% (1/1)100% (9/9)100% (4/4)
getKey (): Criteria$OperationKey 100% (1/1)100% (3/3)100% (1/1)
getValue (): Object 100% (1/1)100% (3/3)100% (1/1)

1/*
2 * Copyright 2013 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.springframework.data.elasticsearch.core.query;
17 
18import java.util.ArrayList;
19import java.util.Arrays;
20import java.util.Collection;
21import java.util.Collections;
22import java.util.LinkedHashSet;
23import java.util.List;
24import java.util.Set;
25 
26import org.apache.commons.lang.StringUtils;
27import org.springframework.dao.InvalidDataAccessApiUsageException;
28import org.springframework.util.Assert;
29 
30/**
31 * Criteria is the central class when constructing queries. It follows more or less a fluent API style, which allows to
32 * easily chain together multiple criteria.
33 *
34 * @author Rizwan Idrees
35 * @author Mohsin Husen
36 */
37public class Criteria {
38 
39        public static final String WILDCARD = "*";
40        public static final String CRITERIA_VALUE_SEPERATOR = " ";
41 
42        private static final String OR_OPERATOR = " OR ";
43        private static final String AND_OPERATOR = " AND ";
44 
45        private Field field;
46        private float boost = Float.NaN;
47        private boolean negating = false;
48 
49        private List<Criteria> criteriaChain = new ArrayList<Criteria>(1);
50 
51        private Set<CriteriaEntry> criteria = new LinkedHashSet<CriteriaEntry>();
52 
53        public Criteria() {
54        }
55 
56        /**
57         * Creates a new CriterSimpleFieldia for the Filed with provided name
58         * 
59         * @param fieldname
60         */
61        public Criteria(String fieldname) {
62                this(new SimpleField(fieldname));
63        }
64 
65        /**
66         * Creates a new Criteria for the given field
67         * 
68         * @param field
69         */
70        public Criteria(Field field) {
71                Assert.notNull(field, "Field for criteria must not be null");
72                Assert.hasText(field.getName(), "Field.name for criteria must not be null/empty");
73 
74                this.criteriaChain.add(this);
75                this.field = field;
76        }
77 
78        protected Criteria(List<Criteria> criteriaChain, String fieldname) {
79                this(criteriaChain, new SimpleField(fieldname));
80        }
81 
82        protected Criteria(List<Criteria> criteriaChain, Field field) {
83                Assert.notNull(criteriaChain, "CriteriaChain must not be null");
84                Assert.notNull(field, "Field for criteria must not be null");
85                Assert.hasText(field.getName(), "Field.name for criteria must not be null/empty");
86 
87                this.criteriaChain.addAll(criteriaChain);
88                this.criteriaChain.add(this);
89                this.field = field;
90        }
91 
92        /**
93         * Static factory method to create a new Criteria for field with given name
94         * 
95         * @param field
96         * @return
97         */
98        public static Criteria where(String field) {
99                return where(new SimpleField(field));
100        }
101 
102        /**
103         * Static factory method to create a new Criteria for provided field
104         * 
105         * @param field
106         * @return
107         */
108        public static Criteria where(Field field) {
109                return new Criteria(field);
110        }
111 
112        /**
113         * Chain using {@code AND}
114         * 
115         * @param field
116         * @return
117         */
118        public Criteria and(Field field) {
119                return new Criteria(this.criteriaChain, field);
120        }
121 
122        /**
123         * Chain using {@code AND}
124         * 
125         * @param fieldName
126         * @return
127         */
128        public Criteria and(String fieldName) {
129                return new Criteria(this.criteriaChain, fieldName);
130        }
131 
132        /**
133         * Chain using {@code AND}
134         * 
135         * @param criteria
136         * @return
137         */
138        public Criteria and(Criteria criteria) {
139                this.criteriaChain.add(criteria);
140                return this;
141        }
142 
143        /**
144         * Chain using {@code AND}
145         * 
146         * @param criterias
147         * @return
148         */
149        public Criteria and(Criteria... criterias) {
150                this.criteriaChain.addAll(Arrays.asList(criterias));
151                return this;
152        }
153 
154        /**
155         * Chain using {@code OR}
156         * 
157         * @param field
158         * @return
159         */
160        public Criteria or(Field field) {
161                return new OrCriteria(this.criteriaChain, field);
162        }
163 
164        /**
165         * Chain using {@code OR}
166         * 
167         * @param criteria
168         * @return
169         */
170        public Criteria or(Criteria criteria) {
171                Assert.notNull(criteria, "Cannot chain 'null' criteria.");
172 
173                Criteria orConnectedCritiera = new OrCriteria(this.criteriaChain, criteria.getField());
174                orConnectedCritiera.criteria.addAll(criteria.criteria);
175                return orConnectedCritiera;
176        }
177 
178        /**
179         * Chain using {@code OR}
180         * 
181         * @param fieldName
182         * @return
183         */
184        public Criteria or(String fieldName) {
185                return or(new SimpleField(fieldName));
186        }
187 
188        /**
189         * Crates new CriteriaEntry without any wildcards
190         * 
191         * @param o
192         * @return
193         */
194        public Criteria is(Object o) {
195                criteria.add(new CriteriaEntry(OperationKey.EQUALS, o));
196                return this;
197        }
198 
199        /**
200         * Crates new CriteriaEntry with leading and trailing wildcards <br/>
201         * <strong>NOTE: </strong> mind your schema as leading wildcards may not be supported and/or execution might be slow.
202         * 
203         * @param s
204         * @return
205         */
206        public Criteria contains(String s) {
207                assertNoBlankInWildcardedQuery(s, true, true);
208                criteria.add(new CriteriaEntry(OperationKey.CONTAINS, s));
209                return this;
210        }
211 
212        /**
213         * Crates new CriteriaEntry with trailing wildcard
214         * 
215         * @param s
216         * @return
217         */
218        public Criteria startsWith(String s) {
219                assertNoBlankInWildcardedQuery(s, true, false);
220                criteria.add(new CriteriaEntry(OperationKey.STARTS_WITH, s));
221                return this;
222        }
223 
224        /**
225         * Crates new CriteriaEntry with leading wildcard <br />
226         * <strong>NOTE: </strong> mind your schema and execution times as leading wildcards may not be supported.
227         * 
228         * @param s
229         * @return
230         */
231        public Criteria endsWith(String s) {
232                assertNoBlankInWildcardedQuery(s, false, true);
233                criteria.add(new CriteriaEntry(OperationKey.ENDS_WITH, s));
234                return this;
235        }
236 
237        /**
238         * Crates new CriteriaEntry with trailing -
239         * 
240         * @return
241         */
242        public Criteria not() {
243                this.negating = true;
244                return this;
245        }
246 
247        /**
248         * Crates new CriteriaEntry with trailing ~
249         * 
250         * @param s
251         * @return
252         */
253        public Criteria fuzzy(String s) {
254        criteria.add(new CriteriaEntry(OperationKey.FUZZY, s));
255        return this;
256        }
257 
258 
259        /**
260         * Crates new CriteriaEntry allowing native elasticsearch expressions
261         * 
262         * @param s
263         * @return
264         */
265        public Criteria expression(String s) {
266                criteria.add(new CriteriaEntry(OperationKey.EXPRESSION, s));
267                return this;
268        }
269 
270        /**
271         * Boost positive hit with given factor. eg. ^2.3
272         * 
273         * @param boost
274         * @return
275         */
276        public Criteria boost(float boost) {
277                if (boost < 0) {
278                        throw new InvalidDataAccessApiUsageException("Boost must not be negative.");
279                }
280                this.boost = boost;
281                return this;
282        }
283 
284        /**
285         * Crates new CriteriaEntry for {@code RANGE [lowerBound TO upperBound]}
286         * 
287         * @param lowerBound
288         * @param upperBound
289         * @return
290         */
291        public Criteria between(Object lowerBound, Object upperBound) {
292                if (lowerBound == null && upperBound == null) {
293                        throw new InvalidDataAccessApiUsageException("Range [* TO *] is not allowed");
294                }
295 
296                criteria.add(new CriteriaEntry(OperationKey.BETWEEN, new Object[] { lowerBound, upperBound }));
297                return this;
298        }
299 
300        /**
301         * Crates new CriteriaEntry for {@code RANGE [* TO upperBound]}
302         * 
303         * @param upperBound
304         * @return
305         */
306        public Criteria lessThanEqual(Object upperBound) {
307                between(null, upperBound);
308                return this;
309        }
310 
311        /**
312         * Crates new CriteriaEntry for {@code RANGE [lowerBound TO *]}
313         * 
314         * @param lowerBound
315         * @return
316         */
317        public Criteria greaterThanEqual(Object lowerBound) {
318                between(lowerBound, null);
319                return this;
320        }
321 
322        /**
323         * Crates new CriteriaEntry for multiple values {@code (arg0 arg1 arg2 ...)}
324         * 
325         * @param values
326         * @return
327         */
328        public Criteria in(Object... values) {
329                if (values.length == 0 || (values.length > 1 && values[1] instanceof Collection)) {
330                        throw new InvalidDataAccessApiUsageException("At least one element "
331                                        + (values.length > 0 ? ("of argument of type " + values[1].getClass().getName()) : "")
332                                        + " has to be present.");
333                }
334                return in(Arrays.asList(values));
335        }
336 
337        /**
338         * Crates new CriteriaEntry for multiple values {@code (arg0 arg1 arg2 ...)}
339         * 
340         * @param values the collection containing the values to match against
341         * @return
342         */
343        public Criteria in(Iterable<?> values) {
344                Assert.notNull(values, "Collection of 'in' values must not be null");
345        criteria.add(new CriteriaEntry(OperationKey.IN, values));
346                return this;
347        }
348 
349 
350        private void assertNoBlankInWildcardedQuery(String searchString, boolean leadingWildcard, boolean trailingWildcard) {
351                if (StringUtils.contains(searchString, CRITERIA_VALUE_SEPERATOR)) {
352                        throw new InvalidDataAccessApiUsageException("Cannot constructQuery '" + (leadingWildcard ? "*" : "") + "\""
353                                        + searchString + "\"" + (trailingWildcard ? "*" : "") + "'. Use epxression or mulitple clauses instead.");
354                }
355        }
356 
357        /**
358         * Field targeted by this Criteria
359         * 
360         * @return
361         */
362        public Field getField() {
363                return this.field;
364        }
365 
366        public Set<CriteriaEntry> getCriteriaEntries() {
367                return Collections.unmodifiableSet(this.criteria);
368        }
369 
370        /**
371         * Conjunction to be used with this criteria (AND | OR)
372         * 
373         * @return
374         */
375        public String getConjunctionOperator() {
376                return AND_OPERATOR;
377        }
378 
379        public List<Criteria> getCriteriaChain() {
380                return Collections.unmodifiableList(this.criteriaChain);
381        }
382 
383        public boolean isNegating() {
384                return this.negating;
385        }
386 
387    public boolean isAnd(){
388        return AND_OPERATOR == getConjunctionOperator();
389    }
390 
391    public boolean isOr(){
392        return OR_OPERATOR == getConjunctionOperator();
393    }
394 
395        public float getBoost() {
396                return this.boost;
397        }
398 
399        static class OrCriteria extends Criteria {
400 
401                public OrCriteria() {
402                        super();
403                }
404 
405                public OrCriteria(Field field) {
406                        super(field);
407                }
408 
409                public OrCriteria(List<Criteria> criteriaChain, Field field) {
410                        super(criteriaChain, field);
411                }
412 
413                public OrCriteria(List<Criteria> criteriaChain, String fieldname) {
414                        super(criteriaChain, fieldname);
415                }
416 
417                public OrCriteria(String fieldname) {
418                        super(fieldname);
419                }
420 
421                @Override
422                public String getConjunctionOperator() {
423                        return OR_OPERATOR;
424                }
425 
426        }
427 
428        public enum OperationKey {
429                EQUALS, CONTAINS, STARTS_WITH, ENDS_WITH, EXPRESSION, BETWEEN, FUZZY, IN;
430        }
431 
432        public static class CriteriaEntry {
433 
434                private OperationKey  key;
435                private Object value;
436 
437                CriteriaEntry(OperationKey key, Object value) {
438                        this.key = key;
439                        this.value = value;
440                }
441 
442                public OperationKey getKey() {
443                        return key;
444                }
445 
446                public Object getValue() {
447                        return value;
448                }
449 
450        }
451 
452}

[all classes][org.springframework.data.elasticsearch.core.query]
EMMA 2.0.5312 (C) Vladimir Roubtsov