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