1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.notes;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Comparator;
|
---|
8 | import java.util.Date;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Objects;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
13 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * A map note. It always has at least one comment since a comment is required to create a note on osm.org.
|
---|
17 | * @since 7451
|
---|
18 | */
|
---|
19 | public class Note {
|
---|
20 |
|
---|
21 | /** Note state */
|
---|
22 | public enum State {
|
---|
23 | /** Note is open */
|
---|
24 | OPEN,
|
---|
25 | /** Note is closed */
|
---|
26 | CLOSED
|
---|
27 | }
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Sorts notes in the following order:
|
---|
31 | * 1) Open notes
|
---|
32 | * 2) Closed notes
|
---|
33 | * 3) New notes
|
---|
34 | * Within each subgroup it sorts by ID
|
---|
35 | */
|
---|
36 | public static final Comparator<Note> DEFAULT_COMPARATOR = (n1, n2) -> {
|
---|
37 | if (n1.getId() < 0 && n2.getId() > 0) {
|
---|
38 | return 1;
|
---|
39 | }
|
---|
40 | if (n1.getId() > 0 && n2.getId() < 0) {
|
---|
41 | return -1;
|
---|
42 | }
|
---|
43 | if (n1.getState() == State.CLOSED && n2.getState() == State.OPEN) {
|
---|
44 | return 1;
|
---|
45 | }
|
---|
46 | if (n1.getState() == State.OPEN && n2.getState() == State.CLOSED) {
|
---|
47 | return -1;
|
---|
48 | }
|
---|
49 | return Long.compare(Math.abs(n1.getId()), Math.abs(n2.getId()));
|
---|
50 | };
|
---|
51 |
|
---|
52 | /** Sorts notes strictly by creation date */
|
---|
53 | public static final Comparator<Note> DATE_COMPARATOR = (n1, n2) -> n1.createdAt.compareTo(n2.createdAt);
|
---|
54 |
|
---|
55 | /** Sorts notes by user, then creation date */
|
---|
56 | public static final Comparator<Note> USER_COMPARATOR = (n1, n2) -> {
|
---|
57 | String n1User = n1.getFirstComment().getUser().getName();
|
---|
58 | String n2User = n2.getFirstComment().getUser().getName();
|
---|
59 | return n1User.equals(n2User) ? DATE_COMPARATOR.compare(n1, n2) : n1User.compareTo(n2User);
|
---|
60 | };
|
---|
61 |
|
---|
62 | /** Sorts notes by the last modified date */
|
---|
63 | public static final Comparator<Note> LAST_ACTION_COMPARATOR =
|
---|
64 | (n1, n2) -> NoteComment.DATE_COMPARATOR.compare(n1.getLastComment(), n2.getLastComment());
|
---|
65 |
|
---|
66 | private long id;
|
---|
67 | private LatLon latLon;
|
---|
68 | private Date createdAt;
|
---|
69 | private Date closedAt;
|
---|
70 | private State state;
|
---|
71 | private List<NoteComment> comments = new ArrayList<>();
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Create a note with a given location
|
---|
75 | * @param latLon Geographic location of this note
|
---|
76 | */
|
---|
77 | public Note(LatLon latLon) {
|
---|
78 | this.latLon = latLon;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /** @return The unique OSM ID of this note */
|
---|
82 | public long getId() {
|
---|
83 | return id;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Sets note id.
|
---|
88 | * @param id OSM ID of this note
|
---|
89 | */
|
---|
90 | public void setId(long id) {
|
---|
91 | this.id = id;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /** @return The geographic location of the note */
|
---|
95 | public LatLon getLatLon() {
|
---|
96 | return latLon;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /** @return Date that this note was submitted */
|
---|
100 | public Date getCreatedAt() {
|
---|
101 | return DateUtils.cloneDate(createdAt);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Sets date at which this note has been created.
|
---|
106 | * @param createdAt date at which this note has been created
|
---|
107 | */
|
---|
108 | public void setCreatedAt(Date createdAt) {
|
---|
109 | this.createdAt = DateUtils.cloneDate(createdAt);
|
---|
110 | }
|
---|
111 |
|
---|
112 | /** @return Date that this note was closed. Null if it is still open. */
|
---|
113 | public Date getClosedAt() {
|
---|
114 | return DateUtils.cloneDate(closedAt);
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Sets date at which this note has been closed.
|
---|
119 | * @param closedAt date at which this note has been closed
|
---|
120 | */
|
---|
121 | public void setClosedAt(Date closedAt) {
|
---|
122 | this.closedAt = DateUtils.cloneDate(closedAt);
|
---|
123 | }
|
---|
124 |
|
---|
125 | /** @return The open or closed state of this note */
|
---|
126 | public State getState() {
|
---|
127 | return state;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Sets the note state.
|
---|
132 | * @param state note state (open or closed)
|
---|
133 | */
|
---|
134 | public void setState(State state) {
|
---|
135 | this.state = state;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /** @return An ordered list of comments associated with this note */
|
---|
139 | public List<NoteComment> getComments() {
|
---|
140 | return comments;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Returns the last comment, or {@code null}.
|
---|
145 | * @return the last comment, or {@code null}
|
---|
146 | * @since 11821
|
---|
147 | */
|
---|
148 | public NoteComment getLastComment() {
|
---|
149 | return comments.isEmpty() ? null : comments.get(comments.size()-1);
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Adds a comment.
|
---|
154 | * @param comment note comment
|
---|
155 | */
|
---|
156 | public void addComment(NoteComment comment) {
|
---|
157 | comments.add(comment);
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Returns the comment that was submitted by the user when creating the note
|
---|
162 | * @return First comment object
|
---|
163 | */
|
---|
164 | public NoteComment getFirstComment() {
|
---|
165 | return comments.isEmpty() ? null : comments.get(0);
|
---|
166 | }
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Copies values from a new note into an existing one. Used after a note
|
---|
170 | * has been updated on the server and the local copy needs refreshing.
|
---|
171 | * @param note New values to copy
|
---|
172 | */
|
---|
173 | public void updateWith(Note note) {
|
---|
174 | this.comments = note.comments;
|
---|
175 | this.createdAt = DateUtils.cloneDate(note.createdAt);
|
---|
176 | this.id = note.id;
|
---|
177 | this.state = note.state;
|
---|
178 | this.latLon = note.latLon;
|
---|
179 | }
|
---|
180 |
|
---|
181 | @Override
|
---|
182 | public int hashCode() {
|
---|
183 | return Objects.hash(id);
|
---|
184 | }
|
---|
185 |
|
---|
186 | @Override
|
---|
187 | public boolean equals(Object obj) {
|
---|
188 | if (this == obj)
|
---|
189 | return true;
|
---|
190 | if (obj == null || getClass() != obj.getClass())
|
---|
191 | return false;
|
---|
192 | Note note = (Note) obj;
|
---|
193 | return id == note.id;
|
---|
194 | }
|
---|
195 |
|
---|
196 | @Override
|
---|
197 | public String toString() {
|
---|
198 | return tr("Note") + ' ' + id + ": " + getFirstComment();
|
---|
199 | }
|
---|
200 | }
|
---|