1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
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
17 package net.sf.ehcache.search.impl;
18
19 import java.util.Collections;
20 import java.util.List;
21
22 import net.sf.ehcache.search.Result;
23 import net.sf.ehcache.search.Results;
24 import net.sf.ehcache.search.SearchException;
25
26 /***
27 * Results implementation
28 *
29 * @author teck
30 */
31 public class ResultsImpl implements Results {
32
33 private final List<Result> results;
34 private final boolean hasKeys;
35 private final boolean hasAttributes;
36 private final boolean hasAggregators;
37 private final boolean hasValues;
38
39 /***
40 * Constructor
41 *
42 * @param results
43 * @param hasKeys
44 * @param hasAttributes
45 * @param hasAggregators
46 */
47 public ResultsImpl(List<Result> results, boolean hasKeys, boolean hasValues, boolean hasAttributes, boolean hasAggregators) {
48 this.hasKeys = hasKeys;
49 this.hasValues = hasValues;
50 this.hasAttributes = hasAttributes;
51 this.hasAggregators = hasAggregators;
52 this.results = Collections.unmodifiableList(results);
53 }
54
55 @Override
56 public String toString() {
57 return "Results(size=" + size() + ", hasKeys=" + hasKeys() + ", hasValues=" + hasValues()
58 + ", hasAttributes=" + hasAttributes() + ", hasAggregators=" + hasAggregators() + ")";
59 }
60
61 /***
62 * {@inheritDoc}
63 */
64 public void discard() {
65
66 }
67
68 /***
69 * {@inheritDoc}
70 */
71 public List<Result> all() throws SearchException {
72 return results;
73 }
74
75 /***
76 * {@inheritDoc}
77 */
78 public List<Result> range(int start, int length) throws SearchException {
79 if (start < 0) {
80 throw new IllegalArgumentException("start: " + start);
81 }
82
83 if (length < 0) {
84 throw new IllegalArgumentException("length: " + length);
85 }
86
87 int size = results.size();
88
89 if (start > size - 1 || length == 0) {
90 return Collections.EMPTY_LIST;
91 }
92
93 int end = start + length;
94
95 if (end > size) {
96 end = size;
97 }
98
99 return results.subList(start, end);
100 }
101
102 /***
103 * {@inheritDoc}
104 */
105 public int size() {
106 return results.size();
107 }
108
109 /***
110 * {@inheritDoc}
111 */
112 public boolean hasKeys() {
113 return hasKeys;
114 }
115
116 /***
117 * {@inheritDoc}
118 */
119 public boolean hasValues() {
120 return hasValues;
121 }
122
123 /***
124 * {@inheritDoc}
125 */
126 public boolean hasAttributes() {
127 return hasAttributes;
128 }
129
130 /***
131 * {@inheritDoc}
132 */
133 public boolean hasAggregators() {
134 return hasAggregators;
135 }
136
137 }