aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2012-04-11 09:31:34 +0200
committerSamuel Tardieu <sam@rfc1149.net>2012-04-11 15:45:32 +0200
commit0ba91885caf93dfa363b3de656a214bd031d9bc7 (patch)
treeb1ba746c9e1ec200e7c7753a51a04dd5cd220bd8 /main/src/cgeo
parent138fb60259388ffe469e435a70f79e8676b96e05 (diff)
downloadcgeo-0ba91885caf93dfa363b3de656a214bd031d9bc7.zip
cgeo-0ba91885caf93dfa363b3de656a214bd031d9bc7.tar.gz
cgeo-0ba91885caf93dfa363b3de656a214bd031d9bc7.tar.bz2
Make cgDestination an immutable type
Also, savedSearchedDestination() result is always ignored, so make it void. The side-effects previously done in savedSearchDestination() are also ignored as they only apply to a temporary value that is never returned, so making the type immutable does not harm.
Diffstat (limited to 'main/src/cgeo')
-rw-r--r--main/src/cgeo/geocaching/cgData.java47
-rw-r--r--main/src/cgeo/geocaching/cgDestination.java54
-rw-r--r--main/src/cgeo/geocaching/cgeoapplication.java4
-rw-r--r--main/src/cgeo/geocaching/cgeopoint.java12
4 files changed, 35 insertions, 82 deletions
diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java
index b711de6..7a3cece 100644
--- a/main/src/cgeo/geocaching/cgData.java
+++ b/main/src/cgeo/geocaching/cgData.java
@@ -1412,37 +1412,25 @@ public class cgData {
/**
* Persists the given <code>destination</code> into the database.
*
- * @param destinations
- * @return <code>true</code> if the given destination was successfully
- * persisted <code>false</code> otherwise.
+ * @param destination
+ * a destination to save
*/
- public boolean saveSearchedDestination(cgDestination destination) {
- boolean success = true;
-
- if (destination == null) {
- success = false;
- } else {
- init();
-
- databaseRW.beginTransaction();
+ public void saveSearchedDestination(final cgDestination destination) {
+ init();
- try {
- ContentValues values = new ContentValues();
- values.put("date", destination.getDate());
- putCoords(values, destination.getCoords());
+ databaseRW.beginTransaction();
- long id = databaseRW.insert(dbTableSearchDestionationHistory, null, values);
- destination.setId(id);
- databaseRW.setTransactionSuccessful();
- } catch (Exception e) {
- success = false;
- Log.e(Settings.tag, "Updating searchedDestinations db failed", e);
- } finally {
- databaseRW.endTransaction();
- }
+ try {
+ ContentValues values = new ContentValues();
+ values.put("date", destination.getDate());
+ putCoords(values, destination.getCoords());
+ databaseRW.insert(dbTableSearchDestionationHistory, null, values);
+ databaseRW.setTransactionSuccessful();
+ } catch (Exception e) {
+ Log.e(Settings.tag, "Updating searchedDestinations db failed", e);
+ } finally {
+ databaseRW.endTransaction();
}
-
- return success;
}
public boolean saveWaypoints(String geocode, List<cgWaypoint> waypoints, boolean drop) {
@@ -2323,10 +2311,7 @@ public class cgData {
int indexLongitude = cursor.getColumnIndex("longitude");
do {
- final cgDestination dest = new cgDestination();
- dest.setId(cursor.getLong(indexId));
- dest.setDate(cursor.getLong(indexDate));
- dest.setCoords(getCoords(cursor, indexLatitude, indexLongitude));
+ final cgDestination dest = new cgDestination(cursor.getLong(indexId), cursor.getLong(indexDate), getCoords(cursor, indexLatitude, indexLongitude));
// If coordinates are non-existent or invalid, do not consider
// this point.
diff --git a/main/src/cgeo/geocaching/cgDestination.java b/main/src/cgeo/geocaching/cgDestination.java
index cf9a8ef..1e5253c 100644
--- a/main/src/cgeo/geocaching/cgDestination.java
+++ b/main/src/cgeo/geocaching/cgDestination.java
@@ -2,73 +2,43 @@ package cgeo.geocaching;
import cgeo.geocaching.geopoint.Geopoint;
-public class cgDestination {
+public class cgDestination implements ICoordinates {
- private long id;
-
- private long date;
-
- private Geopoint coords;
-
- public cgDestination() {
- }
+ final private long id;
+ final private long date;
+ final private Geopoint coords;
public cgDestination(long id, long date, final Geopoint coords) {
- super();
this.id = id;
this.date = date;
this.coords = coords;
}
- public long getDate() {
- return date;
+ public cgDestination withDate(final long date) {
+ return new cgDestination(id, date, coords);
}
- public void setDate(long date) {
- this.date = date;
+ public long getDate() {
+ return date;
}
+ @Override
public Geopoint getCoords() {
return coords;
}
- public void setCoords(final Geopoint coords) {
- this.coords = coords;
- }
-
@Override
public int hashCode() {
- final int prime = 31;
- int result = 1;
- long temp;
- temp = Double.doubleToLongBits(coords.getLatitude());
- result = prime * result + (int) (temp ^ (temp >>> 32));
- temp = Double.doubleToLongBits(coords.getLongitude());
- result = prime * result + (int) (temp ^ (temp >>> 32));
- return result;
+ return coords.hashCode();
}
@Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (!(obj instanceof cgDestination)) {
- return false;
- }
- cgDestination other = (cgDestination) obj;
- return coords.isEqualTo(other.coords);
+ public boolean equals(final Object obj) {
+ return obj != null && obj instanceof cgDestination && ((cgDestination) obj).coords.equals(coords);
}
public long getId() {
return id;
}
- public void setId(long id) {
- this.id = id;
- }
-
}
diff --git a/main/src/cgeo/geocaching/cgeoapplication.java b/main/src/cgeo/geocaching/cgeoapplication.java
index 6418d86..afb0ba0 100644
--- a/main/src/cgeo/geocaching/cgeoapplication.java
+++ b/main/src/cgeo/geocaching/cgeoapplication.java
@@ -350,8 +350,8 @@ public class cgeoapplication extends Application {
}
/** {@link cgData#saveSearchedDestination(cgDestination)} */
- public boolean saveSearchedDestination(cgDestination destination) {
- return storage.saveSearchedDestination(destination);
+ public void saveSearchedDestination(cgDestination destination) {
+ storage.saveSearchedDestination(destination);
}
/** {@link cgData#saveWaypoints(String, List, boolean)} */
diff --git a/main/src/cgeo/geocaching/cgeopoint.java b/main/src/cgeo/geocaching/cgeopoint.java
index d3e5fba..6242952 100644
--- a/main/src/cgeo/geocaching/cgeopoint.java
+++ b/main/src/cgeo/geocaching/cgeopoint.java
@@ -410,16 +410,14 @@ public class cgeopoint extends AbstractActivity {
private void addToHistory(final Geopoint coords) {
// Add locations to history
- cgDestination loc = new cgDestination();
- loc.setCoords(coords);
+ final cgDestination loc = new cgDestination(0, 0, coords);
- if (!getHistoryOfSearchedLocations().contains(loc))
- {
- loc.setDate(System.currentTimeMillis());
- getHistoryOfSearchedLocations().add(0, loc);
+ if (!getHistoryOfSearchedLocations().contains(loc)) {
+ final cgDestination updatedLoc = loc.withDate(System.currentTimeMillis());
+ getHistoryOfSearchedLocations().add(0, updatedLoc);
// Save location
- app.saveSearchedDestination(loc);
+ app.saveSearchedDestination(updatedLoc);
// Ensure to remove the footer
historyListView.removeFooterView(getEmptyHistoryFooter());