1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
package cgeo.geocaching;
import cgeo.geocaching.connector.gc.Login;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.enumerations.LoadFlags.LoadFlag;
import cgeo.geocaching.enumerations.LoadFlags.SaveFlag;
import cgeo.geocaching.enumerations.StatusCode;
import cgeo.geocaching.gcvote.GCVote;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;
public class SearchResult implements Parcelable {
final private Set<String> geocodes;
final private Set<String> filteredGeocodes;
private StatusCode error = null;
private String url = "";
public String[] viewstates = null;
private int totalCnt = 0;
final public static Parcelable.Creator<SearchResult> CREATOR = new Parcelable.Creator<SearchResult>() {
@Override
public SearchResult createFromParcel(Parcel in) {
return new SearchResult(in);
}
@Override
public SearchResult[] newArray(int size) {
return new SearchResult[size];
}
};
/**
* Build a new empty search result.
*/
public SearchResult() {
this(new HashSet<String>());
}
/**
* Copy a search result, for example to apply different filters on it.
*
* @param searchResult the original search result, which cannot be null
*/
public SearchResult(final SearchResult searchResult) {
geocodes = new HashSet<String>(searchResult.geocodes);
filteredGeocodes = new HashSet<String>(searchResult.filteredGeocodes);
error = searchResult.error;
url = searchResult.url;
viewstates = searchResult.viewstates;
setTotal(searchResult.getTotal());
}
/**
* Build a search result from an existing collection of geocodes.
*
* @param geocodes a non-null collection of geocodes
* @param total the total number of geocodes (FIXME: what is the meaning of this number wrt to geocodes.size()?)
*/
public SearchResult(final Collection<String> geocodes, final int total) {
this.geocodes = new HashSet<String>(geocodes.size());
this.geocodes.addAll(geocodes);
this.filteredGeocodes = new HashSet<String>();
this.setTotal(total);
}
/**
* Build a search result from an existing collection of geocodes.
*
* @param geocodes a non-null set of geocodes
*/
public SearchResult(final Set<String> geocodes) {
this(geocodes, geocodes.size());
}
public SearchResult(final Parcel in) {
final ArrayList<String> list = new ArrayList<String>();
in.readStringList(list);
geocodes = new HashSet<String>(list);
final ArrayList<String> filteredList = new ArrayList<String>();
in.readStringList(filteredList);
filteredGeocodes = new HashSet<String>(filteredList);
error = (StatusCode) in.readSerializable();
url = in.readString();
final int length = in.readInt();
if (length >= 0) {
viewstates = new String[length];
in.readStringArray(viewstates);
}
setTotal(in.readInt());
}
/**
* Build a search result designating a single cache.
*
* @param cache the cache to include
*/
public SearchResult(final Geocache cache) {
this(Collections.singletonList(cache));
}
/**
* Build a search result from a collection of caches.
*
* @param caches the non-null collection of caches to include
*/
public SearchResult(final Collection<Geocache> caches) {
this();
for (final Geocache cache : caches) {
addAndPutInCache(cache);
}
}
@Override
public void writeToParcel(final Parcel out, final int flags) {
out.writeStringArray(geocodes.toArray(new String[geocodes.size()]));
out.writeStringArray(filteredGeocodes.toArray(new String[filteredGeocodes.size()]));
out.writeSerializable(error);
out.writeString(url);
if (viewstates == null) {
out.writeInt(-1);
} else {
out.writeInt(viewstates.length);
out.writeStringArray(viewstates);
}
out.writeInt(getTotal());
}
@Override
public int describeContents() {
return 0;
}
public Set<String> getGeocodes() {
return Collections.unmodifiableSet(geocodes);
}
public int getCount() {
return geocodes.size();
}
public StatusCode getError() {
return error;
}
public void setError(final StatusCode error) {
this.error = error;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String[] getViewstates() {
return viewstates;
}
public void setViewstates(String[] viewstates) {
if (Login.isEmpty(viewstates)) {
return;
}
System.arraycopy(viewstates, 0, this.viewstates, 0, viewstates.length);
}
public int getTotal() {
return totalCnt;
}
public void setTotal(int totalCnt) {
this.totalCnt = totalCnt;
}
/**
* @param excludeDisabled
* @param excludeMine
* @param cacheType
* @return
*/
public SearchResult filterSearchResults(final boolean excludeDisabled, final boolean excludeMine, final CacheType cacheType) {
SearchResult result = new SearchResult(this);
result.geocodes.clear();
final ArrayList<Geocache> cachesForVote = new ArrayList<Geocache>();
final Set<Geocache> caches = cgData.loadCaches(geocodes, LoadFlags.LOAD_CACHE_OR_DB);
int excluded = 0;
for (Geocache cache : caches) {
// Is there any reason to exclude the cache from the list?
final boolean excludeCache = (excludeDisabled && cache.isDisabled()) ||
(excludeMine && (cache.isOwner() || cache.isFound())) ||
(!cacheType.contains(cache));
if (excludeCache) {
excluded++;
} else {
result.addAndPutInCache(cache);
cachesForVote.add(cache);
}
}
// decrease maximum number of caches by filtered ones
result.setTotal(result.getTotal() - excluded);
GCVote.loadRatings(cachesForVote);
return result;
}
public Geocache getFirstCacheFromResult(final EnumSet<LoadFlag> loadFlags) {
return CollectionUtils.isNotEmpty(geocodes) ? cgData.loadCache(geocodes.iterator().next(), loadFlags) : null;
}
public Set<Geocache> getCachesFromSearchResult(final EnumSet<LoadFlag> loadFlags) {
return cgData.loadCaches(geocodes, loadFlags);
}
/** Add the geocode to the search. No cache is loaded into the CacheCache */
public boolean addGeocode(final String geocode) {
if (StringUtils.isBlank(geocode)) {
throw new IllegalArgumentException("geocode must not be blank");
}
return geocodes.add(geocode);
}
/** Add the geocodes to the search. No caches are loaded into the CacheCache */
public boolean addGeocodes(Set<String> geocodes) {
return this.geocodes.addAll(geocodes);
}
/** Add the cache geocode to the search and store the cache in the CacheCache */
public boolean addAndPutInCache(final Geocache cache) {
addGeocode(cache.getGeocode());
return cgData.saveCache(cache, EnumSet.of(SaveFlag.SAVE_CACHE));
}
public boolean isEmpty() {
return geocodes.isEmpty();
}
public boolean hasUnsavedCaches() {
for (final String geocode : getGeocodes()) {
if (!cgData.isOffline(geocode, null)) {
return true;
}
}
return false;
}
public void addFilteredGeocodes(Set<String> cachedMissingFromSearch) {
filteredGeocodes.addAll(cachedMissingFromSearch);
}
public Set<String> getFilteredGeocodes() {
return Collections.unmodifiableSet(filteredGeocodes);
}
public void addSearchResult(SearchResult other) {
if (other != null) {
addGeocodes(other.geocodes);
addFilteredGeocodes(other.filteredGeocodes);
}
}
}
|