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

Last change on this file since 9519 was 9371, checked in by simon04, 8 years ago

Java 7: use Objects.equals and Objects.hash

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data;
3
4import java.awt.geom.Area;
5import java.util.ArrayList;
6import java.util.Collection;
7import java.util.List;
8import java.util.Objects;
9
10import 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 */
17public 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 @Override
42 public int hashCode() {
43 return Objects.hash(bounds, origin);
44 }
45
46 @Override
47 public boolean equals(Object obj) {
48 if (this == obj) return true;
49 if (obj == null || getClass() != obj.getClass()) return false;
50 DataSource that = (DataSource) obj;
51 return Objects.equals(bounds, that.bounds) &&
52 Objects.equals(origin, that.origin);
53 }
54
55 @Override
56 public String toString() {
57 return "DataSource [bounds=" + bounds + ", origin=" + origin + ']';
58 }
59
60 /**
61 * Returns the total area of downloaded data (the "yellow rectangles").
62 * @param dataSources list of data sources
63 * @return Area object encompassing downloaded data.
64 * @see Data#getDataSourceArea()
65 */
66 public static Area getDataSourceArea(Collection<DataSource> dataSources) {
67 if (dataSources == null || dataSources.isEmpty()) {
68 return null;
69 }
70 Area a = new Area();
71 for (DataSource source : dataSources) {
72 // create area from data bounds
73 a.add(new Area(source.bounds.asRect()));
74 }
75 return a;
76 }
77
78 /**
79 * <p>Replies the list of data source bounds.</p>
80 *
81 * <p>Dataset maintains a list of data sources which have been merged into the
82 * data set. Each of these sources can optionally declare a bounding box of the
83 * data it supplied to the dataset.</p>
84 *
85 * <p>This method replies the list of defined (non {@code null}) bounding boxes.</p>
86 * @param dataSources list of data sources
87 *
88 * @return the list of data source bounds. An empty list, if no non-null data source
89 * bounds are defined.
90 * @see Data#getDataSourceBounds()
91 */
92 public static List<Bounds> getDataSourceBounds(Collection<DataSource> dataSources) {
93 if (dataSources == null) {
94 return null;
95 }
96 List<Bounds> ret = new ArrayList<>(dataSources.size());
97 for (DataSource ds : dataSources) {
98 if (ds.bounds != null) {
99 ret.add(ds.bounds);
100 }
101 }
102 return ret;
103 }
104}
Note: See TracBrowser for help on using the repository browser.