source: josm/trunk/src/org/openstreetmap/josm/data/gpx/ImmutableGpxTrack.java@ 10212

Last change on this file since 10212 was 9949, checked in by Don-vip, 8 years ago

remove deprecated stuff, code cleanup, javadoc, unit tests

  • Property svn:eol-style set to native
File size: 2.4 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
13/**
14 * Immutable GPX track.
15 * @since 2907
16 */
17public class ImmutableGpxTrack extends WithAttributes implements GpxTrack {
18
19 private final Collection<GpxTrackSegment> segments;
20 private final double length;
21 private final Bounds bounds;
22
23 /**
24 * Constructs a new {@code ImmutableGpxTrack}.
25 * @param trackSegs track segments
26 * @param attributes track attributes
27 */
28 public ImmutableGpxTrack(Collection<Collection<WayPoint>> trackSegs, Map<String, Object> attributes) {
29 List<GpxTrackSegment> newSegments = new ArrayList<>();
30 for (Collection<WayPoint> trackSeg: trackSegs) {
31 if (trackSeg != null && !trackSeg.isEmpty()) {
32 newSegments.add(new ImmutableGpxTrackSegment(trackSeg));
33 }
34 }
35 this.attr = Collections.unmodifiableMap(new HashMap<>(attributes));
36 this.segments = Collections.unmodifiableCollection(newSegments);
37 this.length = calculateLength();
38 this.bounds = calculateBounds();
39 }
40
41 private double calculateLength() {
42 double result = 0.0; // in meters
43
44 for (GpxTrackSegment trkseg : segments) {
45 result += trkseg.length();
46 }
47 return result;
48 }
49
50 private Bounds calculateBounds() {
51 Bounds result = null;
52 for (GpxTrackSegment segment: segments) {
53 Bounds segBounds = segment.getBounds();
54 if (segBounds != null) {
55 if (result == null) {
56 result = new Bounds(segBounds);
57 } else {
58 result.extend(segBounds);
59 }
60 }
61 }
62 return result;
63 }
64
65 @Override
66 public Map<String, Object> getAttributes() {
67 return attr;
68 }
69
70 @Override
71 public Bounds getBounds() {
72 if (bounds == null)
73 return null;
74 else
75 return new Bounds(bounds);
76 }
77
78 @Override
79 public double length() {
80 return length;
81 }
82
83 @Override
84 public Collection<GpxTrackSegment> getSegments() {
85 return segments;
86 }
87
88 @Override
89 public int getUpdateCount() {
90 return 0;
91 }
92}
Note: See TracBrowser for help on using the repository browser.