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.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 geocodes; private StatusCode error = null; private String url = ""; public String[] viewstates = null; private int totalCnt = 0; final public static Parcelable.Creator CREATOR = new Parcelable.Creator() { @Override public SearchResult createFromParcel(Parcel in) { return new SearchResult(in); } @Override public SearchResult[] newArray(int size) { return new SearchResult[size]; } }; public SearchResult() { this((Set) null); } public SearchResult(SearchResult searchResult) { if (searchResult != null) { this.geocodes = new HashSet(searchResult.geocodes); this.error = searchResult.error; this.url = searchResult.url; this.viewstates = searchResult.viewstates; this.setTotal(searchResult.getTotal()); } else { this.geocodes = new HashSet(); } } public SearchResult(final Set geocodes, final int total) { if (geocodes == null) { this.geocodes = new HashSet(); } else { this.geocodes = new HashSet(geocodes.size()); this.geocodes.addAll(geocodes); } this.setTotal(total); } public SearchResult(final Set geocodes) { this(geocodes, geocodes == null ? 0 : geocodes.size()); } public SearchResult(final Parcel in) { ArrayList list = new ArrayList(); in.readStringList(list); geocodes = new HashSet(list); 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()); } public SearchResult(cgCache cache) { this(); addCache(cache); } public SearchResult(Collection caches) { this(); for (cgCache cache : caches) { addCache(cache); } } @Override public void writeToParcel(final Parcel out, final int flags) { out.writeStringArray(geocodes.toArray(new String[geocodes.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 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; } this.viewstates = viewstates; } 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 cachesForVote = new ArrayList(); final Set caches = cgeoapplication.getInstance().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()) || (excludeMine && (cache.isOwn() || cache.isFound())) || (cacheType != CacheType.ALL && cacheType != cache.getType()); if (!excludeCache) { result.addCache(cache); cachesForVote.add(cache); } } GCVote.loadRatings(cachesForVote); return result; } public cgCache getFirstCacheFromResult(final EnumSet loadFlags) { if (geocodes != null && geocodes.size() >= 1) { return cgeoapplication.getInstance().loadCache((String) geocodes.toArray()[0], loadFlags); } return null; } public Set getCachesFromSearchResult(final EnumSet loadFlags) { return cgeoapplication.getInstance().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 geocodes) { return this.geocodes.addAll(geocodes); } /** 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)); } public boolean isEmpty() { return geocodes.isEmpty(); } }