diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2013-04-06 17:09:52 +0200 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2013-04-06 17:25:23 +0200 |
| commit | f09e2a09ad31f2190d6184fd8fd441fa8c95d6ca (patch) | |
| tree | 8b2518455f207e499643ab10ab4a4195cfb0959d /main/src | |
| parent | 60a9cc4e0c29c8728c310dd08d4368b171859ca4 (diff) | |
| download | cgeo-f09e2a09ad31f2190d6184fd8fd441fa8c95d6ca.zip cgeo-f09e2a09ad31f2190d6184fd8fd441fa8c95d6ca.tar.gz cgeo-f09e2a09ad31f2190d6184fd8fd441fa8c95d6ca.tar.bz2 | |
refactoring: simplify batch export of geocaches
By using list-backed sublists, the code to export geocaches in batch can
be simplified.
Also, there are places where a more generic Collection type can be used
in formal parameters to avoid influencing the type of actual arguments.
Diffstat (limited to 'main/src')
| -rw-r--r-- | main/src/cgeo/geocaching/cgData.java | 3 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/export/GpxExport.java | 25 |
2 files changed, 11 insertions, 17 deletions
diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java index 28485a5..86821fb 100644 --- a/main/src/cgeo/geocaching/cgData.java +++ b/main/src/cgeo/geocaching/cgData.java @@ -33,6 +33,7 @@ import java.io.File; import java.io.FilenameFilter; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.EnumSet; @@ -1396,7 +1397,7 @@ public class cgData { * @param geocodes * @return Set of loaded caches. Never null. */ - public static Set<Geocache> loadCaches(final Set<String> geocodes, final EnumSet<LoadFlag> loadFlags) { + public static Set<Geocache> loadCaches(final Collection<String> geocodes, final EnumSet<LoadFlag> loadFlags) { if (CollectionUtils.isEmpty(geocodes)) { return new HashSet<Geocache>(); } diff --git a/main/src/cgeo/geocaching/export/GpxExport.java b/main/src/cgeo/geocaching/export/GpxExport.java index 19b8518..4c5d1e2 100644 --- a/main/src/cgeo/geocaching/export/GpxExport.java +++ b/main/src/cgeo/geocaching/export/GpxExport.java @@ -5,9 +5,9 @@ import cgeo.geocaching.LogEntry; import cgeo.geocaching.R; import cgeo.geocaching.Settings; import cgeo.geocaching.Waypoint; +import cgeo.geocaching.activity.ActivityMixin; import cgeo.geocaching.cgData; import cgeo.geocaching.cgeoapplication; -import cgeo.geocaching.activity.ActivityMixin; import cgeo.geocaching.enumerations.CacheAttribute; import cgeo.geocaching.enumerations.LoadFlags; import cgeo.geocaching.geopoint.Geopoint; @@ -37,9 +37,8 @@ import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Collection; import java.util.Date; -import java.util.HashSet; -import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Set; @@ -106,7 +105,7 @@ class GpxExport extends AbstractExport { } private class ExportTask extends AsyncTaskWithProgress<Void, File> { - private final Set<String> allGeocodes = new HashSet<String>(); + private final List<String> allGeocodes; private final Activity activity; private int countExported = 0; @@ -122,7 +121,8 @@ class GpxExport extends AbstractExport { super(activity, caches.size(), getProgressTitle(), cgeoapplication.getInstance().getResources().getQuantityString(R.plurals.cache_counts, caches.size(), caches.size())); // get rid of the (half loaded) caches, we will reload them as full caches during the export - for (Geocache geocache : caches) { + allGeocodes = new ArrayList<String>(caches.size()); + for (final Geocache geocache : caches) { allGeocodes.add(geocache.getGeocode()); } this.activity = activity; @@ -159,17 +159,10 @@ class GpxExport extends AbstractExport { // Split the overall set of geocodes into small chunks. That is a compromise between memory efficiency (because // we don't load all caches fully into memory) and speed (because we don't query each cache separately). - while (!allGeocodes.isEmpty()) { - int cachesInBatch = 0; - HashSet<String> geocodesOfBatch = new HashSet<String>(CACHES_PER_BATCH); - for (Iterator<String> iterator = allGeocodes.iterator(); iterator.hasNext() && cachesInBatch < CACHES_PER_BATCH;) { - final String geocode = iterator.next(); - geocodesOfBatch.add(geocode); - cachesInBatch++; - } - allGeocodes.removeAll(geocodesOfBatch); - exportBatch(gpx, geocodesOfBatch); + final List<String> batch = allGeocodes.subList(0, Math.min(CACHES_PER_BATCH, allGeocodes.size())); + exportBatch(gpx, batch); + batch.clear(); } gpx.endTag(PREFIX_GPX, "gpx"); @@ -195,7 +188,7 @@ class GpxExport extends AbstractExport { return exportFile; } - private void exportBatch(final XmlSerializer gpx, HashSet<String> geocodesOfBatch) throws IOException { + private void exportBatch(final XmlSerializer gpx, Collection<String> geocodesOfBatch) throws IOException { Set<Geocache> caches = cgData.loadCaches(geocodesOfBatch, LoadFlags.LOAD_ALL_DB_ONLY); for (Geocache cache : caches) { gpx.startTag(PREFIX_GPX, "wpt"); |
