diff options
-rw-r--r-- | main/src/cgeo/geocaching/ParseResult.java | 8 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/SearchResult.java | 32 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgData.java | 17 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgeoapplication.java | 22 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgeocaches.java | 4 | ||||
-rw-r--r-- | tests/src/cgeo/geocaching/files/GPXImporterTest.java | 14 |
6 files changed, 53 insertions, 44 deletions
diff --git a/main/src/cgeo/geocaching/ParseResult.java b/main/src/cgeo/geocaching/ParseResult.java index 976a014..ee5bfe5 100644 --- a/main/src/cgeo/geocaching/ParseResult.java +++ b/main/src/cgeo/geocaching/ParseResult.java @@ -5,6 +5,7 @@ import cgeo.geocaching.enumerations.LoadFlags; import java.util.ArrayList; import java.util.List; +import java.util.Set; /** * Search result including list of caches @@ -26,7 +27,7 @@ public class ParseResult extends SearchResult { cacheList.addAll(parseResult.cacheList); } - public ParseResult(final List<String> geocodes) { + public ParseResult(final Set<String> geocodes) { super(geocodes); cgeoapplication app = cgeoapplication.getInstance(); for (String geocode : geocodes) { @@ -47,8 +48,9 @@ public class ParseResult extends SearchResult { (excludeMine && (cache.isOwn() || cache.isFound())) || (cacheType != CacheType.ALL && cacheType != cache.getType()); if (!excludeCache) { - result.addGeocode(cache.getGeocode()); - result.cacheList.add(cache); + if (result.addGeocode(cache.getGeocode())) { + result.cacheList.add(cache); + } } } } diff --git a/main/src/cgeo/geocaching/SearchResult.java b/main/src/cgeo/geocaching/SearchResult.java index 54120ef..fdb50a1 100644 --- a/main/src/cgeo/geocaching/SearchResult.java +++ b/main/src/cgeo/geocaching/SearchResult.java @@ -7,11 +7,12 @@ import android.os.Parcelable; import java.util.ArrayList; import java.util.Collections; -import java.util.List; +import java.util.HashSet; +import java.util.Set; public class SearchResult implements Parcelable { - final protected List<String> geocodes; + final protected Set<String> geocodes; public StatusCode error = null; public String url = ""; public String[] viewstates = null; @@ -28,33 +29,34 @@ public class SearchResult implements Parcelable { }; public SearchResult() { - this((List<String>) null); + this((Set<String>) null); } public SearchResult(SearchResult searchResult) { if (searchResult != null) { - this.geocodes = new ArrayList<String>(searchResult.geocodes); + this.geocodes = new HashSet<String>(searchResult.geocodes); this.error = searchResult.error; this.url = searchResult.url; this.viewstates = searchResult.viewstates; this.totalCnt = searchResult.totalCnt; } else { - this.geocodes = new ArrayList<String>(); + this.geocodes = new HashSet<String>(); } } - public SearchResult(final List<String> geocodes) { + public SearchResult(final Set<String> geocodes) { if (geocodes == null) { - this.geocodes = new ArrayList<String>(); + this.geocodes = new HashSet<String>(); } else { - this.geocodes = new ArrayList<String>(geocodes.size()); + this.geocodes = new HashSet<String>(geocodes.size()); this.geocodes.addAll(geocodes); } } public SearchResult(final Parcel in) { - geocodes = new ArrayList<String>(); - in.readStringList(geocodes); + ArrayList<String> list = new ArrayList<String>(); + in.readStringList(list); + geocodes = new HashSet<String>(list); error = (StatusCode) in.readSerializable(); url = in.readString(); final int length = in.readInt(); @@ -67,7 +69,7 @@ public class SearchResult implements Parcelable { @Override public void writeToParcel(final Parcel out, final int flags) { - out.writeStringList(geocodes); + out.writeStringArray(geocodes.toArray(new String[geocodes.size()])); out.writeSerializable(error); out.writeString(url); if (viewstates == null) { @@ -84,16 +86,16 @@ public class SearchResult implements Parcelable { return 0; } - public List<String> getGeocodes() { - return Collections.unmodifiableList(geocodes); + public Set<String> getGeocodes() { + return Collections.unmodifiableSet(geocodes); } public int getCount() { return geocodes.size(); } - public void addGeocode(final String geocode) { - geocodes.add(geocode); + public boolean addGeocode(final String geocode) { + return geocodes.add(geocode); } public static StatusCode getError(final SearchResult search) { diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java index b095da2..7562104 100644 --- a/main/src/cgeo/geocaching/cgData.java +++ b/main/src/cgeo/geocaching/cgData.java @@ -29,6 +29,7 @@ import java.util.Collections; import java.util.Date; import java.util.EnumSet; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Locale; @@ -2584,10 +2585,10 @@ public class cgData { return count; } - public List<String> loadBatchOfStoredGeocodes(final boolean detailedOnly, final Geopoint coords, final CacheType cacheType, final int list) { + public Set<String> loadBatchOfStoredGeocodes(final boolean detailedOnly, final Geopoint coords, final CacheType cacheType, final int list) { init(); - List<String> geocodes = new ArrayList<String>(); + Set<String> geocodes = new HashSet<String>(); StringBuilder specifySql = new StringBuilder(); @@ -2636,10 +2637,10 @@ public class cgData { return geocodes; } - public List<String> loadBatchOfHistoricGeocodes(final boolean detailedOnly, final CacheType cacheType) { + public Set<String> loadBatchOfHistoricGeocodes(final boolean detailedOnly, final CacheType cacheType) { init(); - List<String> geocodes = new ArrayList<String>(); + Set<String> geocodes = new HashSet<String>(); StringBuilder specifySql = new StringBuilder(); specifySql.append("visiteddate > 0"); @@ -2686,22 +2687,22 @@ public class cgData { return geocodes; } - public List<String> getCachedInViewport(final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon, final CacheType cacheType) { + public Set<String> getCachedInViewport(final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon, final CacheType cacheType) { return getInViewport(false, centerLat, centerLon, spanLat, spanLon, cacheType); } - public List<String> getStoredInViewport(final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon, final CacheType cacheType) { + public Set<String> getStoredInViewport(final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon, final CacheType cacheType) { return getInViewport(true, centerLat, centerLon, spanLat, spanLon, cacheType); } - public List<String> getInViewport(final boolean stored, final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon, final CacheType cacheType) { + public Set<String> getInViewport(final boolean stored, final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon, final CacheType cacheType) { if (centerLat == null || centerLon == null || spanLat == null || spanLon == null) { return null; } init(); - List<String> geocodes = new ArrayList<String>(); + Set<String> geocodes = new HashSet<String>(); // viewport limitation double latMin = (centerLat / 1e6) - ((spanLat / 1e6) / 2) - ((spanLat / 1e6) / 4); diff --git a/main/src/cgeo/geocaching/cgeoapplication.java b/main/src/cgeo/geocaching/cgeoapplication.java index 856a51c..3bfe69a 100644 --- a/main/src/cgeo/geocaching/cgeoapplication.java +++ b/main/src/cgeo/geocaching/cgeoapplication.java @@ -23,8 +23,10 @@ import java.io.File; import java.util.ArrayList; import java.util.Date; import java.util.EnumSet; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; public class cgeoapplication extends Application { @@ -303,7 +305,7 @@ public class cgeoapplication extends Application { return null; } - List<String> geocodeList = new ArrayList<String>(); + Set<String> geocodeList = new HashSet<String>(); geocodeList.add(geocode); return getBounds(geocodeList); @@ -317,7 +319,7 @@ public class cgeoapplication extends Application { return getBounds(search.getGeocodes()); } - public List<Number> getBounds(final List<String> geocodes) { + public List<Number> getBounds(final Set<String> geocodes) { if (CollectionUtils.isEmpty(geocodes)) { return null; } @@ -326,13 +328,13 @@ public class cgeoapplication extends Application { } public cgCache getCache(final SearchResult search) { - if (search == null) { + if (search == null || search.getCount() < 1) { return null; } - final List<String> geocodeList = search.getGeocodes(); + final Set<String> geocodeList = search.getGeocodes(); - return getCacheByGeocode(geocodeList.get(0), LoadFlags.LOADALL); + return getCacheByGeocode(geocodeList.toArray(new String[geocodeList.size()])[0], LoadFlags.LOADALL); } /** @@ -363,7 +365,7 @@ public class cgeoapplication extends Application { List<cgCache> cachesOut = new ArrayList<cgCache>(); - final List<String> geocodeList = search.getGeocodes(); + final Set<String> geocodeList = search.getGeocodes(); // The list of geocodes is sufficient. more parameters generate an overly complex select. final List<cgCache> cachesPre = storage.loadCaches(geocodeList.toArray(), null, null, null, null, loadFlags); @@ -375,7 +377,7 @@ public class cgeoapplication extends Application { } public SearchResult getBatchOfStoredCaches(final boolean detailedOnly, final Geopoint coords, final CacheType cacheType, final int list) { - final List<String> geocodes = storage.loadBatchOfStoredGeocodes(detailedOnly, coords, cacheType, list); + final Set<String> geocodes = storage.loadBatchOfStoredGeocodes(detailedOnly, coords, cacheType, list); final SearchResult search = new SearchResult(geocodes); search.totalCnt = getAllStoredCachesCount(true, cacheType, list); return search; @@ -386,19 +388,19 @@ public class cgeoapplication extends Application { } public SearchResult getHistoryOfCaches(final boolean detailedOnly, final CacheType cacheType) { - final List<String> geocodes = storage.loadBatchOfHistoricGeocodes(detailedOnly, cacheType); + final Set<String> geocodes = storage.loadBatchOfHistoricGeocodes(detailedOnly, cacheType); final SearchResult search = new SearchResult(geocodes); search.totalCnt = getAllHistoricCachesCount(); return search; } public SearchResult getCachedInViewport(final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon, final CacheType cacheType) { - final List<String> geocodes = storage.getCachedInViewport(centerLat, centerLon, spanLat, spanLon, cacheType); + final Set<String> geocodes = storage.getCachedInViewport(centerLat, centerLon, spanLat, spanLon, cacheType); return new SearchResult(geocodes); } public SearchResult getStoredInViewport(final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon, final CacheType cacheType) { - final List<String> geocodes = storage.getStoredInViewport(centerLat, centerLon, spanLat, spanLon, cacheType); + final Set<String> geocodes = storage.getStoredInViewport(centerLat, centerLon, spanLat, spanLon, cacheType); return new SearchResult(geocodes); } diff --git a/main/src/cgeo/geocaching/cgeocaches.java b/main/src/cgeo/geocaching/cgeocaches.java index dba64e0..29d2093 100644 --- a/main/src/cgeo/geocaching/cgeocaches.java +++ b/main/src/cgeo/geocaching/cgeocaches.java @@ -75,8 +75,10 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; public class cgeocaches extends AbstractListActivity { @@ -2528,7 +2530,7 @@ public class cgeocaches extends AbstractListActivity { // apply filter settings (if there's a filter) if (adapter != null) { - List<String> geocodes = new ArrayList<String>(); + Set<String> geocodes = new HashSet<String>(); for (cgCache cache : adapter.getFilteredList()) { geocodes.add(cache.getGeocode()); } diff --git a/tests/src/cgeo/geocaching/files/GPXImporterTest.java b/tests/src/cgeo/geocaching/files/GPXImporterTest.java index 457bb21..b883278 100644 --- a/tests/src/cgeo/geocaching/files/GPXImporterTest.java +++ b/tests/src/cgeo/geocaching/files/GPXImporterTest.java @@ -1,8 +1,8 @@ package cgeo.geocaching.files; +import cgeo.geocaching.SearchResult; import cgeo.geocaching.Settings; import cgeo.geocaching.cgCache; -import cgeo.geocaching.SearchResult; import cgeo.geocaching.cgeoapplication; import cgeo.geocaching.test.AbstractResourceInstrumentationTestCase; import cgeo.geocaching.test.R; @@ -54,7 +54,7 @@ public class GPXImporterTest extends AbstractResourceInstrumentationTestCase { assertEquals(GPXImporter.IMPORT_STEP_STORE_CACHES, iMsg.next().what); assertEquals(GPXImporter.IMPORT_STEP_FINISHED, iMsg.next().what); SearchResult search = (SearchResult) importStepHandler.messages.get(3).obj; - assertEquals(Collections.singletonList("GC31J2H"), search.getGeocodes()); + assertEquals(Collections.singletonList("GC31J2H"), new ArrayList<String>(search.getGeocodes())); cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("GC31J2H"); assertCacheProperties(cache); @@ -83,7 +83,7 @@ public class GPXImporterTest extends AbstractResourceInstrumentationTestCase { assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_READ_WPT_FILE, GPXImporter.IMPORT_STEP_STORE_CACHES, GPXImporter.IMPORT_STEP_FINISHED); SearchResult search = (SearchResult) importStepHandler.messages.get(4).obj; - assertEquals(Collections.singletonList("GC31J2H"), search.getGeocodes()); + assertEquals(Collections.singletonList("GC31J2H"), new ArrayList<String>(search.getGeocodes())); cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("GC31J2H"); assertCacheProperties(cache); @@ -106,7 +106,7 @@ public class GPXImporterTest extends AbstractResourceInstrumentationTestCase { assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_STORE_CACHES, GPXImporter.IMPORT_STEP_FINISHED); SearchResult search = (SearchResult) importStepHandler.messages.get(3).obj; - assertEquals(Collections.singletonList("OC5952"), search.getGeocodes()); + assertEquals(Collections.singletonList("OC5952"), new ArrayList<String>(search.getGeocodes())); cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("OC5952"); assertCacheProperties(cache); @@ -146,7 +146,7 @@ public class GPXImporterTest extends AbstractResourceInstrumentationTestCase { assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_STORE_CACHES, GPXImporter.IMPORT_STEP_FINISHED); SearchResult search = (SearchResult) importStepHandler.messages.get(3).obj; - assertEquals(Collections.singletonList("GC31J2H"), search.getGeocodes()); + assertEquals(Collections.singletonList("GC31J2H"), new ArrayList<String>(search.getGeocodes())); cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("GC31J2H"); assertCacheProperties(cache); @@ -164,7 +164,7 @@ public class GPXImporterTest extends AbstractResourceInstrumentationTestCase { assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_READ_WPT_FILE, GPXImporter.IMPORT_STEP_STORE_CACHES, GPXImporter.IMPORT_STEP_FINISHED); SearchResult search = (SearchResult) importStepHandler.messages.get(4).obj; - assertEquals(Collections.singletonList("GC31J2H"), search.getGeocodes()); + assertEquals(Collections.singletonList("GC31J2H"), new ArrayList<String>(search.getGeocodes())); cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("GC31J2H"); assertCacheProperties(cache); @@ -189,7 +189,7 @@ public class GPXImporterTest extends AbstractResourceInstrumentationTestCase { assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_READ_WPT_FILE, GPXImporter.IMPORT_STEP_STORE_CACHES, GPXImporter.IMPORT_STEP_FINISHED); SearchResult search = (SearchResult) importStepHandler.messages.get(4).obj; - assertEquals(Collections.singletonList("GC31J2H"), search.getGeocodes()); + assertEquals(Collections.singletonList("GC31J2H"), new ArrayList<String>(search.getGeocodes())); cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("GC31J2H"); assertCacheProperties(cache); |