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 to create a note on osm.org.
015 * @since 7451
016 */
017public class Note {
018
019    /** Note state */
020    public enum State {
021        /** Note is open */
022        OPEN,
023        /** Note is closed */
024        CLOSED
025    }
026
027    private long id;
028    private LatLon latLon;
029    private Date createdAt;
030    private Date closedAt;
031    private State state;
032    private List<NoteComment> comments = new ArrayList<>();
033
034    /**
035     * Create a note with a given location
036     * @param latLon Geographic location of this note
037     */
038    public Note(LatLon latLon) {
039        this.latLon = latLon;
040    }
041
042    /** @return The unique OSM ID of this note */
043    public long getId() {
044        return id;
045    }
046
047    /**
048     * Sets note id.
049     * @param id OSM ID of this note
050     */
051    public void setId(long id) {
052        this.id = id;
053    }
054
055    /** @return The geographic location of the note */
056    public LatLon getLatLon() {
057        return latLon;
058    }
059
060    /** @return Date that this note was submitted */
061    public Date getCreatedAt() {
062        return createdAt;
063    }
064
065    /**
066     * Sets date at which this note has been created.
067     * @param createdAt date at which this note has been created
068     */
069    public void setCreatedAt(Date createdAt) {
070        this.createdAt = createdAt;
071    }
072
073    /** @return Date that this note was closed. Null if it is still open. */
074    public Date getClosedAt() {
075        return closedAt;
076    }
077
078    /**
079     * Sets date at which this note has been closed.
080     * @param closedAt date at which this note has been closed
081     */
082    public void setClosedAt(Date closedAt) {
083        this.closedAt = closedAt;
084    }
085
086    /** @return The open or closed state of this note */
087    public State getState() {
088        return state;
089    }
090
091    /**
092     * Sets the note state.
093     * @param state note state (open or closed)
094     */
095    public void setState(State state) {
096        this.state = state;
097    }
098
099    /** @return An ordered list of comments associated with this note */
100    public List<NoteComment> getComments() {
101        return comments;
102    }
103
104    /**
105     * Adds a comment.
106     * @param comment note comment
107     */
108    public void addComment(NoteComment comment) {
109        comments.add(comment);
110    }
111
112    /**
113     * Returns the comment that was submitted by the user when creating the note
114     * @return First comment object
115     */
116    public NoteComment getFirstComment() {
117        return comments.isEmpty() ? null : comments.get(0);
118    }
119
120    /**
121     * Copies values from a new note into an existing one. Used after a note
122     * has been updated on the server and the local copy needs refreshing.
123     * @param note New values to copy
124     */
125    public void updateWith(Note note) {
126        this.comments = note.comments;
127        this.createdAt = note.createdAt;
128        this.id = note.id;
129        this.state = note.state;
130        this.latLon = note.latLon;
131    }
132
133    @Override
134    public int hashCode() {
135        return Objects.hash(id);
136    }
137
138    @Override
139    public boolean equals(Object obj) {
140        if (this == obj)
141            return true;
142        if (obj == null || getClass() != obj.getClass())
143            return false;
144        Note note = (Note) obj;
145        return id == note.id;
146    }
147
148    @Override
149    public String toString() {
150        return tr("Note") + ' ' + id + ": " + getFirstComment();
151    }
152}