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.time.Instant; 007import java.util.ArrayList; 008import java.util.Comparator; 009import java.util.List; 010import java.util.Objects; 011 012import org.openstreetmap.josm.data.coor.LatLon; 013 014import jakarta.annotation.Nullable; 015 016/** 017 * A map note. It always has at least one comment since a comment is required to create a note on osm.org. 018 * @since 7451 019 */ 020public class Note { 021 022 /** Note state */ 023 public enum State { 024 /** Note is open */ 025 OPEN, 026 /** Note is closed */ 027 CLOSED 028 } 029 030 /** 031 * Sorts notes in the following order: 032 * <ol> 033 * <li>Open notes</li> 034 * <li>Closed notes</li> 035 * <li>New notes</li> 036 * </ol> 037 * Within each subgroup it sorts by ID 038 */ 039 public static final Comparator<Note> DEFAULT_COMPARATOR = (n1, n2) -> { 040 if (n1.getId() < 0 && n2.getId() > 0) { 041 return 1; 042 } 043 if (n1.getId() > 0 && n2.getId() < 0) { 044 return -1; 045 } 046 if (n1.getState() == State.CLOSED && n2.getState() == State.OPEN) { 047 return 1; 048 } 049 if (n1.getState() == State.OPEN && n2.getState() == State.CLOSED) { 050 return -1; 051 } 052 return Long.compare(Math.abs(n1.getId()), Math.abs(n2.getId())); 053 }; 054 055 /** Sorts notes strictly by creation date */ 056 public static final Comparator<Note> DATE_COMPARATOR = Comparator.comparing(n -> n.createdAt); 057 058 /** Sorts notes by user, then creation date */ 059 public static final Comparator<Note> USER_COMPARATOR = 060 Comparator.comparing(Note::getUserName, Comparator.nullsLast(Comparator.naturalOrder())).thenComparing(DATE_COMPARATOR); 061 062 /** Sorts notes by the last modified date */ 063 public static final Comparator<Note> LAST_ACTION_COMPARATOR = Comparator.comparing(Note::getLastComment, NoteComment.DATE_COMPARATOR); 064 065 private long id; 066 private LatLon latLon; 067 private Instant createdAt; 068 private Instant closedAt; 069 private State state; 070 private List<NoteComment> comments = new ArrayList<>(); 071 072 /** 073 * Create a note with a given location 074 * @param latLon Geographic location of this note 075 */ 076 public Note(LatLon latLon) { 077 this.latLon = latLon; 078 } 079 080 /** 081 * Returns the unique OSM ID of this note. 082 * @return The unique OSM ID of this note 083 */ 084 public long getId() { 085 return id; 086 } 087 088 /** 089 * Sets note id. 090 * @param id OSM ID of this note 091 */ 092 public void setId(long id) { 093 this.id = id; 094 } 095 096 /** 097 * Returns the geographic location of the note. 098 * @return The geographic location of the note 099 */ 100 public LatLon getLatLon() { 101 return latLon; 102 } 103 104 /** 105 * Returns the date at which this note was submitted. 106 * @return Date that this note was submitted 107 */ 108 public Instant getCreatedAt() { 109 return createdAt; 110 } 111 112 /** 113 * Sets date at which this note has been created. 114 * @param createdAt date at which this note has been created 115 */ 116 public void setCreatedAt(Instant createdAt) { 117 this.createdAt = createdAt; 118 } 119 120 /** 121 * Returns the date at which this note was closed. 122 * @return Date that this note was closed. Null if it is still open. 123 */ 124 public Instant getClosedAt() { 125 return closedAt; 126 } 127 128 /** 129 * Sets date at which this note has been closed. 130 * @param closedAt date at which this note has been closed 131 */ 132 public void setClosedAt(Instant closedAt) { 133 this.closedAt = closedAt; 134 } 135 136 /** 137 * Returns the open or closed state of this note. 138 * @return The open or closed state of this note 139 */ 140 public State getState() { 141 return state; 142 } 143 144 /** 145 * Sets the note state. 146 * @param state note state (open or closed) 147 */ 148 public void setState(State state) { 149 this.state = state; 150 } 151 152 /** 153 * Returns the list of comments associated with this note. 154 * @return An ordered list of comments associated with this note 155 */ 156 public List<NoteComment> getComments() { 157 return comments; 158 } 159 160 /** 161 * Returns the last comment, or {@code null}. 162 * @return the last comment, or {@code null} 163 * @since 11821 164 */ 165 @Nullable 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 @Nullable 183 public NoteComment getFirstComment() { 184 return comments.isEmpty() ? null : comments.get(0); 185 } 186 187 @Nullable 188 private String getUserName() { 189 return getFirstComment() == null ? null : getFirstComment().getUser().getName(); 190 } 191 192 /** 193 * Copies values from a new note into an existing one. Used after a note 194 * has been updated on the server and the local copy needs refreshing. 195 * @param note New values to copy 196 */ 197 public void updateWith(Note note) { 198 this.comments = note.comments; 199 this.createdAt = note.createdAt; 200 this.id = note.id; 201 this.state = note.state; 202 this.latLon = note.latLon; 203 } 204 205 @Override 206 public int hashCode() { 207 return Objects.hash(id); 208 } 209 210 @Override 211 public boolean equals(Object obj) { 212 if (this == obj) 213 return true; 214 if (obj == null || getClass() != obj.getClass()) 215 return false; 216 Note note = (Note) obj; 217 return id == note.id; 218 } 219 220 @Override 221 public String toString() { 222 return tr("Note") + ' ' + id + ": " + getFirstComment(); 223 } 224}