001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.gpx;
003
004import java.util.Collection;
005import java.util.Iterator;
006import java.util.Map;
007import java.util.Objects;
008
009/**
010 * Line represents a linear collection of GPX waypoints with the ordered/unordered distinction.
011 * @since 14451
012 */
013public class Line implements Collection<WayPoint> {
014    private final Collection<WayPoint> waypoints;
015    private final boolean unordered;
016
017    /**
018     * Constructs a new {@code Line}.
019     * @param waypoints collection of waypoints
020     * @param attributes track/route attributes
021     */
022    public Line(Collection<WayPoint> waypoints, Map<String, Object> attributes) {
023        this.waypoints = Objects.requireNonNull(waypoints);
024        unordered = attributes.isEmpty() && waypoints.stream().allMatch(x -> x.get(GpxConstants.PT_TIME) == null);
025    }
026
027    /**
028     * Constructs a new {@code Line}.
029     * @param trackSegment track segment
030     * @param trackAttributes track attributes
031     */
032    public Line(GpxTrackSegment trackSegment, Map<String, Object> trackAttributes) {
033        this(trackSegment.getWayPoints(), trackAttributes);
034    }
035
036    /**
037     * Constructs a new {@code Line}.
038     * @param route route
039     */
040    public Line(GpxRoute route) {
041        this(route.routePoints, route.attr);
042    }
043
044    /**
045     * Determines if waypoints are ordered.
046     * @return {@code true} if waypoints are ordered
047     */
048    public boolean isUnordered() {
049        return unordered;
050    }
051
052    @Override
053    public int size() {
054        return waypoints.size();
055    }
056
057    @Override
058    public boolean isEmpty() {
059        return waypoints.isEmpty();
060    }
061
062    @Override
063    public boolean contains(Object o) {
064        return waypoints.contains(o);
065    }
066
067    @Override
068    public Iterator<WayPoint> iterator() {
069        return waypoints.iterator();
070    }
071
072    @Override
073    public Object[] toArray() {
074        return waypoints.toArray();
075    }
076
077    @Override
078    public <T> T[] toArray(T[] a) {
079        return waypoints.toArray(a);
080    }
081
082    @Override
083    public boolean add(WayPoint e) {
084        return waypoints.add(e);
085    }
086
087    @Override
088    public boolean remove(Object o) {
089        return waypoints.remove(o);
090    }
091
092    @Override
093    public boolean containsAll(Collection<?> c) {
094        return waypoints.containsAll(c);
095    }
096
097    @Override
098    public boolean addAll(Collection<? extends WayPoint> c) {
099        return waypoints.addAll(c);
100    }
101
102    @Override
103    public boolean removeAll(Collection<?> c) {
104        return waypoints.removeAll(c);
105    }
106
107    @Override
108    public boolean retainAll(Collection<?> c) {
109        return waypoints.retainAll(c);
110    }
111
112    @Override
113    public void clear() {
114        waypoints.clear();
115    }
116}