1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.gpx;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.Collections;
|
---|
7 | import java.util.HashMap;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.Map;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.Bounds;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Immutable GPX track.
|
---|
15 | * @since 2907
|
---|
16 | */
|
---|
17 | public class ImmutableGpxTrack extends WithAttributes implements GpxTrack {
|
---|
18 |
|
---|
19 | private final List<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.unmodifiableList(newSegments);
|
---|
37 | this.length = calculateLength();
|
---|
38 | this.bounds = calculateBounds();
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Constructs a new {@code ImmutableGpxTrack} from {@code GpxTrackSegment} objects.
|
---|
43 | * @param segments The segments to build the track from. Input is not deep-copied,
|
---|
44 | * which means the caller may reuse the same segments to build
|
---|
45 | * multiple ImmutableGpxTrack instances from. This should not be
|
---|
46 | * a problem, since this object cannot modify {@code this.segments}.
|
---|
47 | * @param attributes Attributes for the GpxTrack, the input map is copied.
|
---|
48 | * @since 13210
|
---|
49 | */
|
---|
50 | public ImmutableGpxTrack(List<GpxTrackSegment> segments, Map<String, Object> attributes) {
|
---|
51 | this.attr = Collections.unmodifiableMap(new HashMap<>(attributes));
|
---|
52 | this.segments = Collections.unmodifiableList(segments);
|
---|
53 | this.length = calculateLength();
|
---|
54 | this.bounds = calculateBounds();
|
---|
55 | }
|
---|
56 |
|
---|
57 | private double calculateLength() {
|
---|
58 | double result = 0.0; // in meters
|
---|
59 |
|
---|
60 | for (GpxTrackSegment trkseg : segments) {
|
---|
61 | result += trkseg.length();
|
---|
62 | }
|
---|
63 | return result;
|
---|
64 | }
|
---|
65 |
|
---|
66 | private Bounds calculateBounds() {
|
---|
67 | Bounds result = null;
|
---|
68 | for (GpxTrackSegment segment: segments) {
|
---|
69 | Bounds segBounds = segment.getBounds();
|
---|
70 | if (segBounds != null) {
|
---|
71 | if (result == null) {
|
---|
72 | result = new Bounds(segBounds);
|
---|
73 | } else {
|
---|
74 | result.extend(segBounds);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 | return result;
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Override
|
---|
82 | public Map<String, Object> getAttributes() {
|
---|
83 | return attr;
|
---|
84 | }
|
---|
85 |
|
---|
86 | @Override
|
---|
87 | public Bounds getBounds() {
|
---|
88 | return bounds == null ? null : new Bounds(bounds);
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | public double length() {
|
---|
93 | return length;
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public Collection<GpxTrackSegment> getSegments() {
|
---|
98 | return segments;
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | public int hashCode() {
|
---|
103 | return 31 * super.hashCode() + ((segments == null) ? 0 : segments.hashCode());
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override
|
---|
107 | public boolean equals(Object obj) {
|
---|
108 | if (this == obj)
|
---|
109 | return true;
|
---|
110 | if (!super.equals(obj))
|
---|
111 | return false;
|
---|
112 | if (getClass() != obj.getClass())
|
---|
113 | return false;
|
---|
114 | ImmutableGpxTrack other = (ImmutableGpxTrack) obj;
|
---|
115 | if (segments == null) {
|
---|
116 | if (other.segments != null)
|
---|
117 | return false;
|
---|
118 | } else if (!segments.equals(other.segments))
|
---|
119 | return false;
|
---|
120 | return true;
|
---|
121 | }
|
---|
122 | }
|
---|