Changeset 17749 in josm


Ignore:
Timestamp:
2021-04-11T21:56:50+02:00 (4 years ago)
Author:
simon04
Message:

see #14176 - Migrate OsmPrimitive to Instant

Location:
trunk
Files:
18 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmChangeTask.java

    r16453 r17749  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.time.Instant;
    67import java.util.Arrays;
    78import java.util.Date;
     
    112113                // A changeset does not contain all referred primitives, this is the map of incomplete ones
    113114                // For each incomplete primitive, we'll have to get its state at date it was referred
    114                 Map<OsmPrimitive, Date> toLoad = new HashMap<>();
     115                Map<OsmPrimitive, Instant> toLoad = new HashMap<>();
    115116                for (OsmPrimitive p : downloadedData.allNonDeletedPrimitives()) {
    116117                    if (p.isIncomplete()) {
    117                         Date timestamp = p.getReferrers().stream()
     118                        Instant timestamp = p.getReferrers().stream()
    118119                                .filter(ref -> !ref.isTimestampEmpty())
    119120                                .findFirst()
    120                                 .map(AbstractPrimitive::getTimestamp)
     121                                .map(AbstractPrimitive::getInstant)
    121122                                .orElse(null);
    122123                        toLoad.put(p, timestamp);
     
    138139    private static final class HistoryLoaderAndListener extends HistoryLoadTask implements HistoryDataSetListener {
    139140
    140         private final Map<OsmPrimitive, Date> toLoad;
    141 
    142         private HistoryLoaderAndListener(Map<OsmPrimitive, Date> toLoad) {
     141        private final Map<OsmPrimitive, Instant> toLoad;
     142
     143        private HistoryLoaderAndListener(Map<OsmPrimitive, Instant> toLoad) {
    143144            this.toLoad = toLoad;
    144145            this.setChangesetDataNeeded(false);
     
    150151        @Override
    151152        public void historyUpdated(HistoryDataSet source, PrimitiveId id) {
    152             Map<OsmPrimitive, Date> toLoadNext = new HashMap<>();
    153             for (Iterator<Entry<OsmPrimitive, Date>> it = toLoad.entrySet().iterator(); it.hasNext();) {
    154                 Entry<OsmPrimitive, Date> entry = it.next();
     153            Map<OsmPrimitive, Instant> toLoadNext = new HashMap<>();
     154            for (Iterator<Entry<OsmPrimitive, Instant>> it = toLoad.entrySet().iterator(); it.hasNext();) {
     155                Entry<OsmPrimitive, Instant> entry = it.next();
    155156                OsmPrimitive p = entry.getKey();
    156157                History history = source.getHistory(p.getPrimitiveId());
    157                 Date date = entry.getValue();
     158                Instant date = entry.getValue();
    158159                // If the history has been loaded and a timestamp is known
    159160                if (history != null && date != null) {
    160161                    // Lookup for the primitive version at the specified timestamp
    161                     HistoryOsmPrimitive hp = history.getByDate(date);
     162                    HistoryOsmPrimitive hp = history.getByDate(Date.from(date));
    162163                    if (hp != null) {
    163164                        PrimitiveData data;
  • trunk/src/org/openstreetmap/josm/data/osm/AbstractPrimitive.java

    r17589 r17749  
    55
    66import java.text.MessageFormat;
     7import java.time.Instant;
    78import java.util.Arrays;
    89import java.util.Collection;
     
    297298    }
    298299
     300    @Deprecated
    299301    @Override
    300302    public void setTimestamp(Date timestamp) {
     
    303305
    304306    @Override
     307    public void setInstant(Instant timestamp) {
     308        this.timestamp = (int) timestamp.getEpochSecond();
     309    }
     310
     311    @Override
    305312    public void setRawTimestamp(int timestamp) {
    306313        this.timestamp = timestamp;
    307314    }
    308315
     316    @Deprecated
    309317    @Override
    310318    public Date getTimestamp() {
    311         return new Date(TimeUnit.SECONDS.toMillis(Integer.toUnsignedLong(timestamp)));
     319        return Date.from(getInstant());
     320    }
     321
     322    @Override
     323    public Instant getInstant() {
     324        return Instant.ofEpochSecond(Integer.toUnsignedLong(timestamp));
    312325    }
    313326
  • trunk/src/org/openstreetmap/josm/data/osm/Changeset.java

    r17717 r17749  
    103103        final Changeset changeset = new Changeset(primitive.getChangesetId());
    104104        changeset.setUser(primitive.getUser());
    105         changeset.setCreatedAt(primitive.getTimestamp().toInstant()); // not accurate in all cases
     105        changeset.setCreatedAt(primitive.getInstant()); // not accurate in all cases
    106106        return changeset;
    107107    }
  • trunk/src/org/openstreetmap/josm/data/osm/IPrimitive.java

    r17459 r17749  
    22package org.openstreetmap.josm.data.osm;
    33
     4import java.time.Instant;
    45import java.util.Date;
    56import java.util.List;
     
    276277     * @return date of last modification
    277278     * @see #setTimestamp
    278      */
     279     * @deprecated Use {@link #getInstant}
     280     */
     281    @Deprecated
    279282    Date getTimestamp();
    280283
     
    284287     * used to check against edit conflicts.
    285288     *
     289     * @return date of last modification
     290     * @see #getInstant
     291     */
     292    Instant getInstant();
     293
     294    /**
     295     * Time of last modification to this object. This is not set by JOSM but
     296     * read from the server and delivered back to the server unmodified. It is
     297     * used to check against edit conflicts.
     298     *
    286299     * @return last modification as timestamp
    287300     * @see #setRawTimestamp
     
    293306     * @param timestamp date of last modification
    294307     * @see #getTimestamp
    295      */
     308     * @deprecated Use {@link #setInstant}
     309     */
     310    @Deprecated
    296311    void setTimestamp(Date timestamp);
     312
     313    /**
     314     * Sets time of last modification to this object
     315     * @param timestamp date of last modification
     316     * @see #getInstant
     317     */
     318    void setInstant(Instant timestamp);
    297319
    298320    /**
  • trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java

    r17584 r17749  
    301301    }
    302302
     303    @Deprecated
    303304    @Override
    304305    public void setTimestamp(Date timestamp) {
  • trunk/src/org/openstreetmap/josm/data/osm/history/HistoryOsmPrimitive.java

    r16913 r17749  
    9696     */
    9797    protected HistoryOsmPrimitive(OsmPrimitive p) {
    98         this(p.getId(), p.getVersion(), p.isVisible(), p.getUser(), p.getChangesetId(), p.getTimestamp());
     98        this(p.getId(), p.getVersion(), p.isVisible(), p.getUser(), p.getChangesetId(), Date.from(p.getInstant()));
    9999    }
    100100
     
    364364            Logging.log(Logging.LEVEL_ERROR, "Cannot change visibility for "+data+':', e);
    365365        }
    366         data.setTimestamp(timestamp);
     366        data.setInstant(timestamp.toInstant());
    367367        data.setKeys(tags);
    368368        data.setOsmId(id, (int) version);
  • trunk/src/org/openstreetmap/josm/data/osm/search/SearchCompiler.java

    r17747 r17749  
    741741            String mv;
    742742            if ("timestamp".equals(key) && osm instanceof OsmPrimitive) {
    743                 mv = DateUtils.fromTimestamp(((OsmPrimitive) osm).getRawTimestamp());
     743                mv = ((OsmPrimitive) osm).getInstant().toString();
    744744            } else {
    745745                mv = osm.get(key);
     
    14931493            try {
    14941494                // if min timestamp is empty: use lowest possible date
    1495                 minDate = DateUtils.fromString(rangeA1.isEmpty() ? "1980" : rangeA1).getTime();
     1495                minDate = DateUtils.parseInstant(rangeA1.isEmpty() ? "1980" : rangeA1).toEpochMilli();
    14961496            } catch (UncheckedParseException | DateTimeException ex) {
    14971497                throw new SearchParseError(tr("Cannot parse timestamp ''{0}''", rangeA1), ex);
     
    14991499            try {
    15001500                // if max timestamp is empty: use "now"
    1501                 maxDate = rangeA2.isEmpty() ? System.currentTimeMillis() : DateUtils.fromString(rangeA2).getTime();
     1501                maxDate = rangeA2.isEmpty() ? System.currentTimeMillis() : DateUtils.parseInstant(rangeA2).toEpochMilli();
    15021502            } catch (UncheckedParseException | DateTimeException ex) {
    15031503                throw new SearchParseError(tr("Cannot parse timestamp ''{0}''", rangeA2), ex);
     
    15081508        @Override
    15091509        protected Long getNumber(OsmPrimitive osm) {
    1510             return osm.getTimestamp().getTime();
     1510            return osm.getInstant().toEpochMilli();
    15111511        }
    15121512
  • trunk/src/org/openstreetmap/josm/gui/dialogs/InspectPrimitiveDataText.java

    r17585 r17749  
    154154        add(tr("Data Set: "), Integer.toHexString(o.getDataSet().hashCode()));
    155155        add(tr("Edited at: "), o.isTimestampEmpty() ? tr("<new object>")
    156                 : DateUtils.fromTimestamp(o.getRawTimestamp()));
     156                : o.getInstant().toString());
    157157        add(tr("Edited by: "), o.getUser() == null ? tr("<new object>")
    158158                : getNameAndId(o.getUser().getName(), o.getUser().getId()));
  • trunk/src/org/openstreetmap/josm/gui/history/VersionInfoPanel.java

    r14463 r17749  
    236236     */
    237237    public void update(final OsmPrimitive primitive, final boolean isLatest) {
    238         update(Changeset.fromPrimitive(primitive), isLatest, primitive.getTimestamp(), primitive.getVersion(), primitive.getPrimitiveId());
     238        Date timestamp = Date.from(primitive.getInstant());
     239        update(Changeset.fromPrimitive(primitive), isLatest, timestamp, primitive.getVersion(), primitive.getPrimitiveId());
    239240    }
    240241
  • trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java

    r17715 r17749  
    2424import java.io.IOException;
    2525import java.time.DateTimeException;
    26 import java.time.Instant;
    2726import java.util.ArrayList;
    2827import java.util.Arrays;
     
    877876                wpt.setTimeInMillis(time);
    878877            } else if ((v = gpxVal(n, GpxConstants.PT_TIME)) != null) {
    879                 wpt.setTimeInMillis(DateUtils.tsFromString(v));
     878                wpt.setInstant(DateUtils.parseInstant(v));
    880879            } else if (!n.isTimestampEmpty()) {
    881                 wpt.setInstant(Instant.ofEpochSecond(Integer.toUnsignedLong(n.getRawTimestamp())));
     880                wpt.setInstant(n.getInstant());
    882881            }
    883882        } catch (UncheckedParseException | DateTimeException e) {
  • trunk/src/org/openstreetmap/josm/gui/layer/gpx/ConvertFromGpxLayerAction.java

    r17715 r17749  
    99import java.time.Instant;
    1010import java.util.ArrayList;
    11 import java.util.Date;
    1211import java.util.List;
    1312import java.util.Map;
     
    135134                    p.put(GpxConstants.GPX_PREFIX + key, String.valueOf(date));
    136135                }
    137                 p.setTimestamp(Date.from(date));
     136                p.setInstant(date);
    138137            }
    139138        }
  • trunk/src/org/openstreetmap/josm/io/AbstractReader.java

    r17166 r17749  
    431431        }
    432432        try {
    433             int timestamp = timestampCache.computeIfAbsent(time, t -> (int) (DateUtils.tsFromString(t) / 1000));
     433            int timestamp = timestampCache.computeIfAbsent(time, t -> (int) (DateUtils.parseInstant(t).getEpochSecond()));
    434434            current.setRawTimestamp(timestamp);
    435435        } catch (UncheckedParseException | DateTimeException e) {
  • trunk/src/org/openstreetmap/josm/io/DiffResultProcessor.java

    r17717 r17749  
    99import java.util.Collection;
    1010import java.util.Collections;
    11 import java.util.Date;
    1211import java.util.HashMap;
    1312import java.util.HashSet;
     
    153152                    // TODO is there a way to obtain the timestamp for non-closed changesets?
    154153                    Instant instant = Utils.firstNonNull(cs.getClosedAt(), Instant.now());
    155                     p.setTimestamp(Date.from(instant));
     154                    p.setInstant(instant);
    156155                }
    157156            }
  • trunk/src/org/openstreetmap/josm/io/OsmWriter.java

    r17717 r17749  
    366366            }
    367367            if (!osm.isTimestampEmpty()) {
    368                 out.print(" timestamp='"+DateUtils.fromTimestamp(osm.getRawTimestamp())+'\'');
     368                out.print(" timestamp='"+osm.getInstant()+'\'');
    369369            }
    370370            // user and visible added with 0.4 API
  • trunk/test/unit/org/openstreetmap/josm/data/osm/DataSetMergerTest.java

    r17346 r17749  
    1414import java.time.Instant;
    1515import java.util.Arrays;
    16 import java.util.Date;
    1716
    1817import org.junit.jupiter.api.AfterEach;
     
    2524import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    2625import org.openstreetmap.josm.testutils.JOSMTestRules;
    27 import org.openstreetmap.josm.tools.date.DateUtils;
    2826
    2927import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     
    307305        n.put("key1", "value1");
    308306        n.setUser(myUser);
    309         n.setTimestamp(new Date());
     307        n.setInstant(Instant.now());
    310308
    311309        my.addPrimitive(n);
     
    314312        n1.setCoor(LatLon.ZERO);
    315313        n1.put("key1", "value1");
    316         n1.setTimestamp(Date.from(Instant.now().plusSeconds(3600)));
     314        n1.setInstant(Instant.now().plusSeconds(3600));
    317315        n1.setUser(theirUser);
    318316        their.addPrimitive(n1);
     
    346344        n1.setOsmId(1, 1);
    347345        n1.put("key1", "value1");
    348         n1.setTimestamp(new Date());
     346        n1.setInstant(Instant.now());
    349347        their.addPrimitive(n1);
    350348
     
    646644        User user = User.createOsmUser(1111, "their");
    647645        theirWay.setUser(user);
    648         theirWay.setTimestamp(new Date());
     646        theirWay.setInstant(Instant.now());
    649647        their.addPrimitive(theirWay);
    650648
     
    699697        User user = User.createOsmUser(1111, "their");
    700698        theirWay.setUser(user);
    701         theirWay.setTimestamp(new Date());
     699        theirWay.setInstant(Instant.now());
    702700        their.addPrimitive(theirWay);
    703701
     
    754752        theirWay.addNode(tn3);
    755753        theirWay.setUser(User.createOsmUser(1111, "their"));
    756         theirWay.setTimestamp(new Date());
     754        theirWay.setInstant(Instant.now());
    757755        their.addPrimitive(theirWay);
    758756
     
    11821180        Node nA = new Node(2848219691L, 1);
    11831181        nA.setCoor(LatLon.ZERO);
    1184         nA.setTimestamp(DateUtils.fromString("2014-05-10T14:25:40Z"));
     1182        nA.setInstant(Instant.parse("2014-05-10T14:25:40Z"));
    11851183        nA.setChangesetId(22251108);
    11861184        nA.setUser(User.createOsmUser(385987, "yaho"));
  • trunk/test/unit/org/openstreetmap/josm/data/osm/search/SearchCompilerTest.java

    r17746 r17749  
    1313import java.nio.file.Files;
    1414import java.nio.file.Paths;
     15import java.time.Instant;
    1516import java.util.Arrays;
    1617import java.util.Collection;
     
    4445import org.openstreetmap.josm.testutils.JOSMTestRules;
    4546import org.openstreetmap.josm.tools.Logging;
    46 import org.openstreetmap.josm.tools.date.DateUtils;
    4747
    4848import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     
    449449    void testTimestamp() throws SearchParseError {
    450450        final Node n1 = new Node();
    451         n1.setTimestamp(DateUtils.fromString("2010-01-22"));
     451        n1.setInstant(Instant.parse("2010-01-22T00:00:00Z"));
    452452        assertTrue(SearchCompiler.compile("timestamp:2010/2011").match(n1));
    453453        assertTrue(SearchCompiler.compile("timestamp:2010-01/2011").match(n1));
     
    455455        assertFalse(SearchCompiler.compile("timestamp:2010-01-23/2011").match(n1));
    456456        assertFalse(SearchCompiler.compile("timestamp:2010/2010-01-21").match(n1));
    457         n1.setTimestamp(DateUtils.fromString("2016-01-22"));
     457        n1.setInstant(Instant.parse("2016-01-22T00:00:00Z"));
    458458        assertFalse(SearchCompiler.compile("timestamp:2010/2011").match(n1));
    459459    }
  • trunk/test/unit/org/openstreetmap/josm/io/OsmJsonReaderTest.java

    r17275 r17749  
    99import java.io.InputStream;
    1010import java.nio.charset.StandardCharsets;
     11import java.time.Instant;
    1112import java.util.Iterator;
    1213
     
    131132        assertEquals(1, n.getUniqueId());
    132133        assertEquals(new LatLon(2.0, -3.0), n.getCoor());
    133         assertEquals("2018-01-01T00:00:00Z", DateUtils.newIsoDateTimeFormat().format(n.getTimestamp()));
     134        assertEquals(Instant.parse("2018-01-01T00:00:00Z"), n.getInstant());
    134135        assertEquals(4, n.getVersion());
    135136        assertEquals(5, n.getChangesetId());
  • trunk/test/unit/org/openstreetmap/josm/io/OsmWriterTest.java

    r17717 r17749  
    2424import org.openstreetmap.josm.data.osm.DataSet;
    2525import org.openstreetmap.josm.data.osm.DownloadPolicy;
     26import org.openstreetmap.josm.data.osm.INode;
     27import org.openstreetmap.josm.data.osm.Node;
    2628import org.openstreetmap.josm.data.osm.NodeData;
    2729import org.openstreetmap.josm.data.osm.UploadPolicy;
     
    127129        }
    128130    }
     131
     132    /**
     133     * Unit test of {@link OsmWriter#visit(INode)}.
     134     * @throws IOException if an I/O error occurs
     135     */
     136    @Test
     137    void testNode() throws IOException {
     138        Node node = new Node(1, 42);
     139        node.setCoor(new LatLon(12., 34.));
     140        node.setInstant(Instant.parse("2006-05-10T18:27:47Z"));
     141        try (StringWriter stringWriter = new StringWriter();
     142             OsmWriter osmWriter = OsmWriterFactory.createOsmWriter(new PrintWriter(stringWriter), true, OsmWriter.DEFAULT_API_VERSION)) {
     143            osmWriter.visit(node);
     144            assertEquals("  <node id='1' timestamp='2006-05-10T18:27:47Z' visible='true' version='42' lat='12.0' lon='34.0' />\n",
     145                    stringWriter.toString().replace("\r", ""));
     146        }
     147    }
    129148}
Note: See TracChangeset for help on using the changeset viewer.