PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/main/src/cgeo/geocaching/SearchResult.java

https://github.com/Portree-Kid/c-geo-opensource
Java | 214 lines | 167 code | 36 blank | 11 comment | 26 complexity | c882d1bb159e67c0cafa65ab0624a8ad MD5 | raw file
  1. package cgeo.geocaching;
  2. import cgeo.geocaching.enumerations.CacheType;
  3. import cgeo.geocaching.enumerations.LoadFlags;
  4. import cgeo.geocaching.enumerations.LoadFlags.LoadFlag;
  5. import cgeo.geocaching.enumerations.LoadFlags.SaveFlag;
  6. import cgeo.geocaching.enumerations.StatusCode;
  7. import cgeo.geocaching.gcvote.GCVote;
  8. import org.apache.commons.collections.CollectionUtils;
  9. import org.apache.commons.lang3.StringUtils;
  10. import android.os.Parcel;
  11. import android.os.Parcelable;
  12. import java.util.ArrayList;
  13. import java.util.Collections;
  14. import java.util.EnumSet;
  15. import java.util.HashSet;
  16. import java.util.Set;
  17. public class SearchResult implements Parcelable {
  18. final private Set<String> geocodes;
  19. private StatusCode error = null;
  20. private String url = "";
  21. public String[] viewstates = null;
  22. private int totalCnt = 0;
  23. final public static Parcelable.Creator<SearchResult> CREATOR = new Parcelable.Creator<SearchResult>() {
  24. public SearchResult createFromParcel(Parcel in) {
  25. return new SearchResult(in);
  26. }
  27. public SearchResult[] newArray(int size) {
  28. return new SearchResult[size];
  29. }
  30. };
  31. public SearchResult() {
  32. this((Set<String>) null);
  33. }
  34. public SearchResult(SearchResult searchResult) {
  35. if (searchResult != null) {
  36. this.geocodes = new HashSet<String>(searchResult.geocodes);
  37. this.error = searchResult.error;
  38. this.url = searchResult.url;
  39. this.viewstates = searchResult.viewstates;
  40. this.setTotal(searchResult.getTotal());
  41. } else {
  42. this.geocodes = new HashSet<String>();
  43. }
  44. }
  45. public SearchResult(final Set<String> geocodes, final int total) {
  46. if (geocodes == null) {
  47. this.geocodes = new HashSet<String>();
  48. } else {
  49. this.geocodes = new HashSet<String>(geocodes.size());
  50. this.geocodes.addAll(geocodes);
  51. }
  52. this.setTotal(total);
  53. }
  54. public SearchResult(final Set<String> geocodes) {
  55. this(geocodes, geocodes == null ? 0 : geocodes.size());
  56. }
  57. public SearchResult(final Parcel in) {
  58. ArrayList<String> list = new ArrayList<String>();
  59. in.readStringList(list);
  60. geocodes = new HashSet<String>(list);
  61. error = (StatusCode) in.readSerializable();
  62. url = in.readString();
  63. final int length = in.readInt();
  64. if (length >= 0) {
  65. viewstates = new String[length];
  66. in.readStringArray(viewstates);
  67. }
  68. setTotal(in.readInt());
  69. }
  70. @Override
  71. public void writeToParcel(final Parcel out, final int flags) {
  72. out.writeStringArray(geocodes.toArray(new String[geocodes.size()]));
  73. out.writeSerializable(error);
  74. out.writeString(url);
  75. if (viewstates == null) {
  76. out.writeInt(-1);
  77. } else {
  78. out.writeInt(viewstates.length);
  79. out.writeStringArray(viewstates);
  80. }
  81. out.writeInt(getTotal());
  82. }
  83. @Override
  84. public int describeContents() {
  85. return 0;
  86. }
  87. public Set<String> getGeocodes() {
  88. return Collections.unmodifiableSet(geocodes);
  89. }
  90. public int getCount() {
  91. return geocodes.size();
  92. }
  93. public StatusCode getError() {
  94. return error;
  95. }
  96. public void setError(final StatusCode error) {
  97. this.error = error;
  98. }
  99. public String getUrl() {
  100. return url;
  101. }
  102. public void setUrl(String url) {
  103. this.url = url;
  104. }
  105. public String[] getViewstates() {
  106. return viewstates;
  107. }
  108. public void setViewstates(String[] viewstates) {
  109. if (cgBase.isEmpty(viewstates)) {
  110. return;
  111. }
  112. this.viewstates = viewstates;
  113. }
  114. public int getTotal() {
  115. return totalCnt;
  116. }
  117. public void setTotal(int totalCnt) {
  118. this.totalCnt = totalCnt;
  119. }
  120. /**
  121. * @param excludeDisabled
  122. * @param excludeMine
  123. * @param cacheType
  124. * @return
  125. */
  126. public SearchResult filterSearchResults(final boolean excludeDisabled, final boolean excludeMine, final CacheType cacheType) {
  127. SearchResult result = new SearchResult(this);
  128. result.geocodes.clear();
  129. final ArrayList<cgCache> cachesForVote = new ArrayList<cgCache>();
  130. final Set<cgCache> caches = cgeoapplication.getInstance().loadCaches(geocodes, LoadFlags.LOAD_CACHE_OR_DB);
  131. for (cgCache cache : caches) {
  132. // Is there any reason to exclude the cache from the list?
  133. final boolean excludeCache = (excludeDisabled && cache.isDisabled()) ||
  134. (excludeMine && (cache.isOwn() || cache.isFound())) ||
  135. (cacheType != CacheType.ALL && cacheType != cache.getType());
  136. if (!excludeCache) {
  137. result.addCache(cache);
  138. cachesForVote.add(cache);
  139. }
  140. }
  141. GCVote.loadRatings(cachesForVote);
  142. return result;
  143. }
  144. public cgCache getFirstCacheFromResult(final EnumSet<LoadFlag> loadFlags) {
  145. if (geocodes != null && geocodes.size() >= 1) {
  146. return cgeoapplication.getInstance().loadCache((String) geocodes.toArray()[0], loadFlags);
  147. }
  148. return null;
  149. }
  150. public Set<cgCache> getCachesFromSearchResult(final EnumSet<LoadFlag> loadFlags) {
  151. return cgeoapplication.getInstance().loadCaches(geocodes, loadFlags);
  152. }
  153. /** Add the geocode to the search. No cache is loaded into the CacheCache */
  154. public boolean addGeocode(final String geocode) {
  155. if (StringUtils.isBlank(geocode)) {
  156. throw new IllegalArgumentException("geocode must not be blank");
  157. }
  158. return geocodes.add(geocode);
  159. }
  160. /** Add the geocodes to the search. No caches are loaded into the CacheCache */
  161. public boolean addGeocodes(Set<String> geocodes) {
  162. return this.geocodes.addAll(geocodes);
  163. }
  164. /** Add the cache geocode to the search and store the cache in the CacheCache */
  165. public boolean addCache(final cgCache cache) {
  166. addGeocode(cache.getGeocode());
  167. return cgeoapplication.getInstance().saveCache(cache, EnumSet.of(SaveFlag.SAVE_CACHE));
  168. }
  169. /** Add the cache geocodes to the search and store them in the CacheCache */
  170. public void addCaches(final Set<cgCache> caches) {
  171. if (CollectionUtils.isEmpty(caches)) {
  172. return;
  173. }
  174. for (final cgCache cache : caches) {
  175. addCache(cache);
  176. }
  177. }
  178. }