source: josm/trunk/src/org/openstreetmap/josm/data/notes/Note.java@ 12851

Last change on this file since 12851 was 12540, checked in by Don-vip, 7 years ago

checkstyle - SingleLineJavadocCheck

  • Property svn:eol-style set to native
File size: 5.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.notes;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.ArrayList;
7import java.util.Comparator;
8import java.util.Date;
9import java.util.List;
10import java.util.Objects;
11
12import org.openstreetmap.josm.data.coor.LatLon;
13import 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 */
19public 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 /**
82 * Returns the unique OSM ID of this note.
83 * @return The unique OSM ID of this note
84 */
85 public long getId() {
86 return id;
87 }
88
89 /**
90 * Sets note id.
91 * @param id OSM ID of this note
92 */
93 public void setId(long id) {
94 this.id = id;
95 }
96
97 /**
98 * Returns the geographic location of the note.
99 * @return The geographic location of the note
100 */
101 public LatLon getLatLon() {
102 return latLon;
103 }
104
105 /**
106 * Returns the date at which this note was submitted.
107 * @return Date that this note was submitted
108 */
109 public Date getCreatedAt() {
110 return DateUtils.cloneDate(createdAt);
111 }
112
113 /**
114 * Sets date at which this note has been created.
115 * @param createdAt date at which this note has been created
116 */
117 public void setCreatedAt(Date createdAt) {
118 this.createdAt = DateUtils.cloneDate(createdAt);
119 }
120
121 /**
122 * Returns the date at which this note was closed.
123 * @return Date that this note was closed. Null if it is still open.
124 */
125 public Date getClosedAt() {
126 return DateUtils.cloneDate(closedAt);
127 }
128
129 /**
130 * Sets date at which this note has been closed.
131 * @param closedAt date at which this note has been closed
132 */
133 public void setClosedAt(Date closedAt) {
134 this.closedAt = DateUtils.cloneDate(closedAt);
135 }
136
137 /**
138 * Returns the open or closed state of this note.
139 * @return The open or closed state of this note
140 */
141 public State getState() {
142 return state;
143 }
144
145 /**
146 * Sets the note state.
147 * @param state note state (open or closed)
148 */
149 public void setState(State state) {
150 this.state = state;
151 }
152
153 /**
154 * Returns the list of comments associated with this note.
155 * @return An ordered list of comments associated with this note
156 */
157 public List<NoteComment> getComments() {
158 return comments;
159 }
160
161 /**
162 * Returns the last comment, or {@code null}.
163 * @return the last comment, or {@code null}
164 * @since 11821
165 */
166 public NoteComment getLastComment() {
167 return comments.isEmpty() ? null : comments.get(comments.size()-1);
168 }
169
170 /**
171 * Adds a comment.
172 * @param comment note comment
173 */
174 public void addComment(NoteComment comment) {
175 comments.add(comment);
176 }
177
178 /**
179 * Returns the comment that was submitted by the user when creating the note
180 * @return First comment object
181 */
182 public NoteComment getFirstComment() {
183 return comments.isEmpty() ? null : comments.get(0);
184 }
185
186 /**
187 * Copies values from a new note into an existing one. Used after a note
188 * has been updated on the server and the local copy needs refreshing.
189 * @param note New values to copy
190 */
191 public void updateWith(Note note) {
192 this.comments = note.comments;
193 this.createdAt = DateUtils.cloneDate(note.createdAt);
194 this.id = note.id;
195 this.state = note.state;
196 this.latLon = note.latLon;
197 }
198
199 @Override
200 public int hashCode() {
201 return Objects.hash(id);
202 }
203
204 @Override
205 public boolean equals(Object obj) {
206 if (this == obj)
207 return true;
208 if (obj == null || getClass() != obj.getClass())
209 return false;
210 Note note = (Note) obj;
211 return id == note.id;
212 }
213
214 @Override
215 public String toString() {
216 return tr("Note") + ' ' + id + ": " + getFirstComment();
217 }
218}
Note: See TracBrowser for help on using the repository browser.