1 | /* |
2 | * Copyright 2012 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 | */ |
16 | package org.springframework.data.elasticsearch.core.query; |
17 | |
18 | |
19 | import org.springframework.data.domain.Pageable; |
20 | import org.springframework.util.Assert; |
21 | |
22 | public class CriteriaQuery extends AbstractQuery implements Query { |
23 | |
24 | private Criteria criteria; |
25 | private CriteriaQuery() { |
26 | } |
27 | |
28 | public CriteriaQuery(Criteria criteria) { |
29 | this(criteria, null); |
30 | } |
31 | |
32 | public CriteriaQuery(Criteria criteria, Pageable pageable) { |
33 | this.criteria = criteria; |
34 | this.pageable = pageable; |
35 | if (pageable != null) { |
36 | this.addSort(pageable.getSort()); |
37 | } |
38 | } |
39 | |
40 | public static final Query fromQuery(CriteriaQuery source) { |
41 | return fromQuery(source, new CriteriaQuery()); |
42 | } |
43 | |
44 | public static <T extends CriteriaQuery> T fromQuery(CriteriaQuery source, T destination) { |
45 | if (source == null || destination == null) { |
46 | return null; |
47 | } |
48 | |
49 | if (source.getCriteria() != null) { |
50 | destination.addCriteria(source.getCriteria()); |
51 | } |
52 | |
53 | if (source.getSort() != null) { |
54 | destination.addSort(source.getSort()); |
55 | } |
56 | |
57 | return destination; |
58 | } |
59 | |
60 | @SuppressWarnings("unchecked") |
61 | public final <T extends CriteriaQuery> T addCriteria(Criteria criteria) { |
62 | Assert.notNull(criteria, "Cannot add null criteria."); |
63 | if (this.criteria == null) { |
64 | this.criteria = criteria; |
65 | } else { |
66 | this.criteria.and(criteria); |
67 | } |
68 | return (T) this; |
69 | } |
70 | |
71 | public Criteria getCriteria() { |
72 | return this.criteria; |
73 | } |
74 | |
75 | } |