source: josm/trunk/src/org/openstreetmap/josm/data/osm/Changeset.java@ 8512

Last change on this file since 8512 was 8512, checked in by Don-vip, 9 years ago

checkstyle: redundant modifiers

  • Property svn:eol-style set to native
File size: 9.6 KB
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[625]2package org.openstreetmap.josm.data.osm;
3
[7704]4import java.util.ArrayList;
[2115]5import java.util.Collection;
[7704]6import java.util.Collections;
[2115]7import java.util.Date;
8import java.util.HashMap;
[7704]9import java.util.List;
[2115]10import java.util.Map;
[2081]11
[2613]12import org.openstreetmap.josm.data.Bounds;
[2115]13import org.openstreetmap.josm.data.coor.LatLon;
[1990]14import org.openstreetmap.josm.data.osm.visitor.Visitor;
[7995]15import org.openstreetmap.josm.tools.CheckParameterUtil;
[1990]16
[625]17/**
18 * Represents a single changeset in JOSM. For now its only used during
19 * upload but in the future we may do more.
[8130]20 * @since 625
[625]21 */
[2115]22public final class Changeset implements Tagged {
[6069]23
[7995]24 /** The maximum changeset tag length allowed by API 0.6 **/
25 public static final int MAX_CHANGESET_TAG_LENGTH = 255;
[6069]26
[2115]27 /** the changeset id */
[2604]28 private int id;
[2115]29 /** the user who owns the changeset */
30 private User user;
31 /** date this changeset was created at */
32 private Date createdAt;
33 /** the date this changeset was closed at*/
34 private Date closedAt;
35 /** indicates whether this changeset is still open or not */
36 private boolean open;
37 /** the min. coordinates of the bounding box of this changeset */
38 private LatLon min;
39 /** the max. coordinates of the bounding box of this changeset */
40 private LatLon max;
[7700]41 /** the number of comments for this changeset */
42 private int commentsCount;
[2115]43 /** the map of tags */
[8510]44 private Map<String, String> tags;
[7704]45 /** indicates whether this changeset is incomplete. For an incomplete changeset we only know its id */
[2115]46 private boolean incomplete;
[2686]47 /** the changeset content */
48 private ChangesetDataSet content = null;
[7704]49 /** the changeset discussion */
50 private List<ChangesetDiscussionComment> discussion = null;
[1169]51
52 /**
[2115]53 * Creates a new changeset with id 0.
[1169]54 */
[2081]55 public Changeset() {
[5996]56 this(0);
[2081]57 }
58
[2115]59 /**
[6830]60 * Creates a changeset with id <code>id</code>. If id &gt; 0, sets incomplete to true.
[2305]61 *
[2115]62 * @param id the id
63 */
[2604]64 public Changeset(int id) {
[2115]65 this.id = id;
66 this.incomplete = id > 0;
[7005]67 this.tags = new HashMap<>();
[2081]68 }
69
[2115]70 /**
71 * Creates a clone of <code>other</code>
[2305]72 *
[2115]73 * @param other the other changeset. If null, creates a new changeset with id 0.
74 */
75 public Changeset(Changeset other) {
76 if (other == null) {
77 this.id = 0;
[7005]78 this.tags = new HashMap<>();
[2115]79 } else if (other.isIncomplete()) {
80 setId(other.getId());
81 this.incomplete = true;
[7005]82 this.tags = new HashMap<>();
[2115]83 } else {
[2613]84 this.id = other.id;
85 mergeFrom(other);
[2115]86 this.incomplete = false;
87 }
[2081]88 }
89
[1523]90 public void visit(Visitor v) {
91 v.visit(this);
[1169]92 }
[625]93
[2115]94 public int compareTo(Changeset other) {
[8365]95 return Integer.compare(getId(), other.getId());
[1169]96 }
[1933]97
98 public String getName() {
[1990]99 // no translation
[2070]100 return "changeset " + getId();
[1990]101 }
102
103 public String getDisplayName(NameFormatter formatter) {
104 return formatter.format(this);
105 }
[2081]106
[2604]107 public int getId() {
[2115]108 return id;
109 }
[2081]110
[2604]111 public void setId(int id) {
[2115]112 this.id = id;
[2081]113 }
[2115]114
115 public User getUser() {
116 return user;
117 }
118
119 public void setUser(User user) {
120 this.user = user;
121 }
122
123 public Date getCreatedAt() {
124 return createdAt;
125 }
126
127 public void setCreatedAt(Date createdAt) {
128 this.createdAt = createdAt;
129 }
130
131 public Date getClosedAt() {
132 return closedAt;
133 }
134
135 public void setClosedAt(Date closedAt) {
136 this.closedAt = closedAt;
137 }
138
139 public boolean isOpen() {
140 return open;
141 }
142
143 public void setOpen(boolean open) {
144 this.open = open;
145 }
146
147 public LatLon getMin() {
148 return min;
149 }
150
151 public void setMin(LatLon min) {
152 this.min = min;
153 }
154
155 public LatLon getMax() {
156 return max;
157 }
158
[2613]159 public Bounds getBounds() {
160 if (min != null && max != null)
[8510]161 return new Bounds(min, max);
[2613]162 return null;
163 }
164
[2115]165 public void setMax(LatLon max) {
166 this.max = max;
167 }
168
[7700]169 /**
170 * Replies the number of comments for this changeset.
171 * @return the number of comments for this changeset
172 * @since 7700
173 */
[8512]174 public int getCommentsCount() {
[7700]175 return commentsCount;
176 }
177
178 /**
179 * Sets the number of comments for this changeset.
180 * @param commentsCount the number of comments for this changeset
181 * @since 7700
182 */
[8512]183 public void setCommentsCount(int commentsCount) {
[7700]184 this.commentsCount = commentsCount;
185 }
186
[5890]187 @Override
[2115]188 public Map<String, String> getKeys() {
189 return tags;
190 }
191
[5890]192 @Override
[2115]193 public void setKeys(Map<String, String> keys) {
[7995]194 CheckParameterUtil.ensureParameterNotNull(keys, "keys");
195 for (String value : keys.values()) {
196 if (value != null && value.length() > MAX_CHANGESET_TAG_LENGTH) {
197 throw new IllegalArgumentException("Changeset tag value is too long: "+value);
198 }
199 }
[2115]200 this.tags = keys;
201 }
202
203 public boolean isIncomplete() {
204 return incomplete;
205 }
206
207 public void setIncomplete(boolean incomplete) {
208 this.incomplete = incomplete;
209 }
210
[5890]211 @Override
[2115]212 public void put(String key, String value) {
[7995]213 CheckParameterUtil.ensureParameterNotNull(key, "key");
214 if (value != null && value.length() > MAX_CHANGESET_TAG_LENGTH) {
215 throw new IllegalArgumentException("Changeset tag value is too long: "+value);
216 }
[2115]217 this.tags.put(key, value);
218 }
219
[5890]220 @Override
[2115]221 public String get(String key) {
222 return this.tags.get(key);
223 }
224
[5890]225 @Override
[2115]226 public void remove(String key) {
227 this.tags.remove(key);
228 }
229
[5890]230 @Override
[2305]231 public void removeAll() {
232 this.tags.clear();
233 }
234
[2115]235 public boolean hasEqualSemanticAttributes(Changeset other) {
236 if (other == null)
237 return false;
238 if (closedAt == null) {
239 if (other.closedAt != null)
240 return false;
241 } else if (!closedAt.equals(other.closedAt))
242 return false;
243 if (createdAt == null) {
244 if (other.createdAt != null)
245 return false;
246 } else if (!createdAt.equals(other.createdAt))
247 return false;
248 if (id != other.id)
249 return false;
250 if (max == null) {
251 if (other.max != null)
252 return false;
253 } else if (!max.equals(other.max))
254 return false;
255 if (min == null) {
256 if (other.min != null)
257 return false;
258 } else if (!min.equals(other.min))
259 return false;
260 if (open != other.open)
261 return false;
262 if (tags == null) {
263 if (other.tags != null)
264 return false;
265 } else if (!tags.equals(other.tags))
266 return false;
267 if (user == null) {
268 if (other.user != null)
269 return false;
270 } else if (!user.equals(other.user))
271 return false;
[7700]272 if (commentsCount != other.commentsCount) {
273 return false;
274 }
[2115]275 return true;
276 }
277
278 @Override
279 public int hashCode() {
280 if (id > 0)
[2676]281 return id;
[2613]282 else
283 return super.hashCode();
[2115]284 }
285
286 @Override
287 public boolean equals(Object obj) {
288 if (this == obj)
289 return true;
290 if (obj == null)
291 return false;
292 if (getClass() != obj.getClass())
293 return false;
294 Changeset other = (Changeset) obj;
295 if (this.id > 0 && other.id == this.id)
296 return true;
[2613]297 return this == obj;
[2115]298 }
299
[5890]300 @Override
[2115]301 public boolean hasKeys() {
302 return !tags.keySet().isEmpty();
303 }
304
[5890]305 @Override
[2115]306 public Collection<String> keySet() {
307 return tags.keySet();
308 }
[2598]309
310 public boolean isNew() {
311 return id <= 0;
312 }
[2604]313
314 public void mergeFrom(Changeset other) {
315 if (other == null)
316 return;
317 if (id != other.id)
318 return;
319 this.user = other.user;
320 this.createdAt = other.createdAt;
321 this.closedAt = other.closedAt;
322 this.open = other.open;
323 this.min = other.min;
324 this.max = other.max;
[7700]325 this.commentsCount = other.commentsCount;
[7005]326 this.tags = new HashMap<>(other.tags);
[2613]327 this.incomplete = other.incomplete;
[7715]328 this.discussion = other.discussion != null ? new ArrayList<>(other.discussion) : null;
[2686]329
330 // FIXME: merging of content required?
331 this.content = other.content;
[2604]332 }
[2686]333
334 public boolean hasContent() {
335 return content != null;
336 }
337
338 public ChangesetDataSet getContent() {
339 return content;
340 }
341
342 public void setContent(ChangesetDataSet content) {
343 this.content = content;
344 }
[7704]345
346 /**
347 * Replies the list of comments in the changeset discussion, if any.
348 * @return the list of comments in the changeset discussion. May be empty but never null
349 * @since 7704
350 */
[8512]351 public synchronized List<ChangesetDiscussionComment> getDiscussion() {
[7704]352 if (discussion == null) {
353 return Collections.emptyList();
354 }
355 return new ArrayList<>(discussion);
356 }
357
358 /**
359 * Adds a comment to the changeset discussion.
360 * @param comment the comment to add. Ignored if null
361 * @since 7704
362 */
[8512]363 public synchronized void addDiscussionComment(ChangesetDiscussionComment comment) {
[7704]364 if (comment == null) {
365 return;
366 }
367 if (discussion == null) {
368 discussion = new ArrayList<>();
369 }
370 discussion.add(comment);
371 }
[625]372}
Note: See TracBrowser for help on using the repository browser.