1 /*
2 * @(#)$Id: ResultSet.java 803 2010-05-13 15:55:01Z bpuype $
3 *
4 * Author : Ueli Kurmann, igesture@uelikurmann.ch
5 *
6 * Purpose : Class representing a set of result objects.
7 *
8 * -----------------------------------------------------------------------
9 *
10 * Revision Information:
11 *
12 * Date Who Reason
13 *
14 * Dec 26, 2006 ukurmann Initial Release
15 * Feb 27, 2007 bsigner Cleanup
16 *
17 * -----------------------------------------------------------------------
18 *
19 * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
20 *
21 * This software is the proprietary information of ETH Zurich.
22 * Use is subject to license terms.
23 *
24 */
25
26
27 package org.ximtec.igesture.core;
28
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.Comparator;
33 import java.util.List;
34 import java.util.SortedSet;
35 import java.util.TreeSet;
36
37 import org.sigtec.util.Constant;
38 import org.ximtec.igesture.Recogniser;
39
40
41 /**
42 * Class representing a set of result objects.
43 *
44 * @version 1.0, Dec 2006
45 * @author Ueli Kurmann, igesture@uelikurmann.ch
46 * @author Beat Signer, signer@inf.ethz.ch
47 */
48 public class ResultSet {
49
50 private static final String RESULT_SET = "ResultSet";
51
52 private SortedSet<Result> results;
53
54 private int maxSize;
55
56 private Gesture< ? > gesture;
57
58 private Recogniser source = null;
59
60 /**
61 * Constructs a new result set.
62 */
63 public ResultSet() {
64 this(Integer.MAX_VALUE);
65 }
66
67 /**
68 * Constructs a new result set.
69 * @param source The source that created this resultset
70 */
71 public ResultSet(Recogniser source)
72 {
73 this(Integer.MAX_VALUE);
74 setSource(source);
75 }
76
77 /**
78 * Constructs a new result set with a specific maximal size.
79 *
80 * @param maxSize the maximal size of the result set.
81 */
82 public ResultSet(int maxSize) {
83 results = Collections.synchronizedSortedSet(new TreeSet<Result>(
84 new Comparator<Result>() {
85
86 public int compare(Result result1, Result result2) {
87 return Double.compare(result2.getAccuracy(), result1
88 .getAccuracy());
89 }
90 }));
91
92 this.maxSize = maxSize;
93 }
94
95 /**
96 * Constructs a new result set with a specific maximal size.
97 *
98 * @param source the source that created this ResultSet
99 * @param maxSize the maximal size of the result set.
100 */
101 public ResultSet(Recogniser source, int maxSize)
102 {
103 this(maxSize);
104 setSource(source);
105 }
106
107
108 /**
109 * Adds a result object to the result set. The result set is always ordered by
110 * the accuracy value of the result objects.
111 *
112 * @param result the result object to be added.
113 */
114 public synchronized void addResult(Result result) {
115 results.add(result);
116 } // addResult
117
118
119 /**
120 * Adds a collection of result objects.
121 *
122 * @param results the result objects to be added.
123 */
124 public void addResults(Collection<Result> results) {
125 for (Result result : results) {
126 addResult(result);
127 }
128
129 }// addResults
130
131
132 /**
133 * Returns a list containing all result objects.
134 *
135 * @return the list of result objects.
136 */
137 public List<Result> getResults() {
138 return new ArrayList<Result>(results).subList(0, Math.min(maxSize, results
139 .size()));
140 } // getResults
141
142
143 /**
144 * Returns the result for a given index position.
145 *
146 * @param index the index of the result object to be returned.
147 * @return the result object for the given index position.
148 */
149 public Result getResult(int index) {
150 return new ArrayList<Result>(results).get(index);
151 } // getResult
152
153
154 /**
155 * Returns the first result object which is also the result object with the
156 * highest recognition accuracy.
157 *
158 * @return the first result object or null if the result set does not contain
159 * any result objects.
160 */
161 public Result getResult() {
162 return !results.isEmpty() ? getResult(0) : null;
163 } // getResult
164
165
166 /**
167 * Returns true if the result set contains the given gesture class.
168 *
169 * @param gestureClass the gesture class to be tested for.
170 * @return true if the result set contains the specified gesture class.
171 */
172 public boolean contains(GestureClass gestureClass) {
173 for (final Result result : results) {
174
175 if (result.getGestureClass().getName().equals(gestureClass.getName())) {
176 return true;
177 }
178
179 }
180
181 return false;
182 } // contains
183
184
185 /**
186 * Returns the number of results.
187 *
188 * @return the number of results in the result set.
189 */
190 public int size() {
191 return results.size() > maxSize ? maxSize : results.size();
192 } // size
193
194
195 /**
196 * Return true if the result set is empty.
197 *
198 * @return true if the result set is empty.
199 */
200 public boolean isEmpty() {
201 return results.isEmpty();
202 } // isEmpty
203
204
205 /**
206 * Sets the gesture to be recognised.
207 *
208 * @param gesture the gesture to be recognised.
209 */
210 public void setGesture(Gesture< ? > gesture) {
211 this.gesture = gesture;
212 } // setGesture
213
214
215 /**
216 * Returns the gesture to be recognised.
217 *
218 * @return the gesture to be recognised.
219 */
220 public Gesture< ? > getGesture() {
221 return gesture;
222 } // getGesture
223
224
225 @Override
226 public String toString() {
227 final StringBuilder sb = new StringBuilder();
228 sb.append(Constant.OPEN_ANGULAR_BRACKET);
229 sb.append(RESULT_SET);
230
231 for (final Result r : this.getResults()) {
232 sb.append(Constant.DOUBLE_BLANK + Constant.OPEN_ANGULAR_BRACKET
233 + r.getGestureClassName() + Constant.COMMA_BLANK
234 + r.getAccuracy() + Constant.CLOSE_ANGULAR_BRACKET);
235 }
236
237 sb.append(Constant.CLOSE_ANGULAR_BRACKET);
238 return sb.toString();
239 } // toString
240
241
242 /**
243 * Get the source Recogniser that created this ResultSet
244 * @return the source
245 */
246 public Recogniser getSource() {
247 return source;
248 }
249
250
251 /**
252 * Set the source Recogniser that created this ResultSet.
253 * @param source the source to set
254 */
255 public void setSource(Recogniser source) {
256 this.source = source;
257 }
258
259 }