Changeset 17712 in josm


Ignore:
Timestamp:
2021-04-07T22:34:03+02:00 (3 years ago)
Author:
simon04
Message:

see #14176 - Migrate Note/NoteComment to Instant

Location:
trunk
Files:
13 edited

Legend:

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

    r16953 r17712  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.time.Instant;
    67import java.util.ArrayList;
    78import java.util.Comparator;
    8 import java.util.Date;
    99import java.util.List;
    1010import java.util.Objects;
    1111
    1212import org.openstreetmap.josm.data.coor.LatLon;
    13 import org.openstreetmap.josm.tools.date.DateUtils;
    1413
    1514/**
     
    6665    private long id;
    6766    private LatLon latLon;
    68     private Date createdAt;
    69     private Date closedAt;
     67    private Instant createdAt;
     68    private Instant closedAt;
    7069    private State state;
    7170    private List<NoteComment> comments = new ArrayList<>();
     
    107106     * @return Date that this note was submitted
    108107     */
    109     public Date getCreatedAt() {
    110         return DateUtils.cloneDate(createdAt);
     108    public Instant getCreatedAt() {
     109        return createdAt;
    111110    }
    112111
     
    115114     * @param createdAt date at which this note has been created
    116115     */
    117     public void setCreatedAt(Date createdAt) {
    118         this.createdAt = DateUtils.cloneDate(createdAt);
     116    public void setCreatedAt(Instant createdAt) {
     117        this.createdAt = createdAt;
    119118    }
    120119
     
    123122     * @return Date that this note was closed. Null if it is still open.
    124123     */
    125     public Date getClosedAt() {
    126         return DateUtils.cloneDate(closedAt);
     124    public Instant getClosedAt() {
     125        return closedAt;
    127126    }
    128127
     
    131130     * @param closedAt date at which this note has been closed
    132131     */
    133     public void setClosedAt(Date closedAt) {
    134         this.closedAt = DateUtils.cloneDate(closedAt);
     132    public void setClosedAt(Instant closedAt) {
     133        this.closedAt = closedAt;
    135134    }
    136135
     
    191190    public void updateWith(Note note) {
    192191        this.comments = note.comments;
    193         this.createdAt = DateUtils.cloneDate(note.createdAt);
     192        this.createdAt = note.createdAt;
    194193        this.id = note.id;
    195194        this.state = note.state;
  • trunk/src/org/openstreetmap/josm/data/notes/NoteComment.java

    r16953 r17712  
    22package org.openstreetmap.josm.data.notes;
    33
     4import java.time.Instant;
    45import java.util.Comparator;
    5 import java.util.Date;
    66
    77import org.openstreetmap.josm.data.osm.User;
    8 import org.openstreetmap.josm.tools.date.DateUtils;
    98
    109/**
     
    1716    private final String text;
    1817    private final User user;
    19     private final Date commentTimestamp;
     18    private final Instant commentTimestamp;
    2019    private final Action action;
    2120
     
    5049     * @param isNew Whether or not this comment is new and needs to be uploaded
    5150     */
    52     public NoteComment(Date createDate, User user, String commentText, Action action, boolean isNew) {
     51    public NoteComment(Instant createDate, User user, String commentText, Action action, boolean isNew) {
    5352        this.text = commentText;
    5453        this.user = user;
    55         this.commentTimestamp = DateUtils.cloneDate(createDate);
     54        this.commentTimestamp = createDate;
    5655        this.action = action;
    5756        this.isNew = isNew;
     
    7877     * @return The time at which this comment was created
    7978     */
    80     public Date getCommentTimestamp() {
    81         return DateUtils.cloneDate(commentTimestamp);
     79    public Instant getCommentTimestamp() {
     80        return commentTimestamp;
    8281    }
    8382
  • trunk/src/org/openstreetmap/josm/data/osm/NoteData.java

    r16548 r17712  
    22package org.openstreetmap.josm.data.osm;
    33
     4import java.time.Instant;
    45import java.util.ArrayList;
    56import java.util.Collection;
    67import java.util.Collections;
    78import java.util.Comparator;
    8 import java.util.Date;
    99import java.util.List;
    1010import java.util.Map;
     
    177177        }
    178178        Note note = new Note(location);
    179         note.setCreatedAt(new Date());
     179        note.setCreatedAt(Instant.now());
    180180        note.setState(State.OPEN);
    181181        note.setId(newNoteId--);
    182         NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.OPENED, true);
     182        NoteComment comment = new NoteComment(Instant.now(), getCurrentUser(), text, NoteComment.Action.OPENED, true);
    183183        note.addComment(comment);
    184184        if (Logging.isDebugEnabled()) {
     
    204204            Logging.debug("Adding comment to note {0}: {1}", note.getId(), text);
    205205        }
    206         NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.COMMENTED, true);
     206        NoteComment comment = new NoteComment(Instant.now(), getCurrentUser(), text, NoteComment.Action.COMMENTED, true);
    207207        note.addComment(comment);
    208208        dataUpdated();
     
    224224            Logging.debug("closing note {0} with comment: {1}", note.getId(), text);
    225225        }
    226         NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.CLOSED, true);
     226        NoteComment comment = new NoteComment(Instant.now(), getCurrentUser(), text, NoteComment.Action.CLOSED, true);
    227227        note.addComment(comment);
    228228        note.setState(State.CLOSED);
    229         note.setClosedAt(new Date());
     229        note.setClosedAt(Instant.now());
    230230        dataUpdated();
    231231    }
     
    244244        }
    245245        Logging.debug("reopening note {0} with comment: {1}", note.getId(), text);
    246         NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.REOPENED, true);
     246        NoteComment comment = new NoteComment(Instant.now(), getCurrentUser(), text, NoteComment.Action.REOPENED, true);
    247247        note.addComment(comment);
    248248        note.setState(State.OPEN);
  • trunk/src/org/openstreetmap/josm/gui/dialogs/NotesDialog.java

    r17522 r17712  
    1010import java.awt.event.MouseAdapter;
    1111import java.awt.event.MouseEvent;
    12 import java.text.DateFormat;
     12import java.time.format.DateTimeFormatter;
     13import java.time.format.FormatStyle;
    1314import java.util.ArrayList;
    1415import java.util.Arrays;
     
    232233
    233234        private final DefaultListCellRenderer defaultListCellRenderer = new DefaultListCellRenderer();
    234         private final DateFormat dateFormat = DateUtils.getDateTimeFormat(DateFormat.MEDIUM, DateFormat.SHORT);
     235        private final DateTimeFormatter dateFormat = DateUtils.getDateTimeFormatter(FormatStyle.MEDIUM, FormatStyle.SHORT);
    235236
    236237        @Override
  • trunk/src/org/openstreetmap/josm/gui/layer/NoteLayer.java

    r17361 r17712  
    1515import java.io.File;
    1616import java.io.IOException;
    17 import java.text.DateFormat;
     17import java.time.format.FormatStyle;
    1818import java.util.Collection;
    1919import java.util.Collections;
     
    319319                sb.append(userName)
    320320                  .append(" on ")
    321                   .append(DateUtils.getDateFormat(DateFormat.MEDIUM).format(comment.getCommentTimestamp()))
     321                  .append(DateUtils.getDateFormatter(FormatStyle.MEDIUM).format(comment.getCommentTimestamp()))
    322322                  .append(":<br>");
    323323                String htmlText = XmlWriter.encode(comment.getText(), true);
  • trunk/src/org/openstreetmap/josm/io/NoteReader.java

    r14436 r17712  
    66import java.io.InputStream;
    77import java.nio.charset.StandardCharsets;
     8import java.time.Instant;
    89import java.util.ArrayList;
    9 import java.util.Date;
    1010import java.util.List;
    1111import java.util.Locale;
     
    6060        private String commentUsername;
    6161        private Action noteAction;
    62         private Date commentCreateDate;
     62        private Instant commentCreateDate;
    6363        private boolean commentIsNew;
    6464        private List<Note> notes;
     
    100100                commentUsername = attrs.getValue("user");
    101101                noteAction = Action.valueOf(attrs.getValue("action").toUpperCase(Locale.ENGLISH));
    102                 commentCreateDate = DateUtils.fromString(attrs.getValue("timestamp"));
     102                commentCreateDate = DateUtils.parseInstant(attrs.getValue("timestamp"));
    103103                commentIsNew = Boolean.parseBoolean(Optional.ofNullable(attrs.getValue("is_new")).orElse("false"));
    104104                break;
     
    143143                break;
    144144            case "date_created":
    145                 thisNote.setCreatedAt(DateUtils.fromString(buffer.toString()));
     145                thisNote.setCreatedAt(DateUtils.parseInstant(buffer.toString()));
    146146                break;
    147147            case "date_closed":
    148                 thisNote.setClosedAt(DateUtils.fromString(buffer.toString()));
     148                thisNote.setClosedAt(DateUtils.parseInstant(buffer.toString()));
    149149                break;
    150150            case "date":
    151                 commentCreateDate = DateUtils.fromString(buffer.toString());
     151                commentCreateDate = DateUtils.parseInstant(buffer.toString());
    152152                break;
    153153            case "user":
     
    205205        } else {
    206206            note.setState(Note.State.CLOSED);
    207             note.setClosedAt(DateUtils.fromString(closedTimeStr));
     207            note.setClosedAt(DateUtils.parseInstant(closedTimeStr));
    208208        }
    209209        String createdAt = attrs.apply("created_at");
    210210        if (createdAt != null) {
    211             note.setCreatedAt(DateUtils.fromString(createdAt));
     211            note.setCreatedAt(DateUtils.parseInstant(createdAt));
    212212        }
    213213        return note;
  • trunk/src/org/openstreetmap/josm/io/NoteWriter.java

    r17333 r17712  
    1313import org.openstreetmap.josm.data.osm.NoteData;
    1414import org.openstreetmap.josm.data.osm.User;
    15 import org.openstreetmap.josm.tools.date.DateUtils;
    1615
    1716/**
     
    5352            out.print("lat=\"" + LatLon.cDdHighPrecisionFormatter.format(ll.lat()) + "\" ");
    5453            out.print("lon=\"" + LatLon.cDdHighPrecisionFormatter.format(ll.lon()) + "\" ");
    55             out.print("created_at=\"" + DateUtils.fromDate(note.getCreatedAt()) + "\" ");
     54            out.print("created_at=\"" + note.getCreatedAt() + "\" ");
    5655            if (note.getClosedAt() != null) {
    57                 out.print("closed_at=\"" + DateUtils.fromDate(note.getClosedAt()) + "\" ");
     56                out.print("closed_at=\"" + note.getClosedAt() + "\" ");
    5857            }
    5958
     
    7271        out.print("    <comment");
    7372        out.print(" action=\"" + comment.getNoteAction() + "\" ");
    74         out.print("timestamp=\"" + DateUtils.fromDate(comment.getCommentTimestamp()) + "\" ");
     73        out.print("timestamp=\"" + comment.getCommentTimestamp() + "\" ");
    7574        if (comment.getUser() != null && !comment.getUser().equals(User.getAnonymous())) {
    7675            out.print("uid=\"" + comment.getUser().getId() + "\" ");
  • trunk/src/org/openstreetmap/josm/tools/date/DateUtils.java

    r17653 r17712  
    77import java.time.DateTimeException;
    88import java.time.Instant;
     9import java.time.ZoneId;
    910import java.time.ZoneOffset;
    1011import java.time.ZonedDateTime;
    1112import java.time.format.DateTimeFormatter;
    1213import java.time.format.DateTimeParseException;
     14import java.time.format.FormatStyle;
    1315import java.util.Date;
    1416import java.util.Locale;
     
    270272
    271273    /**
     274     * Returns the date formatter to be used for current user, based on user preferences.
     275     * @param dateStyle The date style. Ignored if "ISO dates" option is set.
     276     * @return The date format
     277     */
     278    public static DateTimeFormatter getDateFormatter(FormatStyle dateStyle) {
     279        DateTimeFormatter formatter = PROP_ISO_DATES.get()
     280                ? DateTimeFormatter.ISO_LOCAL_DATE
     281                : DateTimeFormatter.ofLocalizedDate(dateStyle);
     282        return formatter.withZone(ZoneId.systemDefault());
     283    }
     284
     285    /**
    272286     * Returns the date format used for GPX waypoints.
    273287     * @return the date format used for GPX waypoints
     
    305319            return DateFormat.getTimeInstance(timeStyle, Locale.getDefault());
    306320        }
     321    }
     322
     323    /**
     324     * Returns the time formatter to be used for current user, based on user preferences.
     325     * @param timeStyle The time style. Ignored if "ISO dates" option is set.
     326     * @return The time format
     327     */
     328    public static DateTimeFormatter getTimeFormatter(FormatStyle timeStyle) {
     329        DateTimeFormatter formatter = PROP_ISO_DATES.get()
     330                ? DateTimeFormatter.ISO_LOCAL_TIME
     331                : DateTimeFormatter.ofLocalizedTime(timeStyle);
     332        return formatter.withZone(ZoneId.systemDefault());
    307333    }
    308334
     
    337363
    338364    /**
     365     * Returns the date/time formatter to be used for current user, based on user preferences.
     366     * @param dateStyle The date style. Ignored if "ISO dates" option is set.
     367     * @param timeStyle The time style. Ignored if "ISO dates" option is set.
     368     * @return The date/time format
     369     */
     370    public static DateTimeFormatter getDateTimeFormatter(FormatStyle dateStyle, FormatStyle timeStyle) {
     371        DateTimeFormatter formatter = PROP_ISO_DATES.get()
     372                ? DateTimeFormatter.ISO_LOCAL_DATE_TIME
     373                : DateTimeFormatter.ofLocalizedDateTime(dateStyle, timeStyle);
     374        return formatter.withZone(ZoneId.systemDefault());
     375    }
     376
     377    /**
    339378     * Formats a date/time to be displayed to current user, based on user preferences.
    340379     * @param datetime The date/time to display. Must not be {@code null}
  • trunk/test/unit/org/openstreetmap/josm/data/notes/NoteCommentTest.java

    r17275 r17712  
    66import static org.junit.jupiter.api.Assertions.assertTrue;
    77
    8 import java.util.Date;
     8import java.time.Instant;
    99
    1010import org.junit.jupiter.api.extension.RegisterExtension;
     
    3131    @Test
    3232    void testNoteComment() {
    33         NoteComment comment = new NoteComment(new Date(), null, "foo", null, true);
     33        NoteComment comment = new NoteComment(Instant.now(), null, "foo", null, true);
    3434        assertEquals("foo", comment.toString());
    3535        assertTrue(comment.isNew());
  • trunk/test/unit/org/openstreetmap/josm/data/notes/NoteTest.java

    r17275 r17712  
    55import static org.junit.jupiter.api.Assertions.assertNotEquals;
    66
    7 import java.util.Date;
     7import java.time.Instant;
    88
    99import org.junit.jupiter.api.extension.RegisterExtension;
     
    3636        Note note = new Note(LatLon.ZERO);
    3737        assertEquals("Note 0: null", note.toString());
    38         note.addComment(new NoteComment(new Date(), null, "foo", null, true));
     38        note.addComment(new NoteComment(Instant.now(), null, "foo", null, true));
    3939        assertEquals("Note 0: foo", note.toString());
    4040    }
     
    6565            .withPrefabValues(LatLon.class, LatLon.ZERO, new LatLon(1, 1))
    6666            .withPrefabValues(NoteComment.class,
    67                     new NoteComment(new Date(), null, "foo", null, true),
    68                     new NoteComment(new Date(), null, "bar", null, false))
     67                    new NoteComment(Instant.now(), null, "foo", null, true),
     68                    new NoteComment(Instant.now(), null, "bar", null, false))
    6969            .verify();
    7070    }
  • trunk/test/unit/org/openstreetmap/josm/gui/dialogs/NotesDialogTest.java

    r17269 r17712  
    44import static org.junit.jupiter.api.Assertions.assertEquals;
    55
    6 import java.util.Date;
     6import java.time.Instant;
    77
    88import javax.swing.JLabel;
     
    3535    void testMultiLineNoteRendering() {
    3636        Note note = new Note(LatLon.ZERO);
    37         note.setCreatedAt(new Date());
    38         note.addComment(new NoteComment(new Date(), User.createLocalUser(null), "foo\nbar\n\nbaz:\nfoo", null, false));
     37        note.setCreatedAt(Instant.now());
     38        note.addComment(new NoteComment(Instant.now(), User.createLocalUser(null), "foo\nbar\n\nbaz:\nfoo", null, false));
    3939        assertEquals("0: foo; bar; baz: foo",
    4040                ((JLabel) new NoteRenderer().getListCellRendererComponent(new JList<>(), note, 0, false, false)).getText());
  • trunk/test/unit/org/openstreetmap/josm/io/NoteReaderTest.java

    r17275 r17712  
    6464        assertEquals(1, list.size());
    6565        Note n = list.get(0);
    66         assertEquals(DateUtils.fromString("2013-04-24 08:08:51 UTC"), n.getClosedAt());
    67         assertEquals(DateUtils.fromString("2013-04-24 08:07:02 UTC"), n.getCreatedAt());
     66        assertEquals(DateUtils.parseInstant("2013-04-24 08:08:51 UTC"), n.getClosedAt());
     67        assertEquals(DateUtils.parseInstant("2013-04-24 08:07:02 UTC"), n.getCreatedAt());
    6868        assertEquals(4, n.getId());
    6969        assertEquals(new LatLon(36.7232991, 68.86415), n.getLatLon());
     
    7474        NoteComment c1 = comments.get(0);
    7575        assertEquals(c1, n.getFirstComment());
    76         assertEquals(DateUtils.fromString("2013-04-24 08:07:02 UTC"), c1.getCommentTimestamp());
     76        assertEquals(DateUtils.parseInstant("2013-04-24 08:07:02 UTC"), c1.getCommentTimestamp());
    7777        assertEquals(Action.OPENED, c1.getNoteAction());
    7878        assertEquals("test", c1.getText());
  • trunk/test/unit/org/openstreetmap/josm/tools/date/DateUtilsTest.java

    r17275 r17712  
    1010
    1111import java.text.DateFormat;
     12import java.time.Instant;
     13import java.time.format.FormatStyle;
    1214import java.util.Date;
    1315import java.util.Random;
     
    272274        assertNotSame(date, DateUtils.cloneDate(date));
    273275    }
     276
     277    /**
     278     * Unit test of {@link DateUtils#getDateTimeFormatter} method.
     279     */
     280    @Test
     281    void testDateTimeFormatter() {
     282        Instant instant = Instant.parse("2006-01-02T15:04:05Z");
     283        Boolean iso = DateUtils.PROP_ISO_DATES.get();
     284        try {
     285            assertNotNull(DateUtils.getDateFormatter(FormatStyle.SHORT).format(instant));
     286            assertNotNull(DateUtils.getTimeFormatter(FormatStyle.SHORT).format(instant));
     287            assertNotNull(DateUtils.getDateTimeFormatter(FormatStyle.SHORT, FormatStyle.SHORT).format(instant));
     288            DateUtils.PROP_ISO_DATES.put(!iso);
     289            assertNotNull(DateUtils.getDateFormatter(FormatStyle.SHORT).format(instant));
     290            assertNotNull(DateUtils.getTimeFormatter(FormatStyle.SHORT).format(instant));
     291            assertNotNull(DateUtils.getDateTimeFormatter(FormatStyle.SHORT, FormatStyle.SHORT).format(instant));
     292        } finally {
     293            DateUtils.PROP_ISO_DATES.put(iso);
     294        }
     295    }
    274296}
Note: See TracChangeset for help on using the changeset viewer.