]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/deletebydate/DateInfoList.java
Version 19, May 2018
[GpsPrune.git] / tim / prune / function / deletebydate / DateInfoList.java
1 package tim.prune.function.deletebydate;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Date;
6 import java.util.List;
7
8 /**
9  * List of date info objects for use by the table model
10  */
11 public class DateInfoList
12 {
13         /** list of info about points according to date */
14         private List<DateInfo> _infoList = new ArrayList<DateInfo>();
15         /** previously used dateinfo object to reduce list searching */
16         private DateInfo _previousInfo = null;
17         /** true if the list has been sorted, false otherwise */
18         private boolean _hasBeenSorted = false;
19
20
21         /**
22          * Add a point to the corresponding dateinfo
23          * @param inDate date of current point, or null if no timestamp
24          */
25         public void addPoint(Date inDate)
26         {
27                 DateInfo currentInfo = null;
28                 if (_previousInfo != null && _previousInfo.isSameDate(inDate))
29                 {
30                         // found it
31                         currentInfo = _previousInfo;
32                 }
33                 else
34                 {
35                         // loop through list, to see if date already present
36                         for (DateInfo info : _infoList)
37                         {
38                                 if (info.isSameDate(inDate))
39                                 {
40                                         currentInfo = info;
41                                         break;
42                                 }
43                         }
44                         // create new info if necessary
45                         if (currentInfo == null)
46                         {
47                                 currentInfo = new DateInfo(inDate);
48                                 _infoList.add(currentInfo);
49                                 _hasBeenSorted = false;
50                         }
51                         _previousInfo = currentInfo;
52                 }
53                 // Now we've identified the current info or created a new one
54                 currentInfo.incrementCount();
55         }
56
57         /**
58          * Clear the whole list
59          */
60         public void clearAll()
61         {
62                 _infoList.clear();
63                 _previousInfo = null;
64                 _hasBeenSorted = true;
65         }
66
67         /**
68          * @return number of entries in the list, including dateless points
69          */
70         public int getNumEntries()
71         {
72                 return _infoList.size();
73         }
74
75         /**
76          * @return the total number of points found, which should match the track size
77          */
78         public int getTotalNumPoints()
79         {
80                 int total = 0;
81                 for (DateInfo info : _infoList) {
82                         total += info.getPointCount();
83                 }
84                 return total;
85         }
86
87         /**
88          * Sort the info list by ascending timestamps
89          */
90         private void sort()
91         {
92                 if (!_hasBeenSorted)
93                 {
94                         Collections.sort(_infoList);
95                         _hasBeenSorted = true;
96                 }
97         }
98
99         /**
100          * Get the DateInfo object at the given index
101          * @param inIndex index in (sorted) list
102          * @return corresponding object (may throw exception if out of range)
103          */
104         public DateInfo getDateInfo(int inIndex)
105         {
106                 sort();
107                 return _infoList.get(inIndex);
108         }
109 }