Changeset 17837 in josm


Ignore:
Timestamp:
2021-04-30T21:52:45+02:00 (4 years ago)
Author:
simon04
Message:

fix #20824 - NPE while sorting notes

Location:
trunk
Files:
3 edited

Legend:

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

    r17712 r17837  
    5353
    5454    /** 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     };
     55    public static final Comparator<Note> USER_COMPARATOR =
     56            Comparator.comparing(Note::getUserName, Comparator.nullsLast(Comparator.naturalOrder())).thenComparing(DATE_COMPARATOR);
    6057
    6158    /** 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());
     59    public static final Comparator<Note> LAST_ACTION_COMPARATOR = Comparator.comparing(Note::getLastComment, NoteComment.DATE_COMPARATOR);
    6460
    6561    private long id;
     
    181177    public NoteComment getFirstComment() {
    182178        return comments.isEmpty() ? null : comments.get(0);
     179    }
     180
     181    private String getUserName() {
     182        return getFirstComment() == null ? null : getFirstComment().getUser().getName();
    183183    }
    184184
  • trunk/src/org/openstreetmap/josm/data/notes/NoteComment.java

    r17712 r17837  
    4040
    4141    /** Sorts note comments strictly by creation date */
    42     public static final Comparator<NoteComment> DATE_COMPARATOR = Comparator.comparing(n -> n.commentTimestamp);
     42    public static final Comparator<NoteComment> DATE_COMPARATOR = Comparator.nullsLast(Comparator.comparing(n -> n.commentTimestamp));
    4343
    4444    /**
  • trunk/test/unit/org/openstreetmap/josm/data/notes/NoteTest.java

    r17712 r17837  
    66
    77import java.time.Instant;
     8import java.util.List;
    89
    910import org.junit.jupiter.api.extension.RegisterExtension;
     
    1112import org.openstreetmap.josm.TestUtils;
    1213import org.openstreetmap.josm.data.coor.LatLon;
     14import org.openstreetmap.josm.io.NoteReader;
    1315import org.openstreetmap.josm.testutils.JOSMTestRules;
    1416
     
    6971            .verify();
    7072    }
     73
     74    @Test
     75    void testSorting() throws Exception {
     76        List<Note> list = new NoteReader("" +
     77                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
     78                "<osm version=\"0.6\" generator=\"OpenStreetMap server\">\n" +
     79                "<note lon=\"68.86415\" lat=\"36.7232991\">\n"+
     80                "  <id>4</id>\n"+
     81                "  <url>http://api.openstreetmap.org/api/0.6/notes/4</url>\n"+
     82                "  <reopen_url>http://api.openstreetmap.org/api/0.6/notes/4/reopen</reopen_url>\n"+
     83                "  <date_created>2013-04-24 08:07:02 UTC</date_created>\n"+
     84                "  <status>closed</status>\n"+
     85                "  <date_closed>2013-04-24 08:08:51 UTC</date_closed>\n"+
     86                "  <comments>\n"+
     87                "    <comment>\n"+
     88                "      <date>2013-04-24 08:07:02 UTC</date>\n"+
     89                "      <uid>1626</uid>\n"+
     90                "      <user>FredB</user>\n"+
     91                "      <user_url>http://www.openstreetmap.org/user/FredB</user_url>\n"+
     92                "      <action>opened</action>\n"+
     93                "      <text>test</text>\n"+
     94                "      <html>&lt;p&gt;test&lt;/p&gt;</html>\n"+
     95                "    </comment>\n"+
     96                "    <comment>\n"+
     97                "      <date>2013-04-24 08:08:51 UTC</date>\n"+
     98                "      <uid>1626</uid>\n"+
     99                "      <user>FredB</user>\n"+
     100                "      <user_url>http://www.openstreetmap.org/user/FredB</user_url>\n"+
     101                "      <action>closed</action>\n"+
     102                "      <text></text>\n"+
     103                "      <html>&lt;p&gt;&lt;/p&gt;</html>\n"+
     104                "    </comment>\n"+
     105                "  </comments>\n"+
     106                "</note>\n"+
     107                "<note lon=\"23.2663071\" lat=\"50.7173607\">\n" +
     108                "  <id>1396945</id>\n" +
     109                "  <url>https://www.openstreetmap.org/api/0.6/notes/1396945</url>\n" +
     110                "  <comment_url>https://www.openstreetmap.org/api/0.6/notes/1396945/comment</comment_url>\n" +
     111                "  <close_url>https://www.openstreetmap.org/api/0.6/notes/1396945/close</close_url>\n" +
     112                "  <date_created>2018-05-17 15:41:06 UTC</date_created>\n" +
     113                "  <status>open</status>\n" +
     114                "  <comments>\n" +
     115                "  </comments>\n" +
     116                "</note>\n" +
     117                "</osm>\n").parse();
     118        // Non-regression test for ticket #20824
     119        list.sort(Note.DATE_COMPARATOR);
     120        list.sort(Note.DEFAULT_COMPARATOR);
     121        list.sort(Note.LAST_ACTION_COMPARATOR);
     122        list.sort(Note.USER_COMPARATOR);
     123    }
    71124}
Note: See TracChangeset for help on using the changeset viewer.