source: josm/trunk/src/org/openstreetmap/josm/data/gpx/ImmutableGpxTrackSegment.java@ 8308

Last change on this file since 8308 was 7005, checked in by Don-vip, 10 years ago

see #8465 - use diamond operator where applicable

  • Property svn:eol-style set to native
File size: 1.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.gpx;
3
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.Collections;
7
8import org.openstreetmap.josm.data.Bounds;
9
10public class ImmutableGpxTrackSegment implements GpxTrackSegment {
11
12 private final Collection<WayPoint> wayPoints;
13 private final Bounds bounds;
14 private final double length;
15
16 public ImmutableGpxTrackSegment(Collection<WayPoint> wayPoints) {
17 this.wayPoints = Collections.unmodifiableCollection(new ArrayList<>(wayPoints));
18 this.bounds = calculateBounds();
19 this.length = calculateLength();
20 }
21
22 private Bounds calculateBounds() {
23 Bounds result = null;
24 for (WayPoint wpt: wayPoints) {
25 if (result == null) {
26 result = new Bounds(wpt.getCoor());
27 } else {
28 result.extend(wpt.getCoor());
29 }
30 }
31 return result;
32 }
33
34 private double calculateLength() {
35 double result = 0.0; // in meters
36 WayPoint last = null;
37 for (WayPoint tpt : wayPoints) {
38 if(last != null){
39 Double d = last.getCoor().greatCircleDistance(tpt.getCoor());
40 if(!d.isNaN() && !d.isInfinite()) {
41 result += d;
42 }
43 }
44 last = tpt;
45 }
46 return result;
47 }
48
49 @Override
50 public Bounds getBounds() {
51 if (bounds == null)
52 return null;
53 else
54 return new Bounds(bounds);
55 }
56
57 @Override
58 public Collection<WayPoint> getWayPoints() {
59 return wayPoints;
60 }
61
62 @Override
63 public double length() {
64 return length;
65 }
66
67 @Override
68 public int getUpdateCount() {
69 return 0;
70 }
71
72}
Note: See TracBrowser for help on using the repository browser.