diff options
Diffstat (limited to 'main/src/cgeo/geocaching/SearchResult.java')
| -rw-r--r-- | main/src/cgeo/geocaching/SearchResult.java | 91 |
1 files changed, 60 insertions, 31 deletions
diff --git a/main/src/cgeo/geocaching/SearchResult.java b/main/src/cgeo/geocaching/SearchResult.java index e21717d..d1b1df6 100644 --- a/main/src/cgeo/geocaching/SearchResult.java +++ b/main/src/cgeo/geocaching/SearchResult.java @@ -40,38 +40,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 +95,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 cgCache 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<cgCache> caches) { this(); - for (cgCache cache : caches) { + for (final cgCache cache : caches) { addCache(cache); } } @@ -148,7 +169,7 @@ public class SearchResult implements Parcelable { return; } - this.viewstates = viewstates; + System.arraycopy(viewstates, 0, this.viewstates, 0, viewstates.length); } public int getTotal() { @@ -170,8 +191,7 @@ 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); + final Set<cgCache> caches = cgData.loadCaches(geocodes, LoadFlags.LOAD_CACHE_OR_DB); for (cgCache cache : caches) { // Is there any reason to exclude the cache from the list? final boolean excludeCache = (excludeDisabled && cache.isDisabled()) || @@ -188,13 +208,13 @@ public class SearchResult implements Parcelable { public cgCache getFirstCacheFromResult(final EnumSet<LoadFlag> loadFlags) { if (geocodes != null && geocodes.size() >= 1) { - return cgeoapplication.getInstance().loadCache((String) geocodes.toArray()[0], loadFlags); + return cgData.loadCache((String) geocodes.toArray()[0], loadFlags); } return null; } public Set<cgCache> getCachesFromSearchResult(final EnumSet<LoadFlag> loadFlags) { - return cgeoapplication.getInstance().loadCaches(geocodes, loadFlags); + return cgData.loadCaches(geocodes, loadFlags); } /** Add the geocode to the search. No cache is loaded into the CacheCache */ @@ -213,11 +233,20 @@ 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) { 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; + } + } |
