001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.notes;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.util.ArrayList;
007import java.util.Date;
008import java.util.List;
009import java.util.Objects;
010
011import org.openstreetmap.josm.data.coor.LatLon;
012
013/**
014 * A map note. It always has at least one comment since a comment is required
015 * to create a note on osm.org
016 */
017public class Note {
018
019    public enum State { open, closed }
020
021    private long id;
022    private LatLon latLon;
023    private Date createdAt;
024    private Date closedAt;
025    private State state;
026    private List<NoteComment> comments = new ArrayList<>();
027
028    /**
029     * Create a note with a given location
030     * @param latLon Geographic location of this note
031     */
032    public Note(LatLon latLon) {
033        this.latLon = latLon;
034    }
035
036    /** @return The unique OSM ID of this note */
037    public long getId() {
038        return id;
039    }
040
041    public void setId(long id) {
042        this.id = id;
043    }
044
045    /** @return The geographic location of the note */
046    public LatLon getLatLon() {
047        return latLon;
048    }
049
050    /** @return Date that this note was submitted */
051    public Date getCreatedAt() {
052        return createdAt;
053    }
054
055    public void setCreatedAt(Date createdAt) {
056        this.createdAt = createdAt;
057    }
058
059    /** @return Date that this note was closed. Null if it is still open. */
060    public Date getClosedAt() {
061        return closedAt;
062    }
063
064    public void setClosedAt(Date closedAt) {
065        this.closedAt = closedAt;
066    }
067
068    /** @return The open or closed state of this note */
069    public State getState() {
070        return state;
071    }
072
073    public void setState(State state) {
074        this.state = state;
075    }
076
077    /** @return An ordered list of comments associated with this note */
078    public List<NoteComment> getComments() {
079        return comments;
080    }
081
082    public void addComment(NoteComment comment) {
083        this.comments.add(comment);
084    }
085
086    /**
087     * Returns the comment that was submitted by the user when creating the note
088     * @return First comment object
089     */
090    public NoteComment getFirstComment() {
091        return this.comments.get(0);
092    }
093
094    /**
095     * Copies values from a new note into an existing one. Used after a note
096     * has been updated on the server and the local copy needs refreshing.
097     * @param note New values to copy
098     */
099    public void updateWith(Note note) {
100        this.comments = note.comments;
101        this.createdAt = note.createdAt;
102        this.id = note.id;
103        this.state = note.state;
104        this.latLon = note.latLon;
105    }
106
107    @Override
108    public int hashCode() {
109        return Objects.hash(id);
110    }
111
112    @Override
113    public boolean equals(Object obj) {
114        if (this == obj) return true;
115        if (obj == null || getClass() != obj.getClass()) return false;
116        Note note = (Note) obj;
117        return id == note.id;
118    }
119
120    @Override
121    public String toString() {
122        return tr("Note") + " " + id + ": " + getFirstComment();
123    }
124}