| 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 | */ |
| 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 | import java.util.ArrayList; |
| 24 | import java.util.List; |
| 25 | |
| 26 | import static org.apache.commons.collections.CollectionUtils.addAll; |
| 27 | |
| 28 | /** |
| 29 | * AbstractQuery |
| 30 | * |
| 31 | * @author Rizwan Idrees |
| 32 | * @author Mohsin Husen |
| 33 | */ |
| 34 | abstract class AbstractQuery implements Query{ |
| 35 | |
| 36 | private static final Pageable DEFAULT_PAGE = new PageRequest(0, DEFAULT_PAGE_SIZE); |
| 37 | |
| 38 | protected Pageable pageable = DEFAULT_PAGE; |
| 39 | protected Sort sort; |
| 40 | protected List<String> indices = new ArrayList<String>(); |
| 41 | protected List<String> types = new ArrayList<String>(); |
| 42 | protected List<String> fields = new ArrayList<String>(); |
| 43 | |
| 44 | @Override |
| 45 | public Sort getSort() { |
| 46 | return this.sort; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public Pageable getPageable() { |
| 51 | return this.pageable; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public final <T extends Query> T setPageable(Pageable pageable) { |
| 56 | Assert.notNull(pageable); |
| 57 | this.pageable = pageable; |
| 58 | return (T) this.addSort(pageable.getSort()); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public void addFields(String...fields) { |
| 63 | addAll(this.fields, fields); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public List<String> getFields() { |
| 68 | return fields; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public List<String> getIndices() { |
| 73 | return indices; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void addIndices(String... indices) { |
| 78 | addAll(this.indices,indices); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void addTypes(String... types) { |
| 83 | addAll(this.types,types); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public List<String> getTypes() { |
| 88 | return types; |
| 89 | } |
| 90 | |
| 91 | @SuppressWarnings("unchecked") |
| 92 | public final <T extends Query> T addSort(Sort sort) { |
| 93 | if (sort == null) { |
| 94 | return (T) this; |
| 95 | } |
| 96 | |
| 97 | if (this.sort == null) { |
| 98 | this.sort = sort; |
| 99 | } else { |
| 100 | this.sort = this.sort.and(sort); |
| 101 | } |
| 102 | |
| 103 | return (T) this; |
| 104 | } |
| 105 | } |