Changeset 11821 in josm


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

findbugs - EI_EXPOSE_REP2 - Note dates

Location:
trunk/src/org/openstreetmap/josm
Files:
4 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}
  • trunk/src/org/openstreetmap/josm/data/notes/NoteComment.java

    r10134 r11821  
    22package org.openstreetmap.josm.data.notes;
    33
     4import java.util.Comparator;
    45import java.util.Date;
    56
     
    3839    }
    3940
     41    /** Sorts note comments strictly by creation date */
     42    public static final Comparator<NoteComment> DATE_COMPARATOR = (n1, n2) -> n1.commentTimestamp.compareTo(n2.commentTimestamp);
     43
    4044    /**
    4145     * @param createDate The time at which this comment was added
     
    4852        this.text = commentText;
    4953        this.user = user;
    50         this.commentTimestamp = createDate;
     54        this.commentTimestamp = Note.cloneDate(createDate);
    5155        this.action = action;
    5256        this.isNew = isNew;
     
    6569    /** @return The time at which this comment was created */
    6670    public Date getCommentTimestamp() {
    67         return commentTimestamp;
     71        return Note.cloneDate(commentTimestamp);
    6872    }
    6973
  • trunk/src/org/openstreetmap/josm/data/osm/NoteData.java

    r10717 r11821  
    2626    private final Storage<Note> noteList;
    2727    private Note selectedNote;
    28     private Comparator<Note> comparator = DEFAULT_COMPARATOR;
    29 
    30     /**
    31      * Sorts notes in the following order:
    32      * 1) Open notes
    33      * 2) Closed notes
    34      * 3) New notes
    35      * Within each subgroup it sorts by ID
    36      */
    37     public static final Comparator<Note> DEFAULT_COMPARATOR = (n1, n2) -> {
    38         if (n1.getId() < 0 && n2.getId() > 0) {
    39             return 1;
    40         }
    41         if (n1.getId() > 0 && n2.getId() < 0) {
    42             return -1;
    43         }
    44         if (n1.getState() == State.CLOSED && n2.getState() == State.OPEN) {
    45             return 1;
    46         }
    47         if (n1.getState() == State.OPEN && n2.getState() == State.CLOSED) {
    48             return -1;
    49         }
    50         return Long.compare(Math.abs(n1.getId()), Math.abs(n2.getId()));
    51     };
    52 
    53     /** Sorts notes strictly by creation date */
    54     public static final Comparator<Note> DATE_COMPARATOR = (n1, n2) -> n1.getCreatedAt().compareTo(n2.getCreatedAt());
    55 
    56     /** Sorts notes by user, then creation date */
    57     public static final Comparator<Note> USER_COMPARATOR = (n1, n2) -> {
    58         String n1User = n1.getFirstComment().getUser().getName();
    59         String n2User = n2.getFirstComment().getUser().getName();
    60         if (n1User.equals(n2User)) {
    61             return n1.getCreatedAt().compareTo(n2.getCreatedAt());
    62         }
    63         return n1.getFirstComment().getUser().getName().compareTo(n2.getFirstComment().getUser().getName());
    64     };
    65 
    66     /** Sorts notes by the last modified date */
    67     public static final Comparator<Note> LAST_ACTION_COMPARATOR = (n1, n2) -> {
    68         Date n1Date = n1.getComments().get(n1.getComments().size()-1).getCommentTimestamp();
    69         Date n2Date = n2.getComments().get(n2.getComments().size()-1).getCommentTimestamp();
    70         return n1Date.compareTo(n2Date);
    71     };
     28    private Comparator<Note> comparator = Note.DEFAULT_COMPARATOR;
    7229
    7330    /**
  • trunk/src/org/openstreetmap/josm/gui/NoteSortDialog.java

    r9078 r11821  
    1515import org.openstreetmap.josm.Main;
    1616import org.openstreetmap.josm.data.notes.Note;
    17 import org.openstreetmap.josm.data.osm.NoteData;
    1817
    1918/**
     
    4342    public void showSortDialog(Comparator<Note> currentSortMode) {
    4443        JLabel label = new JLabel(tr("Select note sorting method"));
    45         if (currentSortMode == NoteData.DEFAULT_COMPARATOR) {
     44        if (currentSortMode == Note.DEFAULT_COMPARATOR) {
    4645            defaultSort.setSelected(true);
    47         } else if (currentSortMode == NoteData.DATE_COMPARATOR) {
     46        } else if (currentSortMode == Note.DATE_COMPARATOR) {
    4847            dateSort.setSelected(true);
    49         } else if (currentSortMode == NoteData.USER_COMPARATOR) {
     48        } else if (currentSortMode == Note.USER_COMPARATOR) {
    5049            userSort.setSelected(true);
    51         } else if (currentSortMode == NoteData.LAST_ACTION_COMPARATOR) {
     50        } else if (currentSortMode == Note.LAST_ACTION_COMPARATOR) {
    5251            lastActionSort.setSelected(true);
    5352        } else {
     
    7776    public Comparator<Note> getSelectedComparator() {
    7877        if (dateSort.isSelected()) {
    79             return NoteData.DATE_COMPARATOR;
     78            return Note.DATE_COMPARATOR;
    8079        } else if (userSort.isSelected()) {
    81             return NoteData.USER_COMPARATOR;
     80            return Note.USER_COMPARATOR;
    8281        } else if (lastActionSort.isSelected()) {
    83             return NoteData.LAST_ACTION_COMPARATOR;
     82            return Note.LAST_ACTION_COMPARATOR;
    8483        } else {
    85             return NoteData.DEFAULT_COMPARATOR;
     84            return Note.DEFAULT_COMPARATOR;
    8685        }
    8786    }
Note: See TracChangeset for help on using the changeset viewer.