source: josm/trunk/src/org/openstreetmap/josm/data/gpx/ImmutableGpxTrack.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: 2.2 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;
7import java.util.HashMap;
8import java.util.List;
9import java.util.Map;
10
11import org.openstreetmap.josm.data.Bounds;
12
13public class ImmutableGpxTrack extends WithAttributes implements GpxTrack {
14
15 private final Collection<GpxTrackSegment> segments;
16 private final double length;
17 private final Bounds bounds;
18
19 public ImmutableGpxTrack(Collection<Collection<WayPoint>> trackSegs, Map<String, Object> attributes) {
20 List<GpxTrackSegment> newSegments = new ArrayList<>();
21 for (Collection<WayPoint> trackSeg: trackSegs) {
22 if (trackSeg != null && !trackSeg.isEmpty()) {
23 newSegments.add(new ImmutableGpxTrackSegment(trackSeg));
24 }
25 }
26 this.attr = Collections.unmodifiableMap(new HashMap<>(attributes));
27 this.segments = Collections.unmodifiableCollection(newSegments);
28 this.length = calculateLength();
29 this.bounds = calculateBounds();
30 }
31
32 private double calculateLength(){
33 double result = 0.0; // in meters
34
35 for (GpxTrackSegment trkseg : segments) {
36 result += trkseg.length();
37 }
38 return result;
39 }
40
41 private Bounds calculateBounds() {
42 Bounds result = null;
43 for (GpxTrackSegment segment: segments) {
44 Bounds segBounds = segment.getBounds();
45 if (segBounds != null) {
46 if (result == null) {
47 result = new Bounds(segBounds);
48 } else {
49 result.extend(segBounds);
50 }
51 }
52 }
53 return result;
54 }
55
56 @Override
57 public Map<String, Object> getAttributes() {
58 return attr;
59 }
60
61 @Override
62 public Bounds getBounds() {
63 if (bounds == null)
64 return null;
65 else
66 return new Bounds(bounds);
67 }
68
69 @Override
70 public double length() {
71 return length;
72 }
73
74 @Override
75 public Collection<GpxTrackSegment> getSegments() {
76 return segments;
77 }
78
79 @Override
80 public int getUpdateCount() {
81 return 0;
82 }
83}
Note: See TracBrowser for help on using the repository browser.