aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/SearchResult.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/SearchResult.java')
-rw-r--r--main/src/cgeo/geocaching/SearchResult.java109
1 files changed, 68 insertions, 41 deletions
diff --git a/main/src/cgeo/geocaching/SearchResult.java b/main/src/cgeo/geocaching/SearchResult.java
index e21717d..b0540f2 100644
--- a/main/src/cgeo/geocaching/SearchResult.java
+++ b/main/src/cgeo/geocaching/SearchResult.java
@@ -8,6 +8,7 @@ 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;
@@ -40,38 +41,49 @@ public class SearchResult implements Parcelable {
}
};
+ /**
+ * Build a new empty search result.
+ */
public SearchResult() {
- this((Set<String>) null);
+ this(new HashSet<String>());
}
- public SearchResult(SearchResult searchResult) {
- if (searchResult != null) {
- this.geocodes = new HashSet<String>(searchResult.geocodes);
- this.error = searchResult.error;
- this.url = searchResult.url;
- this.viewstates = searchResult.viewstates;
- this.setTotal(searchResult.getTotal());
- } else {
- this.geocodes = 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);
+ error = searchResult.error;
+ url = searchResult.url;
+ viewstates = searchResult.viewstates;
+ setTotal(searchResult.getTotal());
}
- public SearchResult(final Set<String> geocodes, final int total) {
- if (geocodes == null) {
- this.geocodes = new HashSet<String>();
- } else {
- this.geocodes = new HashSet<String>(geocodes.size());
- this.geocodes.addAll(geocodes);
- }
+ /**
+ * 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.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 == null ? 0 : geocodes.size());
+ this(geocodes, geocodes.size());
}
public SearchResult(final Parcel in) {
- ArrayList<String> list = new ArrayList<String>();
+ final ArrayList<String> list = new ArrayList<String>();
in.readStringList(list);
geocodes = new HashSet<String>(list);
error = (StatusCode) in.readSerializable();
@@ -84,14 +96,24 @@ public class SearchResult implements Parcelable {
setTotal(in.readInt());
}
- public SearchResult(cgCache cache) {
- this();
- addCache(cache);
+ /**
+ * Build a search result designating a single cache.
+ *
+ * @param cache the cache to include
+ */
+
+ public SearchResult(final Geocache cache) {
+ this(Collections.singletonList(cache));
}
- public SearchResult(Collection<cgCache> caches) {
+ /**
+ * 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 (cgCache cache : caches) {
+ for (final Geocache cache : caches) {
addCache(cache);
}
}
@@ -148,7 +170,7 @@ public class SearchResult implements Parcelable {
return;
}
- this.viewstates = viewstates;
+ System.arraycopy(viewstates, 0, this.viewstates, 0, viewstates.length);
}
public int getTotal() {
@@ -169,14 +191,13 @@ public class SearchResult implements Parcelable {
SearchResult result = new SearchResult(this);
result.geocodes.clear();
- final ArrayList<cgCache> cachesForVote = new ArrayList<cgCache>();
-
- final Set<cgCache> caches = cgeoapplication.getInstance().loadCaches(geocodes, LoadFlags.LOAD_CACHE_OR_DB);
- for (cgCache cache : caches) {
+ final ArrayList<Geocache> cachesForVote = new ArrayList<Geocache>();
+ final Set<Geocache> caches = cgData.loadCaches(geocodes, LoadFlags.LOAD_CACHE_OR_DB);
+ for (Geocache cache : caches) {
// Is there any reason to exclude the cache from the list?
final boolean excludeCache = (excludeDisabled && cache.isDisabled()) ||
- (excludeMine && (cache.isOwn() || cache.isFound())) ||
- (cacheType != CacheType.ALL && cacheType != cache.getType());
+ (excludeMine && (cache.isOwner() || cache.isFound())) ||
+ (!cacheType.contains(cache));
if (!excludeCache) {
result.addCache(cache);
cachesForVote.add(cache);
@@ -186,15 +207,12 @@ public class SearchResult implements Parcelable {
return result;
}
- public cgCache getFirstCacheFromResult(final EnumSet<LoadFlag> loadFlags) {
- if (geocodes != null && geocodes.size() >= 1) {
- return cgeoapplication.getInstance().loadCache((String) geocodes.toArray()[0], loadFlags);
- }
- return null;
+ public Geocache getFirstCacheFromResult(final EnumSet<LoadFlag> loadFlags) {
+ return CollectionUtils.isNotEmpty(geocodes) ? cgData.loadCache(geocodes.iterator().next(), loadFlags) : null;
}
- public Set<cgCache> getCachesFromSearchResult(final EnumSet<LoadFlag> loadFlags) {
- return cgeoapplication.getInstance().loadCaches(geocodes, loadFlags);
+ 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 */
@@ -211,13 +229,22 @@ public class SearchResult implements Parcelable {
}
/** Add the cache geocode to the search and store the cache in the CacheCache */
- public boolean addCache(final cgCache cache) {
+ public boolean addCache(final Geocache cache) {
addGeocode(cache.getGeocode());
- return cgeoapplication.getInstance().saveCache(cache, EnumSet.of(SaveFlag.SAVE_CACHE));
+ 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;
+ }
+
}