X-Git-Url: http://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=src%2Ftim%2Fprune%2Fdata%2Fsort%2FWaypointComparer.java;fp=src%2Ftim%2Fprune%2Fdata%2Fsort%2FWaypointComparer.java;h=b1c3e2ad9e1919e33016d2b0b0efd8d3eeece986;hp=0000000000000000000000000000000000000000;hb=ce6f2161b8596f7018d6a76bff79bc9e571f35fd;hpb=2d8cb72e84d5cc1089ce77baf1e34ea3ea2f8465 diff --git a/src/tim/prune/data/sort/WaypointComparer.java b/src/tim/prune/data/sort/WaypointComparer.java new file mode 100644 index 0000000..b1c3e2a --- /dev/null +++ b/src/tim/prune/data/sort/WaypointComparer.java @@ -0,0 +1,74 @@ +package tim.prune.data.sort; + +import java.util.Comparator; + +import tim.prune.data.DataPoint; + + +/** + * Class for comparing waypoints to sort them by name or timestamp + */ +public class WaypointComparer implements Comparator +{ + /** Sort mode */ + private SortMode _sortMode; + + + /** + * Constructor + * @param inMode sort mode + */ + public WaypointComparer(SortMode inMode) + { + _sortMode = inMode; + } + + /** + * Main compare method + */ + public int compare(DataPoint inP1, DataPoint inP2) + { + if (inP2 == null || !inP2.isWaypoint()) return -1; // all nulls at end + if (inP1 == null || !inP1.isWaypoint()) return 1; + + // Sort by time, if requested + int result = 0; + if (_sortMode == SortMode.SORTBY_TIME) { + result = compareTimes(inP1, inP2); + } + // check names if names requested or if times didn't work + if (result == 0) { + result = inP1.getWaypointName().compareTo(inP2.getWaypointName()); + } + // names and times equal, try longitude + if (result == 0) { + result = inP1.getLongitude().getDouble() > inP2.getLongitude().getDouble() ? 1 : -1; + } + // and latitude + if (result == 0) { + result = inP1.getLatitude().getDouble() > inP2.getLatitude().getDouble() ? 1 : -1; + } + return result; + } + + /** + * Compare the timestamps of the two waypoints + * @param inP1 first point + * @param inP2 second point + * @return compare value (-1,0,1) + */ + private int compareTimes(DataPoint inP1, DataPoint inP2) + { + // Points might not have timestamps + if (inP1.hasTimestamp() && !inP2.hasTimestamp()) return 1; + if (!inP1.hasTimestamp() && inP2.hasTimestamp()) return -1; + if (inP1.hasTimestamp() && inP2.hasTimestamp()) + { + // Compare the timestamps + long secDiff = inP1.getTimestamp().getMillisecondsSince(inP2.getTimestamp()); + return (secDiff<0?-1:(secDiff==0?0:1)); + } + // neither has a timestamp + return 0; + } +}