source: josm/trunk/src/org/openstreetmap/josm/data/DataSource.java@ 12193

Last change on this file since 12193 was 11634, checked in by Don-vip, 7 years ago

see #10638 - apply also optimization for initial DataSource area computation (patch by GerdP)

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