View Javadoc

1   package net.sf.tourviewer.lib;
2   
3   import java.util.ArrayList;
4   import java.util.Date;
5   import java.util.List;
6   
7   public class Tour implements Comparable {
8   
9   	private boolean autoStatistics = true;
10  	double avgAltitudeInc;
11  	double avgPulse;
12  	double avgSlope;
13  	double avgSpeed;
14  	double avgTemperature;
15  	private Bike bike;
16  	private long distance;
17  	private String endLocation;
18  	private Date endTime;
19  	private List<Marker> markers = new ArrayList<Marker>();
20  	int maxAltitude;
21      private String note;
22  	private List<TourRecord> records = new ArrayList<TourRecord>();
23  	private String startLocation;
24  	private Date startTime;
25  	private String title;
26  	int totalAltitudeDown;
27  	int totalAltitudeUp;
28  	private long travelTime;
29  	private Type type;
30  		
31  	public Tour() 
32  	{
33  	}
34  	
35  	public void addMarker(Marker marker)
36  	{
37  		markers.add(marker);
38  	}
39  
40  	public void addRecord(TourRecord record)
41  	{
42  		if (!records.isEmpty()) {
43  			TourRecord previous = records.get(records.size() - 1);
44  			previous.setNext(record);
45  			record.setPrevious(previous);
46  
47  			if (isAutoStatistics()) {
48  				maxAltitude = Math.max(maxAltitude, record.getAltitude());
49  				updateStatistics(previous, record);
50  			}
51  		}
52  		else if (isAutoStatistics()) {
53  			maxAltitude = record.getAltitude();
54  		}
55  		records.add(record);
56  	}
57  	
58  	public int compareTo(Object o)
59  	{
60  		return getStartTime().compareTo(((Tour)o).getStartTime());
61  	}
62  
63  	public double getAvgAltitudeInc()
64  	{
65  		return avgAltitudeInc;
66  	}
67  	
68  	public double getAvgPulse()
69  	{
70  		return avgPulse;
71  	}
72  
73  	public double getAvgSlope()
74  	{
75  		return avgSlope;
76  	}
77  
78  	public double getAvgSpeed()
79  	{
80  		return avgSpeed;
81  	}
82  
83  	public double getAvgTemperature()
84  	{
85  		return avgTemperature;
86  	}
87  	
88  	public Bike getBike()
89  	{
90  		return bike;
91  	}
92  	
93  	public long getDistance()
94  	{
95  		return distance;
96  	}
97  	
98  	public String getEndLocation()
99  	{
100 		return endLocation;
101 	}
102 	
103 	public Date getEndTime()
104 	{
105 		return endTime;
106 	}
107 	
108 	public int getMaxAltitude()
109 	{
110 		return maxAltitude;
111 	}
112 
113 	public String getNote()
114 	{
115 		return note;
116 	}
117 	
118 	public TourRecord[] getRecords() { 
119 		return records.toArray(new TourRecord[0]);
120 	}
121 
122 	public String getStartLocation()
123 	{
124 		return startLocation;
125 	}
126 	
127 	public Date getStartTime()
128 	{
129 		return startTime;
130 	}
131 	
132 	public String getTitle()
133 	{
134 		return title;
135 	}
136 
137 	public int getTotalAltitudeDown()
138 	{
139 		return totalAltitudeDown;
140 	}
141 
142 	
143 	public int getTotalAltitudeUp()
144 	{
145 		return totalAltitudeUp;
146 	}
147 
148 	public long getTravelTime()
149 	{
150 		return travelTime;
151 	}
152 
153 	
154 	public Type getType()
155 	{
156 		return type;
157 	}
158 	
159 	public boolean isAutoStatistics()
160 	{
161 		return autoStatistics;
162 	}
163 
164 	public void setAutoStatistics(boolean autoStatistics)
165 	{
166 		this.autoStatistics = autoStatistics;
167 	}
168 	
169 	public void setAvgAltitudeInc(double avgAltitudeInc)
170 	{
171 		this.avgAltitudeInc = avgAltitudeInc;
172 	}
173 	
174 	public void setAvgPulse(double avgPulse)
175 	{
176 		this.avgPulse = avgPulse;
177 	}
178 
179 	public void setAvgSlope(double avgSlope)
180 	{
181 		this.avgSlope = avgSlope;
182 	}
183 
184 	public void setAvgSpeed(double avgSpeed)
185 	{
186 		this.avgSpeed = avgSpeed;
187 	}
188 	
189 	public void setAvgTemperature(double avgTemperature)
190 	{
191 		this.avgTemperature = avgTemperature;
192 	}
193 
194 	public void setBike(Bike bike)
195 	{
196 		this.bike = bike;
197 	}
198 
199 	
200 	public void setDistance(long distance)
201 	{
202 		this.distance = distance;
203 	}
204 	
205 	public void setEndLocation(String endLocation)
206 	{
207 		this.endLocation = endLocation;
208 	}
209 	
210 	public void setEndTime(Date endTime)
211 	{
212 		this.endTime = endTime;
213 	}
214 	
215 	public void setMaxAltitude(int maxAltitude)
216 	{
217 		this.maxAltitude = maxAltitude;
218 	}
219 	
220 	public void setNote(String note)
221 	{
222 		this.note = note;
223 	}
224 	
225 	public void setStartLocation(String startLocation)
226 	{
227 		this.startLocation = startLocation;
228 	}
229 
230 	public void setStartTime(Date startTime)
231 	{
232 		this.startTime = startTime;
233 	}
234 	
235 	public void setTitle(String title)
236 	{
237 		this.title = title;
238 	}
239 	
240 	public void setTotalAltitudeDown(int totalAltitudeDown)
241 	{
242 		this.totalAltitudeDown = totalAltitudeDown;
243 	}
244 	
245 	public void setTotalAltitudeUp(int totalAltitudeUp)
246 	{
247 		this.totalAltitudeUp = totalAltitudeUp;
248 	}
249 
250 	public void setTravelTime(long travelTime)
251 	{
252 		this.travelTime = travelTime;
253 	}
254 	
255 	public void setType(Type type)
256 	{
257 		this.type = type;
258 	}
259 	
260 	@Override
261 	public String toString()
262 	{
263 		return getClass().getName() + String.format("[type=%s,distance=%d,startTime=%tF %tT,endTime=%tF %tT]",
264 				type, distance, startTime, startTime, endTime, endTime);
265 	}
266 
267 	
268 	private void updateStatistics(TourRecord previous, TourRecord current)
269 	{
270 		int altitudeDiff = current.getAltitude() - previous.getAltitude();
271 		avgAltitudeInc = current.getAltitude();
272 		if (altitudeDiff > 0) {
273 			totalAltitudeUp += altitudeDiff;
274 		}
275 		else {
276 			totalAltitudeDown += -altitudeDiff;
277 		}
278 
279 		long timeDiff = (current.getTime().getTime() - previous.getTime().getTime()) / 1000;
280 		long distanceDiff = current.getDistance() - previous.getDistance();
281 		if (distanceDiff != 0 && timeDiff > 0) {
282 			long newTravelTime = travelTime + timeDiff;
283 			double leftShare = travelTime / newTravelTime;
284 			double rightShare = timeDiff / newTravelTime;
285 			
286 			long slope = (altitudeDiff > 0) ? distanceDiff / altitudeDiff : 0;
287 			avgSlope = avgSlope * leftShare + slope * rightShare;
288 			double speed = (distanceDiff / timeDiff) * 3.6;
289 			avgSpeed = avgSpeed * leftShare + speed * rightShare; 
290 			avgTemperature = avgTemperature * leftShare + current.getTemperature() * rightShare;
291 			avgPulse = avgPulse * leftShare + current.getPulse() * rightShare;
292 			
293 			travelTime = newTravelTime;
294 		}
295 	}
296 
297 	
298 	public enum Type { BIKE, JOGGING, SKI, SKI_BIKE, OTHER }
299 
300 }