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    /**
042     * Cosntructs a new {@link DataSource}
043     * @param source The source to copy the data from.
044     * @since 10346
045     */
046    public DataSource(DataSource source) {
047        this(source.bounds, source.origin);
048    }
049
050    @Override
051    public int hashCode() {
052        return Objects.hash(bounds, origin);
053    }
054
055    @Override
056    public boolean equals(Object obj) {
057        if (this == obj) return true;
058        if (obj == null || getClass() != obj.getClass()) return false;
059        DataSource that = (DataSource) obj;
060        return Objects.equals(bounds, that.bounds) &&
061                Objects.equals(origin, that.origin);
062    }
063
064    @Override
065    public String toString() {
066        return "DataSource [bounds=" + bounds + ", origin=" + origin + ']';
067    }
068
069    /**
070     * Returns the total area of downloaded data (the "yellow rectangles").
071     * @param dataSources list of data sources
072     * @return Area object encompassing downloaded data.
073     * @see Data#getDataSourceArea()
074     */
075    public static Area getDataSourceArea(Collection<DataSource> dataSources) {
076        if (dataSources == null || dataSources.isEmpty()) {
077            return null;
078        }
079        Area a = new Area();
080        for (DataSource source : dataSources) {
081            // create area from data bounds
082            a.add(new Area(source.bounds.asRect()));
083        }
084        return a;
085    }
086
087    /**
088     * <p>Replies the list of data source bounds.</p>
089     *
090     * <p>Dataset maintains a list of data sources which have been merged into the
091     * data set. Each of these sources can optionally declare a bounding box of the
092     * data it supplied to the dataset.</p>
093     *
094     * <p>This method replies the list of defined (non {@code null}) bounding boxes.</p>
095     * @param dataSources list of data sources
096     *
097     * @return the list of data source bounds. An empty list, if no non-null data source
098     * bounds are defined.
099     * @see Data#getDataSourceBounds()
100     */
101    public static List<Bounds> getDataSourceBounds(Collection<DataSource> dataSources) {
102        if (dataSources == null) {
103            return null;
104        }
105        List<Bounds> ret = new ArrayList<>(dataSources.size());
106        for (DataSource ds : dataSources) {
107            if (ds.bounds != null) {
108                ret.add(ds.bounds);
109            }
110        }
111        return ret;
112    }
113}