aboutsummaryrefslogtreecommitdiffstats
path: root/main/src
diff options
context:
space:
mode:
Diffstat (limited to 'main/src')
-rw-r--r--main/src/cgeo/geocaching/cgCache.java87
-rw-r--r--main/src/cgeo/geocaching/cgeoapplication.java12
-rw-r--r--main/src/cgeo/geocaching/connector/gc/GCParser.java6
-rw-r--r--main/src/cgeo/geocaching/export/GpxExport.java5
-rw-r--r--main/src/cgeo/geocaching/files/GPXParser.java2
-rw-r--r--main/src/cgeo/geocaching/utils/LazyInitializedList.java68
6 files changed, 132 insertions, 48 deletions
diff --git a/main/src/cgeo/geocaching/cgCache.java b/main/src/cgeo/geocaching/cgCache.java
index 461a15e..b38992f 100644
--- a/main/src/cgeo/geocaching/cgCache.java
+++ b/main/src/cgeo/geocaching/cgCache.java
@@ -22,6 +22,7 @@ import cgeo.geocaching.enumerations.WaypointType;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.network.HtmlImage;
import cgeo.geocaching.utils.CancellableHandler;
+import cgeo.geocaching.utils.LazyInitializedList;
import cgeo.geocaching.utils.Log;
import cgeo.geocaching.utils.LogTemplateProvider;
import cgeo.geocaching.utils.LogTemplateProvider.LogContext;
@@ -94,10 +95,25 @@ public class cgCache implements ICache, IWaypoint {
private float myVote = 0; // valid ratings are larger than zero
private int inventoryItems = 0;
private boolean onWatchlist = false;
- private List<String> attributes = null;
- private List<cgWaypoint> waypoints = null;
+ private LazyInitializedList<String> attributes = new LazyInitializedList<String>() {
+ @Override
+ protected List<String> loadFromDatabase() {
+ return cgeoapplication.getInstance().loadAttributes(geocode);
+ }
+ };
+ private LazyInitializedList<cgWaypoint> waypoints = new LazyInitializedList<cgWaypoint>() {
+ @Override
+ protected List<cgWaypoint> loadFromDatabase() {
+ return cgeoapplication.getInstance().loadWaypoints(geocode);
+ }
+ };
private List<cgImage> spoilers = null;
- private List<LogEntry> logs = null;
+ private LazyInitializedList<LogEntry> logs = new LazyInitializedList<LogEntry>() {
+ @Override
+ protected List<LogEntry> loadFromDatabase() {
+ return cgeoapplication.getInstance().loadLogs(geocode);
+ }
+ };
private List<cgTrackable> inventory = null;
private Map<LogType, Integer> logCounts = new HashMap<LogType, Integer>();
private boolean logOffline = false;
@@ -253,14 +269,16 @@ public class cgCache implements ICache, IWaypoint {
if (myVote == 0) {
myVote = other.myVote;
}
- if (attributes == null) {
- attributes = other.attributes;
+ if (attributes.isEmpty()) {
+ attributes.set(other.attributes.get());
}
- if (waypoints == null) {
- waypoints = other.waypoints;
+ if (waypoints.isEmpty()) {
+ waypoints.set(other.waypoints.get());
}
else {
- cgWaypoint.mergeWayPoints(waypoints, other.getWaypoints(), waypoints == null || waypoints.isEmpty());
+ ArrayList<cgWaypoint> newPoints = new ArrayList<cgWaypoint>(waypoints.get());
+ cgWaypoint.mergeWayPoints(newPoints, other.getWaypoints(), false);
+ waypoints.set(newPoints);
}
if (spoilers == null) {
spoilers = other.spoilers;
@@ -273,8 +291,8 @@ public class cgCache implements ICache, IWaypoint {
inventory = other.inventory;
inventoryItems = other.inventoryItems;
}
- if (CollectionUtils.isEmpty(logs)) { // keep last known logs if none
- logs = other.logs;
+ if (logs.isEmpty()) { // keep last known logs if none
+ logs.set(other.logs.get());
}
if (logCounts.size() == 0) {
logCounts = other.logCounts;
@@ -674,10 +692,7 @@ public class cgCache implements ICache, IWaypoint {
@Override
public List<String> getAttributes() {
- if (attributes == null) {
- return Collections.emptyList();
- }
- return Collections.unmodifiableList(attributes);
+ return Collections.unmodifiableList(attributes.get());
}
@Override
@@ -900,10 +915,7 @@ public class cgCache implements ICache, IWaypoint {
* @return always non <code>null</code>
*/
public List<cgWaypoint> getWaypoints() {
- if (waypoints == null) {
- return Collections.emptyList();
- }
- return Collections.unmodifiableList(waypoints);
+ return Collections.unmodifiableList(waypoints.get());
}
/**
@@ -915,7 +927,7 @@ public class cgCache implements ICache, IWaypoint {
* @return <code>true</code> if waypoints successfully added to waypoint database
*/
public boolean setWaypoints(List<cgWaypoint> waypoints, boolean saveToDatabase) {
- this.waypoints = waypoints;
+ this.waypoints.set(waypoints);
finalDefined = false;
if (waypoints != null) {
for (cgWaypoint waypoint : waypoints) {
@@ -929,8 +941,11 @@ public class cgCache implements ICache, IWaypoint {
return saveToDatabase && cgeoapplication.getInstance().saveWaypoints(this);
}
+ /**
+ * @return never <code>null</code>, but an empty collection instead
+ */
public List<LogEntry> getLogs() {
- return getLogs(true);
+ return Collections.unmodifiableList(getLogs(true));
}
/**
@@ -939,14 +954,11 @@ public class cgCache implements ICache, IWaypoint {
* @return the logs with all entries or just the entries of the friends, never <code>null</code>
*/
public List<LogEntry> getLogs(boolean allLogs) {
- if (logs == null) {
- return Collections.emptyList();
- }
if (allLogs) {
- return logs;
+ return logs.get();
}
ArrayList<LogEntry> friendLogs = new ArrayList<LogEntry>();
- for (LogEntry log : logs) {
+ for (LogEntry log : logs.get()) {
if (log.friend) {
friendLogs.add(log);
}
@@ -959,7 +971,7 @@ public class cgCache implements ICache, IWaypoint {
* the log entries
*/
public void setLogs(List<LogEntry> logs) {
- this.logs = logs;
+ this.logs.set(logs);
}
public boolean isLogOffline() {
@@ -1056,7 +1068,7 @@ public class cgCache implements ICache, IWaypoint {
}
public void setAttributes(List<String> attributes) {
- this.attributes = attributes;
+ this.attributes.set(attributes);
}
public void setSpoilers(List<cgImage> spoilers) {
@@ -1122,10 +1134,6 @@ public class cgCache implements ICache, IWaypoint {
* @return <code>true</code> if waypoint successfully added to waypoint database
*/
public boolean addOrChangeWaypoint(final cgWaypoint waypoint, boolean saveToDatabase) {
- if (null == waypoints) {
- waypoints = new ArrayList<cgWaypoint>();
- }
-
waypoint.setGeocode(geocode);
if (waypoint.getId() <= 0) { // this is a new waypoint
@@ -1147,7 +1155,7 @@ public class cgCache implements ICache, IWaypoint {
}
public boolean hasWaypoints() {
- return CollectionUtils.isNotEmpty(waypoints);
+ return !waypoints.isEmpty();
}
public boolean hasFinalDefined() {
@@ -1263,7 +1271,7 @@ public class cgCache implements ICache, IWaypoint {
* @return waypoint or <code>null</code> if index is out of range
*/
public cgWaypoint getWaypoint(final int index) {
- return waypoints != null && index >= 0 && index < waypoints.size() ? waypoints.get(index) : null;
+ return index >= 0 && index < waypoints.size() ? waypoints.get(index) : null;
}
/**
@@ -1314,27 +1322,18 @@ public class cgCache implements ICache, IWaypoint {
}
public void addAttribute(final String attribute) {
- if (attributes == null) {
- attributes = new ArrayList<String>();
- }
attributes.add(attribute);
}
public boolean hasAttributes() {
- return attributes != null && attributes.size() > 0;
+ return !attributes.isEmpty();
}
public void prependLog(final LogEntry log) {
- if (logs == null) {
- logs = new ArrayList<LogEntry>();
- }
- logs.add(0, log);
+ logs.prepend(log);
}
public void appendLog(final LogEntry log) {
- if (logs == null) {
- logs = new ArrayList<LogEntry>();
- }
logs.add(log);
}
diff --git a/main/src/cgeo/geocaching/cgeoapplication.java b/main/src/cgeo/geocaching/cgeoapplication.java
index 572420d..9e221cd 100644
--- a/main/src/cgeo/geocaching/cgeoapplication.java
+++ b/main/src/cgeo/geocaching/cgeoapplication.java
@@ -489,4 +489,16 @@ public class cgeoapplication extends Application {
return storage.getTrackableCodes();
}
+ public List<LogEntry> loadLogs(final String geocode) {
+ return storage.loadLogs(geocode);
+ }
+
+ public List<String> loadAttributes(final String geocode) {
+ return storage.loadAttributes(geocode);
+ }
+
+ public List<cgWaypoint> loadWaypoints(final String geocode) {
+ return storage.loadWaypoints(geocode);
+ }
+
}
diff --git a/main/src/cgeo/geocaching/connector/gc/GCParser.java b/main/src/cgeo/geocaching/connector/gc/GCParser.java
index 218d4fc..778db24 100644
--- a/main/src/cgeo/geocaching/connector/gc/GCParser.java
+++ b/main/src/cgeo/geocaching/connector/gc/GCParser.java
@@ -498,6 +498,7 @@ public abstract class GCParser {
if (null != attributesPre) {
final Matcher matcherAttributesInside = GCConstants.PATTERN_ATTRIBUTESINSIDE.matcher(attributesPre);
+ final ArrayList<String> attributes = new ArrayList<String>();
while (matcherAttributesInside.find()) {
if (matcherAttributesInside.groupCount() > 1 && !matcherAttributesInside.group(2).equalsIgnoreCase("blank")) {
// by default, use the tooltip of the attribute
@@ -512,9 +513,10 @@ public abstract class GCParser {
attribute = imageName.substring(start + 1, end).replace('-', '_').toLowerCase();
}
}
- cache.addAttribute(attribute);
+ attributes.add(attribute);
}
}
+ cache.setAttributes(attributes);
}
} catch (Exception e) {
// failed to parse cache attributes
@@ -1634,7 +1636,7 @@ public abstract class GCParser {
if (allLogs.contains(log)) {
allLogs.get(allLogs.indexOf(log)).friend = true;
} else {
- allLogs.add(log);
+ cache.appendLog(log);
}
}
}
diff --git a/main/src/cgeo/geocaching/export/GpxExport.java b/main/src/cgeo/geocaching/export/GpxExport.java
index 73287da..a97bf0a 100644
--- a/main/src/cgeo/geocaching/export/GpxExport.java
+++ b/main/src/cgeo/geocaching/export/GpxExport.java
@@ -351,12 +351,13 @@ class GpxExport extends AbstractExport {
}
private void writeLogs(final cgCache cache) throws IOException {
- if (cache.getLogs().size() <= 0) {
+ final List<LogEntry> logs = cache.getLogs();
+ if (logs.size() <= 0) {
return;
}
gpx.write("<groundspeak:logs>");
- for (LogEntry log : cache.getLogs()) {
+ for (LogEntry log : logs) {
gpx.write("<groundspeak:log id=\"");
gpx.write(Integer.toString(log.id));
gpx.write("\">");
diff --git a/main/src/cgeo/geocaching/files/GPXParser.java b/main/src/cgeo/geocaching/files/GPXParser.java
index ea26a66..a6a285f 100644
--- a/main/src/cgeo/geocaching/files/GPXParser.java
+++ b/main/src/cgeo/geocaching/files/GPXParser.java
@@ -855,6 +855,8 @@ public abstract class GPXParser extends FileParser {
cache = new cgCache();
cache.setReliableLatLon(true);
+ cache.setAttributes(new ArrayList<String>());
+ cache.setWaypoints(new ArrayList<cgWaypoint>(), false);
for (int i = 0; i < userData.length; i++) {
userData[i] = null;
}
diff --git a/main/src/cgeo/geocaching/utils/LazyInitializedList.java b/main/src/cgeo/geocaching/utils/LazyInitializedList.java
new file mode 100644
index 0000000..6061394
--- /dev/null
+++ b/main/src/cgeo/geocaching/utils/LazyInitializedList.java
@@ -0,0 +1,68 @@
+package cgeo.geocaching.utils;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+public abstract class LazyInitializedList<ElementType> implements Iterable<ElementType> {
+
+ private List<ElementType> list;
+
+ public List<ElementType> get() {
+ initializeList();
+ return list;
+ }
+
+ private void initializeList() {
+ if (list == null) {
+ list = loadFromDatabase();
+ }
+ }
+
+ protected abstract List<ElementType> loadFromDatabase();
+
+ public void add(final ElementType element) {
+ initializeList();
+ list.add(element);
+ }
+
+ public void prepend(final ElementType element) {
+ initializeList();
+ list.add(0, element);
+ }
+
+ public void set(final List<ElementType> elements) {
+ list = new ArrayList<ElementType>(elements);
+ }
+
+ public boolean isEmpty() {
+ initializeList();
+ return list.isEmpty();
+ }
+
+ public ElementType remove(final int index) {
+ initializeList();
+ return list.remove(index);
+ }
+
+ public void add(int index, final ElementType element) {
+ initializeList();
+ list.add(index, element);
+ }
+
+ public int size() {
+ initializeList();
+ return list.size();
+ }
+
+ @Override
+ public Iterator<ElementType> iterator() {
+ initializeList();
+ return list.iterator();
+ }
+
+ public ElementType get(final int index) {
+ initializeList();
+ return list.get(index);
+ }
+}