]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/autoplay/PointInfo.java
Moved source into separate src directory due to popular request
[GpsPrune.git] / src / tim / prune / function / autoplay / PointInfo.java
1 package tim.prune.function.autoplay;
2
3 import tim.prune.data.DataPoint;
4 import tim.prune.data.Timestamp;
5
6 /**
7  * Holds the information about a single point required for the sorting
8  */
9 public class PointInfo implements Comparable<PointInfo>
10 {
11         /** Timestamp of the point, if any */
12         private Timestamp _timestamp  = null;
13         /** Point index in the track */
14         private int       _pointIndex = 0;
15         /** Segment flag of point */
16         private boolean   _segmentFlag = false;
17
18
19         /**
20          * Constructor
21          * @param inPoint point from track
22          * @param inIndex index of point in track
23          */
24         public PointInfo(DataPoint inPoint, int inIndex)
25         {
26                 if (inPoint.hasTimestamp())
27                 {
28                         _timestamp = inPoint.getTimestamp();
29                 }
30                 else if (inPoint.getPhoto() != null && inPoint.getPhoto().hasTimestamp())
31                 {
32                         _timestamp = inPoint.getPhoto().getTimestamp();
33                 }
34                 _pointIndex = inIndex;
35                 _segmentFlag = inPoint.getSegmentStart();
36         }
37
38         /** @return timestamp */
39         public Timestamp getTimestamp() {
40                 return _timestamp;
41         }
42
43         /** @return point index */
44         public int getIndex() {
45                 return _pointIndex;
46         }
47
48         /** @return segment flag */
49         public boolean getSegmentFlag() {
50                 return _segmentFlag;
51         }
52
53         /**
54          * Sort two objects by timestamp and if times equal then by point index
55          */
56         public int compareTo(PointInfo inOther)
57         {
58                 long timeDiff = 0;
59                 final boolean thisHasTime = (_timestamp != null);
60                 final boolean otherHasTime = (inOther._timestamp != null);
61                 if (thisHasTime && otherHasTime)
62                 {
63                         timeDiff = _timestamp.getMillisecondsSince(inOther._timestamp);
64                 }
65                 else if (thisHasTime)
66                 {
67                         timeDiff = -1; // points without time to the end
68                 }
69                 else if (otherHasTime)
70                 {
71                         timeDiff = 1;
72                 }
73                 // If the times are equal (or both missing) then use the point index
74                 if (timeDiff == 0) {
75                         return _pointIndex - inOther._pointIndex;
76                 }
77                 // Otherwise, compare by time
78                 return (timeDiff < 0 ? -1 : 1);
79         }
80
81         @Override
82         public boolean equals(Object inOther)
83         {
84                 if (inOther == null) return false;
85                 try
86                 {
87                         PointInfo other = (PointInfo) inOther;
88                         if (_pointIndex != other._pointIndex) return false;
89                         final boolean thisHasTime = (_timestamp != null);
90                         final boolean otherHasTime = (other._timestamp != null);
91                         if (thisHasTime != otherHasTime) {return false;}
92                         if (!thisHasTime && !otherHasTime) {return true;}
93                         return _timestamp.isEqual(other._timestamp);
94                 }
95                 catch (ClassCastException cce) {}
96                 return false;
97         }
98 }