Ignore:
Timestamp:
2017-04-01T23:27:46+02:00 (7 years ago)
Author:
Don-vip
Message:

findbugs - EI_EXPOSE_REP2 - Note dates

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/notes/Note.java

    r10300 r11821  
    55
    66import java.util.ArrayList;
     7import java.util.Comparator;
    78import java.util.Date;
    89import java.util.List;
     
    2526    }
    2627
     28    /**
     29     * Sorts notes in the following order:
     30     * 1) Open notes
     31     * 2) Closed notes
     32     * 3) New notes
     33     * Within each subgroup it sorts by ID
     34     */
     35    public static final Comparator<Note> DEFAULT_COMPARATOR = (n1, n2) -> {
     36        if (n1.getId() < 0 && n2.getId() > 0) {
     37            return 1;
     38        }
     39        if (n1.getId() > 0 && n2.getId() < 0) {
     40            return -1;
     41        }
     42        if (n1.getState() == State.CLOSED && n2.getState() == State.OPEN) {
     43            return 1;
     44        }
     45        if (n1.getState() == State.OPEN && n2.getState() == State.CLOSED) {
     46            return -1;
     47        }
     48        return Long.compare(Math.abs(n1.getId()), Math.abs(n2.getId()));
     49    };
     50
     51    /** Sorts notes strictly by creation date */
     52    public static final Comparator<Note> DATE_COMPARATOR = (n1, n2) -> n1.createdAt.compareTo(n2.createdAt);
     53
     54    /** Sorts notes by user, then creation date */
     55    public static final Comparator<Note> USER_COMPARATOR = (n1, n2) -> {
     56        String n1User = n1.getFirstComment().getUser().getName();
     57        String n2User = n2.getFirstComment().getUser().getName();
     58        return n1User.equals(n2User) ? DATE_COMPARATOR.compare(n1, n2) : n1User.compareTo(n2User);
     59    };
     60
     61    /** Sorts notes by the last modified date */
     62    public static final Comparator<Note> LAST_ACTION_COMPARATOR =
     63            (n1, n2) -> NoteComment.DATE_COMPARATOR.compare(n1.getLastComment(), n2.getLastComment());
     64
    2765    private long id;
    2866    private LatLon latLon;
     
    6098    /** @return Date that this note was submitted */
    6199    public Date getCreatedAt() {
    62         return createdAt;
     100        return cloneDate(createdAt);
    63101    }
    64102
     
    68106     */
    69107    public void setCreatedAt(Date createdAt) {
    70         this.createdAt = createdAt;
     108        this.createdAt = cloneDate(createdAt);
    71109    }
    72110
    73111    /** @return Date that this note was closed. Null if it is still open. */
    74112    public Date getClosedAt() {
    75         return closedAt;
     113        return cloneDate(closedAt);
    76114    }
    77115
     
    81119     */
    82120    public void setClosedAt(Date closedAt) {
    83         this.closedAt = closedAt;
     121        this.closedAt = cloneDate(closedAt);
    84122    }
    85123
     
    103141
    104142    /**
     143     * Returns the last comment, or {@code null}.
     144     * @return the last comment, or {@code null}
     145     * @since 11821
     146     */
     147    public NoteComment getLastComment() {
     148        return comments.isEmpty() ? null : comments.get(comments.size()-1);
     149    }
     150
     151    /**
    105152     * Adds a comment.
    106153     * @param comment note comment
     
    125172    public void updateWith(Note note) {
    126173        this.comments = note.comments;
    127         this.createdAt = note.createdAt;
     174        this.createdAt = cloneDate(note.createdAt);
    128175        this.id = note.id;
    129176        this.state = note.state;
     
    150197        return tr("Note") + ' ' + id + ": " + getFirstComment();
    151198    }
     199
     200    /**
     201     * Null-safe date cloning method.
     202     * @param d date to clone, or null
     203     * @return cloned date, or null
     204     */
     205    static Date cloneDate(Date d) {
     206        return d != null ? (Date) d.clone() : null;
     207    }
    152208}
Note: See TracChangeset for help on using the changeset viewer.