1 package tim.prune.data.sort;
3 import java.util.Comparator;
5 import tim.prune.data.DataPoint;
9 * Class for comparing waypoints to sort them by name or timestamp
11 public class WaypointComparer implements Comparator<DataPoint>
14 private SortMode _sortMode;
19 * @param inMode sort mode
21 public WaypointComparer(SortMode inMode)
29 public int compare(DataPoint inP1, DataPoint inP2)
31 if (inP2 == null || !inP2.isWaypoint()) return -1; // all nulls at end
32 if (inP1 == null || !inP1.isWaypoint()) return 1;
34 // Sort by time, if requested
36 if (_sortMode == SortMode.SORTBY_TIME) {
37 result = compareTimes(inP1, inP2);
39 // check names if names requested or if times didn't work
41 result = inP1.getWaypointName().compareTo(inP2.getWaypointName());
43 // names and times equal, try longitude
45 result = inP1.getLongitude().getDouble() > inP2.getLongitude().getDouble() ? 1 : -1;
49 result = inP1.getLatitude().getDouble() > inP2.getLatitude().getDouble() ? 1 : -1;
55 * Compare the timestamps of the two waypoints
56 * @param inP1 first point
57 * @param inP2 second point
58 * @return compare value (-1,0,1)
60 private int compareTimes(DataPoint inP1, DataPoint inP2)
62 // Points might not have timestamps
63 if (inP1.hasTimestamp() && !inP2.hasTimestamp()) return 1;
64 if (!inP1.hasTimestamp() && inP2.hasTimestamp()) return -1;
65 if (inP1.hasTimestamp() && inP2.hasTimestamp())
67 // Compare the timestamps
68 long secDiff = inP1.getTimestamp().getMillisecondsSince(inP2.getTimestamp());
69 return (secDiff<0?-1:(secDiff==0?0:1));
71 // neither has a timestamp