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