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 | import org.springframework.data.domain.PageRequest; |
19 | import org.springframework.data.domain.Pageable; |
20 | import org.springframework.data.domain.Sort; |
21 | import org.springframework.util.Assert; |
22 | |
23 | /** |
24 | * AbstractQuery |
25 | * |
26 | */ |
27 | abstract class AbstractQuery implements Query{ |
28 | |
29 | private static final Pageable DEFAULT_PAGE = new PageRequest(0, DEFAULT_PAGE_SIZE); |
30 | |
31 | protected Pageable pageable = DEFAULT_PAGE; |
32 | protected Sort sort; |
33 | |
34 | @Override |
35 | public Sort getSort() { |
36 | return this.sort; |
37 | } |
38 | |
39 | @Override |
40 | public Pageable getPageable() { |
41 | return this.pageable; |
42 | } |
43 | |
44 | @Override |
45 | public final <T extends Query> T setPageable(Pageable pageable) { |
46 | Assert.notNull(pageable); |
47 | |
48 | this.pageable = pageable; |
49 | return (T) this.addSort(pageable.getSort()); |
50 | } |
51 | |
52 | @SuppressWarnings("unchecked") |
53 | public final <T extends Query> T addSort(Sort sort) { |
54 | if (sort == null) { |
55 | return (T) this; |
56 | } |
57 | |
58 | if (this.sort == null) { |
59 | this.sort = sort; |
60 | } else { |
61 | this.sort = this.sort.and(sort); |
62 | } |
63 | |
64 | return (T) this; |
65 | } |
66 | |
67 | } |