diff options
author | Samuel Tardieu <sam@rfc1149.net> | 2011-10-27 09:05:04 +0200 |
---|---|---|
committer | Samuel Tardieu <sam@rfc1149.net> | 2011-10-27 09:05:04 +0200 |
commit | 0e645cf3b74144de8d10f7bc8eaa5e76530e1857 (patch) | |
tree | 5d72c631e4a117828e8c43eb41e0f5fe5ab06f67 | |
parent | 863ebe9d98f0159fd542cbd8d5cffce0c047e45b (diff) | |
download | cgeo-0e645cf3b74144de8d10f7bc8eaa5e76530e1857.zip cgeo-0e645cf3b74144de8d10f7bc8eaa5e76530e1857.tar.gz cgeo-0e645cf3b74144de8d10f7bc8eaa5e76530e1857.tar.bz2 |
Use flags rather than 6 different booleans
This makes the code easier to read, as one can tell right away what is
being loaded.
-rw-r--r-- | main/src/cgeo/geocaching/cgBase.java | 2 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgCache.java | 11 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgData.java | 18 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgeoapplication.java | 24 |
4 files changed, 32 insertions, 23 deletions
diff --git a/main/src/cgeo/geocaching/cgBase.java b/main/src/cgeo/geocaching/cgBase.java index 108b431..836b06b 100644 --- a/main/src/cgeo/geocaching/cgBase.java +++ b/main/src/cgeo/geocaching/cgBase.java @@ -2102,7 +2102,7 @@ public class cgBase { final String realGeocode = StringUtils.isNotBlank(geocode) ? geocode : app.getGeocode(guid); List<cgCache> cacheList = new ArrayList<cgCache>(); - cacheList.add(app.getCacheByGeocode(realGeocode, true, true, true, true, true, true)); + cacheList.add(app.getCacheByGeocode(realGeocode, cgCache.LOADALL)); search.addGeocode(realGeocode); app.addSearch(search, cacheList, false, reason); diff --git a/main/src/cgeo/geocaching/cgCache.java b/main/src/cgeo/geocaching/cgCache.java index 1479fec..61cd1a7 100644 --- a/main/src/cgeo/geocaching/cgCache.java +++ b/main/src/cgeo/geocaching/cgCache.java @@ -31,6 +31,17 @@ import java.util.regex.Pattern; */ public class cgCache implements ICache { + /** + * Cache loading parameters + */ + final public static int LOADATTRIBUTES = 1 << 0; + final public static int LOADWAYPOINTS = 1 << 1; + final public static int LOADSPOILERS = 1 << 2; + final public static int LOADLOGS = 1 << 3; + final public static int LOADINVENTORY = 1 << 4; + final public static int LOADOFFLINELOG = 1 << 5; + final public static int LOADALL = LOADATTRIBUTES | LOADWAYPOINTS | LOADSPOILERS | LOADLOGS | LOADINVENTORY | LOADOFFLINELOG; + public Long updated = null; public Long detailedUpdate = null; public Long visitedDate = null; diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java index e4796e2..82c8340 100644 --- a/main/src/cgeo/geocaching/cgData.java +++ b/main/src/cgeo/geocaching/cgData.java @@ -1792,7 +1792,7 @@ public class cgData { * @return the loaded cache */ - public cgCache loadCache(String geocode, String guid, boolean loadAttributes, boolean loadWaypoints, boolean loadSpoilers, boolean loadLogs, boolean loadInventory, boolean loadOfflineLogs) { + public cgCache loadCache(final String geocode, final String guid, final int loadFlags) { Object[] geocodes = new Object[1]; Object[] guids = new Object[1]; @@ -1808,7 +1808,7 @@ public class cgData { guids = null; } - List<cgCache> caches = loadCaches(geocodes, null, null, null, null, null, loadAttributes, loadWaypoints, loadSpoilers, loadLogs, loadInventory, loadOfflineLogs); + List<cgCache> caches = loadCaches(geocodes, null, null, null, null, null, loadFlags); if (CollectionUtils.isNotEmpty(caches)) { return caches.get(0); } @@ -1816,7 +1816,7 @@ public class cgData { return null; } - public List<cgCache> loadCaches(Object[] geocodes, Object[] guids, Long centerLat, Long centerLon, Long spanLat, Long spanLon, boolean loadAttributes, boolean loadWaypoints, boolean loadSpoilers, boolean loadLogs, boolean loadInventory, boolean loadOfflineLogs) { + public List<cgCache> loadCaches(final Object[] geocodes, final Object[] guids, final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon, final int loadFlags) { init(); // Using more than one of the parametersets results in overly comlex wheres if (((geocodes != null && geocodes.length > 0) && (guids != null && guids.length > 0))) { @@ -1926,7 +1926,7 @@ public class cgData { // cache.attributes entity probably does not need to be preserved, // and the resolution of the "if" statement could be simply // cache.attributes = attributes - if (loadAttributes) { + if ((loadFlags & cgCache.LOADATTRIBUTES) != 0) { final List<String> attributes = loadAttributes(cache.geocode); if (CollectionUtils.isNotEmpty(attributes)) { if (cache.attributes == null) { @@ -1938,7 +1938,7 @@ public class cgData { } } - if (loadWaypoints) { + if ((loadFlags & cgCache.LOADWAYPOINTS) != 0) { final List<cgWaypoint> waypoints = loadWaypoints(cache.geocode); if (CollectionUtils.isNotEmpty(waypoints)) { if (cache.waypoints == null) { @@ -1950,7 +1950,7 @@ public class cgData { } } - if (loadSpoilers) { + if ((loadFlags & cgCache.LOADSPOILERS) != 0) { final List<cgImage> spoilers = loadSpoilers(cache.geocode); if (CollectionUtils.isNotEmpty(spoilers)) { if (cache.spoilers == null) { @@ -1962,7 +1962,7 @@ public class cgData { } } - if (loadLogs) { + if ((loadFlags & cgCache.LOADLOGS) != 0) { final List<cgLog> logs = loadLogs(cache.geocode); if (CollectionUtils.isNotEmpty(logs)) { if (cache.logs == null) { @@ -1979,7 +1979,7 @@ public class cgData { } } - if (loadInventory) { + if ((loadFlags & cgCache.LOADINVENTORY) != 0) { final List<cgTrackable> inventory = loadInventory(cache.geocode); if (CollectionUtils.isNotEmpty(inventory)) { if (cache.inventory == null) { @@ -1991,7 +1991,7 @@ public class cgData { } } - if (loadOfflineLogs) { + if ((loadFlags & cgCache.LOADOFFLINELOG) != 0) { cache.logOffline = hasLogOffline(cache.geocode); } diff --git a/main/src/cgeo/geocaching/cgeoapplication.java b/main/src/cgeo/geocaching/cgeoapplication.java index 1573b3f..3617bf7 100644 --- a/main/src/cgeo/geocaching/cgeoapplication.java +++ b/main/src/cgeo/geocaching/cgeoapplication.java @@ -303,17 +303,17 @@ public class cgeoapplication extends Application { return null; } - return getCacheByGeocode(geocode, false, true, false, false, false, false); + return getCacheByGeocode(geocode, cgCache.LOADWAYPOINTS); } - public cgCache getCacheByGeocode(final String geocode, final boolean loadAttributes, final boolean loadWaypoints, final boolean loadSpoilers, final boolean loadLogs, final boolean loadInventory, final boolean loadOfflineLog) { + public cgCache getCacheByGeocode(final String geocode, final int loadFlags) { if (cachesCache.containsKey(geocode)) { return cachesCache.get(geocode); } - final cgCache cache = getStorage().loadCache(geocode, null, loadAttributes, loadWaypoints, loadSpoilers, loadLogs, loadInventory, loadOfflineLog); + final cgCache cache = getStorage().loadCache(geocode, null, loadFlags); - if (cache != null && cache.detailed && loadAttributes && loadWaypoints && loadSpoilers && loadLogs && loadInventory) { + if (cache != null && cache.detailed && loadFlags == cgCache.LOADALL) { putCacheInCache(cache); } @@ -401,7 +401,7 @@ public class cgeoapplication extends Application { final List<String> geocodeList = search.getGeocodes(); - return getCacheByGeocode(geocodeList.get(0), true, true, true, true, true, true); + return getCacheByGeocode(geocodeList.get(0), cgCache.LOADALL); } /** @@ -411,19 +411,18 @@ public class cgeoapplication extends Application { * @return */ public List<cgCache> getCaches(final cgSearch search, final boolean loadWaypoints) { - return getCaches(search, null, null, null, null, false, loadWaypoints, false, false, false, true); + return getCaches(search, null, null, null, null, (loadWaypoints ? cgCache.LOADWAYPOINTS : 0) | cgCache.LOADOFFLINELOG); } public List<cgCache> getCaches(final cgSearch search, Long centerLat, Long centerLon, Long spanLat, Long spanLon) { - return getCaches(search, centerLat, centerLon, spanLat, spanLon, false, true, false, false, false, true); + return getCaches(search, centerLat, centerLon, spanLat, spanLon, cgCache.LOADWAYPOINTS | cgCache.LOADOFFLINELOG); } - public List<cgCache> getCaches(final cgSearch search, Long centerLat, Long centerLon, Long spanLat, Long spanLon, boolean loadA, boolean loadW, boolean loadS, boolean loadL, boolean loadI, boolean loadO) { + public List<cgCache> getCaches(final cgSearch search, final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon, final int loadFlags) { if (search == null) { List<cgCache> cachesOut = new ArrayList<cgCache>(); - final List<cgCache> cachesPre = storage.loadCaches(null, null, centerLat, centerLon, spanLat, spanLon, loadA, loadW, loadS, loadL, loadI, loadO); - + final List<cgCache> cachesPre = storage.loadCaches(null, null, centerLat, centerLon, spanLat, spanLon, loadFlags); if (cachesPre != null) { cachesOut.addAll(cachesPre); } @@ -436,7 +435,7 @@ public class cgeoapplication extends Application { final List<String> geocodeList = search.getGeocodes(); // The list of geocodes is sufficient. more parameters generate an overly complex select. - final List<cgCache> cachesPre = getStorage().loadCaches(geocodeList.toArray(), null, null, null, null, null, loadA, loadW, loadS, loadL, loadI, loadO); + final List<cgCache> cachesPre = getStorage().loadCaches(geocodeList.toArray(), null, null, null, null, null, loadFlags); if (cachesPre != null) { cachesOut.addAll(cachesPre); } @@ -588,8 +587,7 @@ public class cgeoapplication extends Application { private boolean storeWithMerge(final cgCache cache, final boolean override) { if (!override) { - final cgCache oldCache = storage.loadCache(cache.geocode, cache.guid, - true, true, true, true, true, true); + final cgCache oldCache = storage.loadCache(cache.geocode, cache.guid, cgCache.LOADALL); cache.gatherMissingFrom(oldCache); } return storage.saveCache(cache); |