001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data;
003
004import java.awt.geom.Area;
005import java.util.ArrayList;
006import java.util.Collection;
007import java.util.List;
008import java.util.Objects;
009
010import org.openstreetmap.josm.tools.CheckParameterUtil;
011
012/**
013 * A data source, defined by bounds and textual description for the origin.
014 * @since 247 (creation)
015 * @since 7575 (moved package)
016 */
017public class DataSource {
018
019    /**
020     * The bounds of this data source
021     */
022    public final Bounds bounds;
023
024    /**
025     * The textual description of the origin (example: "OpenStreetMap Server")
026     */
027    public final String origin;
028
029    /**
030     * Constructs a new {@code DataSource}.
031     * @param bounds The bounds of this data source
032     * @param origin The textual description of the origin (example: "OpenStreetMap Server")
033     * @throws IllegalArgumentException if bounds is {@code null}
034     */
035    public DataSource(Bounds bounds, String origin) {
036        CheckParameterUtil.ensureParameterNotNull(bounds, "bounds");
037        this.bounds = bounds;
038        this.origin = origin;
039    }
040
041    @Override
042    public int hashCode() {
043        return Objects.hash(bounds, origin);
044    }
045
046    @Override
047    public boolean equals(Object obj) {
048        if (this == obj) return true;
049        if (obj == null || getClass() != obj.getClass()) return false;
050        DataSource that = (DataSource) obj;
051        return Objects.equals(bounds, that.bounds) &&
052                Objects.equals(origin, that.origin);
053    }
054
055    @Override
056    public String toString() {
057        return "DataSource [bounds=" + bounds + ", origin=" + origin + ']';
058    }
059
060    /**
061     * Returns the total area of downloaded data (the "yellow rectangles").
062     * @param dataSources list of data sources
063     * @return Area object encompassing downloaded data.
064     * @see Data#getDataSourceArea()
065     */
066    public static Area getDataSourceArea(Collection<DataSource> dataSources) {
067        if (dataSources == null || dataSources.isEmpty()) {
068            return null;
069        }
070        Area a = new Area();
071        for (DataSource source : dataSources) {
072            // create area from data bounds
073            a.add(new Area(source.bounds.asRect()));
074        }
075        return a;
076    }
077
078    /**
079     * <p>Replies the list of data source bounds.</p>
080     *
081     * <p>Dataset maintains a list of data sources which have been merged into the
082     * data set. Each of these sources can optionally declare a bounding box of the
083     * data it supplied to the dataset.</p>
084     *
085     * <p>This method replies the list of defined (non {@code null}) bounding boxes.</p>
086     * @param dataSources list of data sources
087     *
088     * @return the list of data source bounds. An empty list, if no non-null data source
089     * bounds are defined.
090     * @see Data#getDataSourceBounds()
091     */
092    public static List<Bounds> getDataSourceBounds(Collection<DataSource> dataSources) {
093        if (dataSources == null) {
094            return null;
095        }
096        List<Bounds> ret = new ArrayList<>(dataSources.size());
097        for (DataSource ds : dataSources) {
098            if (ds.bounds != null) {
099                ret.add(ds.bounds);
100            }
101        }
102        return ret;
103    }
104}