]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/deletebydate/DateInfoList.java
Version 17, September 2014
[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                 if (_previousInfo != null && _previousInfo.isSameDate(inDate))
28                 {
29                         // found it
30                         _previousInfo.incrementCount();
31                 }
32                 else
33                 {
34                         // loop through list, seeing if date already present
35                         boolean foundDate = false;
36                         for (DateInfo info : _infoList)
37                         {
38                                 if (info.isSameDate(inDate))
39                                 {
40                                         info.incrementCount();
41                                         _previousInfo = info;
42                                         foundDate = true;
43                                         break;
44                                 }
45                         }
46                         // create new info if necessary
47                         if (!foundDate)
48                         {
49                                 _previousInfo = new DateInfo(inDate);
50                                 _previousInfo.incrementCount();
51                                 _infoList.add(_previousInfo);
52                                 _hasBeenSorted = false;
53                         }
54                 }
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          * not used, can be removed
69          * @return true if any points without dates were found
70          */
71         public boolean hasDatelessPoints()
72         {
73                 if (_infoList.isEmpty()) {return false;}
74                 sort();
75                 DateInfo firstInfo = _infoList.get(0);
76                 return (firstInfo != null && firstInfo.isDateless() && firstInfo.getPointCount() > 0);
77         }
78
79         /**
80          * @return number of entries in the list, including dateless points
81          */
82         public int getNumEntries()
83         {
84                 return _infoList.size();
85         }
86
87         /**
88          * @return the total number of points found, which should match the track size
89          */
90         public int getTotalNumPoints()
91         {
92                 int total = 0;
93                 for (DateInfo info : _infoList) {
94                         total += info.getPointCount();
95                 }
96                 return total;
97         }
98
99         /**
100          * Sort the info list by ascending timestamps
101          */
102         private void sort()
103         {
104                 if (!_hasBeenSorted)
105                 {
106                         Collections.sort(_infoList);
107                         _hasBeenSorted = true;
108                 }
109         }
110
111         /**
112          * Get the DateInfo object at the given index
113          * @param inIndex index in (sorted) list
114          * @return corresponding object (may throw exception if out of range)
115          */
116         public DateInfo getDateInfo(int inIndex)
117         {
118                 sort();
119                 return _infoList.get(inIndex);
120         }
121 }