001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.osm;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.time.Instant;
007import java.util.ArrayList;
008import java.util.Collection;
009import java.util.Collections;
010import java.util.HashMap;
011import java.util.List;
012import java.util.Map;
013import java.util.Objects;
014import java.util.Optional;
015
016import org.openstreetmap.josm.data.Bounds;
017import org.openstreetmap.josm.data.coor.LatLon;
018import org.openstreetmap.josm.tools.CheckParameterUtil;
019import org.openstreetmap.josm.tools.Utils;
020
021/**
022 * Represents a single changeset in JOSM. For now its only used during
023 * upload but in the future we may do more.
024 * @since 625
025 */
026public final class Changeset implements Tagged, Comparable<Changeset> {
027
028    /** The maximum changeset tag length allowed by API 0.6 **/
029    public static final int MAX_CHANGESET_TAG_LENGTH = MAX_TAG_LENGTH;
030
031    /** the changeset id */
032    private int id;
033    /** the user who owns the changeset */
034    private User user;
035    /** date this changeset was created at */
036    private Instant createdAt;
037    /** the date this changeset was closed at*/
038    private Instant closedAt;
039    /** indicates whether this changeset is still open or not */
040    private boolean open;
041    /** the min. coordinates of the bounding box of this changeset */
042    private LatLon min;
043    /** the max. coordinates of the bounding box of this changeset */
044    private LatLon max;
045    /** the number of comments for this changeset */
046    private int commentsCount;
047    /** the number of changes for this changeset */
048    private int changesCount;
049    /** the map of tags */
050    private Map<String, String> tags;
051    /** indicates whether this changeset is incomplete. For an incomplete changeset we only know its id */
052    private boolean incomplete;
053    /** the changeset content */
054    private ChangesetDataSet content;
055    /** the changeset discussion */
056    private List<ChangesetDiscussionComment> discussion;
057
058    /**
059     * Creates a new changeset with id 0.
060     */
061    public Changeset() {
062        this(0);
063    }
064
065    /**
066     * Creates a changeset with id <code>id</code>. If id &gt; 0, sets incomplete to true.
067     *
068     * @param id the id
069     */
070    public Changeset(int id) {
071        this.id = id;
072        this.incomplete = id > 0;
073        this.tags = new HashMap<>();
074    }
075
076    /**
077     * Creates a clone of <code>other</code>
078     *
079     * @param other the other changeset. If null, creates a new changeset with id 0.
080     */
081    public Changeset(Changeset other) {
082        if (other == null) {
083            this.id = 0;
084            this.tags = new HashMap<>();
085        } else if (other.isIncomplete()) {
086            setId(other.getId());
087            this.incomplete = true;
088            this.tags = new HashMap<>();
089        } else {
090            this.id = other.id;
091            mergeFrom(other);
092            this.incomplete = false;
093        }
094    }
095
096    /**
097     * Creates a changeset with the data obtained from the given preset, i.e.,
098     * the {@link AbstractPrimitive#getChangesetId() changeset id}, {@link AbstractPrimitive#getUser() user}, and
099     * {@link AbstractPrimitive#getInstant() timestamp}.
100     * @param primitive the primitive to use
101     * @return the created changeset
102     */
103    public static Changeset fromPrimitive(final OsmPrimitive primitive) {
104        final Changeset changeset = new Changeset(primitive.getChangesetId());
105        changeset.setUser(primitive.getUser());
106        changeset.setCreatedAt(primitive.getInstant()); // not accurate in all cases
107        return changeset;
108    }
109
110    /**
111     * Compares this changeset to another, based on their identifier.
112     * @param other other changeset
113     * @return the value {@code 0} if {@code getId() == other.getId()};
114     *         a value less than {@code 0} if {@code getId() < other.getId()}; and
115     *         a value greater than {@code 0} if {@code getId() > other.getId()}
116     */
117    @Override
118    public int compareTo(Changeset other) {
119        return Integer.compare(getId(), other.getId());
120    }
121
122    /**
123     * Returns the changeset name.
124     * @return the changeset name (untranslated: "changeset &lt;identifier&gt;")
125     */
126    public String getName() {
127        // no translation
128        return "changeset " + getId();
129    }
130
131    /**
132     * Returns the changeset display name, as per given name formatter.
133     * @param formatter name formatter
134     * @return the changeset display name, as per given name formatter
135     */
136    public String getDisplayName(NameFormatter formatter) {
137        return formatter.format(this);
138    }
139
140    /**
141     * Returns the changeset identifier.
142     * @return the changeset identifier
143     */
144    public int getId() {
145        return id;
146    }
147
148    /**
149     * Sets the changeset identifier.
150     * @param id changeset identifier
151     */
152    public void setId(int id) {
153        this.id = id;
154    }
155
156    /**
157     * Returns the changeset user.
158     * @return the changeset user
159     */
160    public User getUser() {
161        return user;
162    }
163
164    /**
165     * Sets the changeset user.
166     * @param user changeset user
167     */
168    public void setUser(User user) {
169        this.user = user;
170    }
171
172    /**
173     * Returns the changeset creation date.
174     * @return the changeset creation date
175     */
176    public Instant getCreatedAt() {
177        return createdAt;
178    }
179
180    /**
181     * Sets the changeset creation date.
182     * @param createdAt changeset creation date
183     */
184    public void setCreatedAt(Instant createdAt) {
185        this.createdAt = createdAt;
186    }
187
188    /**
189     * Returns the changeset closure date.
190     * @return the changeset closure date
191     */
192    public Instant getClosedAt() {
193        return closedAt;
194    }
195
196    /**
197     * Sets the changeset closure date.
198     * @param closedAt changeset closure date
199     */
200    public void setClosedAt(Instant closedAt) {
201        this.closedAt = closedAt;
202    }
203
204    /**
205     * Determines if this changeset is open.
206     * @return {@code true} if this changeset is open
207     */
208    public boolean isOpen() {
209        return open;
210    }
211
212    /**
213     * Sets whether this changeset is open.
214     * @param open {@code true} if this changeset is open
215     */
216    public void setOpen(boolean open) {
217        this.open = open;
218    }
219
220    /**
221     * Returns the min lat/lon of the changeset bounding box.
222     * @return the min lat/lon of the changeset bounding box
223     */
224    public LatLon getMin() {
225        return min;
226    }
227
228    /**
229     * Sets the min lat/lon of the changeset bounding box.
230     * @param min min lat/lon of the changeset bounding box
231     */
232    public void setMin(LatLon min) {
233        this.min = min;
234    }
235
236    /**
237     * Returns the max lat/lon of the changeset bounding box.
238     * @return the max lat/lon of the changeset bounding box
239     */
240    public LatLon getMax() {
241        return max;
242    }
243
244    /**
245     * Sets the max lat/lon of the changeset bounding box.
246     * @param max min lat/lon of the changeset bounding box
247     */
248    public void setMax(LatLon max) {
249        this.max = max;
250    }
251
252    /**
253     * Returns the changeset bounding box.
254     * @return the changeset bounding box
255     */
256    public Bounds getBounds() {
257        if (min != null && max != null)
258            return new Bounds(min, max);
259        return null;
260    }
261
262    /**
263     * Replies this changeset comment.
264     * @return this changeset comment (empty string if missing)
265     * @since 12494
266     */
267    public String getComment() {
268        return Optional.ofNullable(get("comment")).orElse("");
269    }
270
271    /**
272     * Replies the number of comments for this changeset discussion.
273     * @return the number of comments for this changeset discussion
274     * @since 7700
275     */
276    public int getCommentsCount() {
277        return commentsCount;
278    }
279
280    /**
281     * Sets the number of comments for this changeset discussion.
282     * @param commentsCount the number of comments for this changeset discussion
283     * @since 7700
284     */
285    public void setCommentsCount(int commentsCount) {
286        this.commentsCount = commentsCount;
287    }
288
289    /**
290     * Replies the number of changes for this changeset.
291     * @return the number of changes for this changeset
292     * @since 14231
293     */
294    public int getChangesCount() {
295        return changesCount;
296    }
297
298    /**
299     * Sets the number of changes for this changeset.
300     * @param changesCount the number of changes for this changeset
301     * @since 14231
302     */
303    public void setChangesCount(int changesCount) {
304        this.changesCount = changesCount;
305    }
306
307    @Override
308    public Map<String, String> getKeys() {
309        return tags;
310    }
311
312    @Override
313    public void setKeys(Map<String, String> keys) {
314        CheckParameterUtil.ensureParameterNotNull(keys, "keys");
315        keys.values().stream()
316                .filter(value -> !Utils.checkCodePointCount(value, MAX_CHANGESET_TAG_LENGTH))
317                .findFirst()
318                .ifPresent(value -> {
319                throw new IllegalArgumentException("Changeset tag value is too long: "+value);
320        });
321        this.tags = new HashMap<>(keys);
322    }
323
324    /**
325     * Determines if this changeset is incomplete.
326     * @return {@code true} if this changeset is incomplete
327     */
328    public boolean isIncomplete() {
329        return incomplete;
330    }
331
332    /**
333     * Sets whether this changeset is incomplete
334     * @param incomplete {@code true} if this changeset is incomplete
335     */
336    public void setIncomplete(boolean incomplete) {
337        this.incomplete = incomplete;
338    }
339
340    @Override
341    public void put(String key, String value) {
342        CheckParameterUtil.ensureParameterNotNull(key, "key");
343        if (!Utils.checkCodePointCount(value, MAX_CHANGESET_TAG_LENGTH)) {
344            throw new IllegalArgumentException("Changeset tag value is too long: " + value);
345        }
346
347        this.tags.put(key, value);
348    }
349
350    @Override
351    public String get(String key) {
352        return this.tags.get(key);
353    }
354
355    @Override
356    public void remove(String key) {
357        this.tags.remove(key);
358    }
359
360    @Override
361    public void removeAll() {
362        this.tags.clear();
363    }
364
365    /**
366     * Determines if this changeset has equals semantic attributes with another one.
367     * @param other other changeset
368     * @return {@code true} if this changeset has equals semantic attributes with other changeset
369     */
370    public boolean hasEqualSemanticAttributes(Changeset other) {
371        return other != null
372            && id == other.id
373            && open == other.open
374            && commentsCount == other.commentsCount
375            && changesCount == other.changesCount
376            && Objects.equals(closedAt, other.closedAt)
377            && Objects.equals(createdAt, other.createdAt)
378            && Objects.equals(min, other.min)
379            && Objects.equals(max, other.max)
380            && Objects.equals(tags, other.tags)
381            && Objects.equals(user, other.user);
382    }
383
384    @Override
385    public int hashCode() {
386        return Objects.hash(id);
387    }
388
389    @Override
390    public boolean equals(Object obj) {
391        if (this == obj) return true;
392        if (obj == null || getClass() != obj.getClass()) return false;
393        Changeset changeset = (Changeset) obj;
394        return id == changeset.id;
395    }
396
397    @Override
398    public boolean hasKeys() {
399        return !tags.keySet().isEmpty();
400    }
401
402    @Override
403    public Collection<String> keySet() {
404        return tags.keySet();
405    }
406
407    @Override
408    public int getNumKeys() {
409        return tags.size();
410    }
411
412    /**
413     * Determines if this changeset is new.
414     * @return {@code true} if this changeset is new ({@code id <= 0})
415     */
416    public boolean isNew() {
417        return id <= 0;
418    }
419
420    /**
421     * Merges changeset metadata from another changeset.
422     * @param other other changeset
423     */
424    public void mergeFrom(Changeset other) {
425        if (other == null)
426            return;
427        if (id != other.id)
428            return;
429        this.user = other.user;
430        this.createdAt = other.createdAt;
431        this.closedAt = other.closedAt;
432        this.open = other.open;
433        this.min = other.min;
434        this.max = other.max;
435        this.commentsCount = other.commentsCount;
436        this.changesCount = other.changesCount;
437        this.tags = new HashMap<>(other.tags);
438        this.incomplete = other.incomplete;
439        this.discussion = other.discussion != null ? new ArrayList<>(other.discussion) : null;
440
441        // FIXME: merging of content required?
442        this.content = other.content;
443    }
444
445    /**
446     * Determines if this changeset has contents.
447     * @return {@code true} if this changeset has contents
448     */
449    public boolean hasContent() {
450        return content != null;
451    }
452
453    /**
454     * Returns the changeset contents.
455     * @return the changeset contents, can be null
456     */
457    public ChangesetDataSet getContent() {
458        return content;
459    }
460
461    /**
462     * Sets the changeset contents.
463     * @param content changeset contents, can be null
464     */
465    public void setContent(ChangesetDataSet content) {
466        this.content = content;
467    }
468
469    /**
470     * Replies the list of comments in the changeset discussion, if any.
471     * @return the list of comments in the changeset discussion. May be empty but never null
472     * @since 7704
473     */
474    public synchronized List<ChangesetDiscussionComment> getDiscussion() {
475        if (discussion == null) {
476            return Collections.emptyList();
477        }
478        return Collections.unmodifiableList(discussion);
479    }
480
481    /**
482     * Adds a comment to the changeset discussion.
483     * @param comment the comment to add. Ignored if null
484     * @since 7704
485     */
486    public synchronized void addDiscussionComment(ChangesetDiscussionComment comment) {
487        if (comment == null) {
488            return;
489        }
490        if (discussion == null) {
491            discussion = new ArrayList<>();
492        }
493        discussion.add(comment);
494    }
495
496    @Override
497    public String toString() {
498        return tr("Changeset") + " " + id + ": " + getComment();
499    }
500}