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.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import net.sf.ehcache.search.Attribute;
25 import net.sf.ehcache.search.Result;
26 import net.sf.ehcache.search.SearchException;
27 import net.sf.ehcache.store.StoreQuery;
28
29 /***
30 * Base result implementation
31 *
32 * @author teck
33 */
34 public abstract class BaseResult implements Result {
35
36 private final StoreQuery query;
37 private volatile List<Object> aggregateResults = Collections.emptyList();
38
39 /***
40 * Constructor
41 *
42 * @param query
43 */
44 public BaseResult(StoreQuery query) {
45 this.query = query;
46 }
47
48 /***
49 * Set the aggregate results for this row
50 *
51 * @param aggregateResults
52 */
53 public void setAggregateResults(List<Object> aggregateResults) {
54 this.aggregateResults = Collections.unmodifiableList(aggregateResults);
55 }
56
57 /***
58 * {@inheritDoc}
59 */
60 public Object getKey() {
61 if (query.requestsKeys()) {
62 return basicGetKey();
63 }
64
65 throw new SearchException("keys not included in query. Use includeKeys() to add keys to results.");
66 }
67
68 /***
69 * Get the actual key value
70 *
71 * @return key
72 */
73 protected abstract Object basicGetKey();
74
75 /***
76 * {@inheritDoc}
77 */
78 public List<Object> getAggregatorResults() throws SearchException {
79 if (this.aggregateResults.isEmpty()) {
80 throw new SearchException("No aggregators present in query");
81 }
82 return this.aggregateResults;
83 }
84
85 /***
86 * {@inheritDoc}
87 */
88 public Object getValue() throws SearchException {
89 if (query.requestsValues()) {
90 return basicGetValue();
91 }
92
93 throw new SearchException("values not included in query. Use includeValues() to add values to results.");
94 }
95
96 /***
97 * Get the actual value
98 *
99 * @return value
100 */
101 protected abstract Object basicGetValue();
102
103 /***
104 * {@inheritDoc}
105 */
106 public <T> T getAttribute(Attribute<T> attribute) {
107 String name = attribute.getAttributeName();
108
109 if (!query.requestedAttributes().contains(attribute)) {
110 throw new SearchException("Attribute [" + name + "] not included in query");
111 }
112
113 return (T) basicGetAttribute(name);
114 }
115
116
117 /***
118 * Get the actual attribute value
119 *
120 * @return attribute
121 */
122 protected abstract Object basicGetAttribute(String name);
123
124 @Override
125 public String toString() {
126 StringBuilder sb = new StringBuilder("Result(");
127
128 if (query.requestsKeys()) {
129 sb.append("key=");
130 sb.append(getKey());
131 } else {
132 sb.append("[no key]");
133 }
134
135 sb.append(", ");
136 if (query.requestsValues()) {
137 sb.append("value=");
138 sb.append(getValue());
139 } else {
140 sb.append("[no value]");
141 }
142
143 sb.append(", ");
144 if (!query.requestedAttributes().isEmpty()) {
145 Map<String, String> attrs = new HashMap<String, String>();
146 for (Attribute a : query.requestedAttributes()) {
147 attrs.put(a.getAttributeName(), String.valueOf(getAttribute(a)));
148 }
149
150 sb.append("attributes=" + attrs);
151 } else {
152 sb.append("[no attributes]");
153 }
154
155 sb.append(", ");
156 if (!aggregateResults.isEmpty()) {
157 sb.append("aggregateResults=" + getAggregatorResults());
158 } else {
159 sb.append("[no aggregateResults]");
160 }
161
162 sb.append(")");
163 return sb.toString();
164 }
165
166
167 }