1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data;
|
---|
3 |
|
---|
4 | import java.awt.geom.Area;
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.Collection;
|
---|
7 | import java.util.List;
|
---|
8 | import java.util.Objects;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * A data source, defined by bounds and textual description for the origin.
|
---|
14 | * @since 247 (creation)
|
---|
15 | * @since 7575 (moved package)
|
---|
16 | */
|
---|
17 | public class DataSource {
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * The bounds of this data source
|
---|
21 | */
|
---|
22 | public final Bounds bounds;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * The textual description of the origin (example: "OpenStreetMap Server")
|
---|
26 | */
|
---|
27 | public final String origin;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Constructs a new {@code DataSource}.
|
---|
31 | * @param bounds The bounds of this data source
|
---|
32 | * @param origin The textual description of the origin (example: "OpenStreetMap Server")
|
---|
33 | * @throws IllegalArgumentException if bounds is {@code null}
|
---|
34 | */
|
---|
35 | public DataSource(Bounds bounds, String origin) {
|
---|
36 | CheckParameterUtil.ensureParameterNotNull(bounds, "bounds");
|
---|
37 | this.bounds = bounds;
|
---|
38 | this.origin = origin;
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Cosntructs a new {@link DataSource}
|
---|
43 | * @param source The source to copy the data from.
|
---|
44 | * @since 10346
|
---|
45 | */
|
---|
46 | public DataSource(DataSource source) {
|
---|
47 | this(source.bounds, source.origin);
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public int hashCode() {
|
---|
52 | return Objects.hash(bounds, origin);
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public boolean equals(Object obj) {
|
---|
57 | if (this == obj) return true;
|
---|
58 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
59 | DataSource that = (DataSource) obj;
|
---|
60 | return Objects.equals(bounds, that.bounds) &&
|
---|
61 | Objects.equals(origin, that.origin);
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public String toString() {
|
---|
66 | return "DataSource [bounds=" + bounds + ", origin=" + origin + ']';
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Returns the total area of downloaded data (the "yellow rectangles").
|
---|
71 | * @param dataSources list of data sources
|
---|
72 | * @return Area object encompassing downloaded data.
|
---|
73 | * @see Data#getDataSourceArea()
|
---|
74 | */
|
---|
75 | public static Area getDataSourceArea(Collection<DataSource> dataSources) {
|
---|
76 | if (dataSources == null || dataSources.isEmpty()) {
|
---|
77 | return null;
|
---|
78 | }
|
---|
79 | Area a = new Area();
|
---|
80 | for (DataSource source : dataSources) {
|
---|
81 | // create area from data bounds
|
---|
82 | a.add(new Area(source.bounds.asRect()));
|
---|
83 | }
|
---|
84 | return a;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * <p>Replies the list of data source bounds.</p>
|
---|
89 | *
|
---|
90 | * <p>Dataset maintains a list of data sources which have been merged into the
|
---|
91 | * data set. Each of these sources can optionally declare a bounding box of the
|
---|
92 | * data it supplied to the dataset.</p>
|
---|
93 | *
|
---|
94 | * <p>This method replies the list of defined (non {@code null}) bounding boxes.</p>
|
---|
95 | * @param dataSources list of data sources
|
---|
96 | *
|
---|
97 | * @return the list of data source bounds. An empty list, if no non-null data source
|
---|
98 | * bounds are defined.
|
---|
99 | * @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 | }
|
---|