Changeset 14961 in josm


Ignore:
Timestamp:
2019-04-04T14:35:46+02:00 (5 years ago)
Author:
GerdP
Message:

reduce code duplication, add unit test for new access methods

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/ChangesetDataSet.java

    r14947 r14961  
    192192     */
    193193    public ChangesetDataSetEntry getFirstEntry(PrimitiveId id) {
     194        return getEntry(id, 0);
     195    }
     196
     197    /**
     198     * Replies the last {@link ChangesetDataSetEntry} with id <code>id</code> from this dataset.
     199     * null, if there is no such primitive in the data set.
     200     * @param id the id
     201     * @return the last {@link ChangesetDataSetEntry} with id <code>id</code> from this dataset or null.
     202     * @since 14946
     203     */
     204    public ChangesetDataSetEntry getLastEntry(PrimitiveId id) {
     205        return getEntry(id, 1);
     206    }
     207
     208    private ChangesetDataSetEntry getEntry(PrimitiveId id, int n) {
    194209        if (id == null)
    195210            return null;
     
    199214        if (val instanceof ChangesetDataSetEntry[]) {
    200215            ChangesetDataSetEntry[] entries = (ChangesetDataSetEntry[]) val;
    201             return entries[0];
    202         } else {
    203             return (ChangesetDataSetEntry) val;
    204         }
    205     }
    206 
    207     /**
    208      * Replies the last {@link ChangesetDataSetEntry} with id <code>id</code> from this dataset.
    209      * null, if there is no such primitive in the data set.
    210      * @param id the id
    211      * @return the last {@link ChangesetDataSetEntry} with id <code>id</code> from this dataset or null.
    212      * @since 14946
    213      */
    214     public ChangesetDataSetEntry getLastEntry(PrimitiveId id) {
    215         if (id == null)
    216             return null;
    217         Object val = entryMap.get(id);
    218         if (val == null)
    219             return null;
    220         if (val instanceof ChangesetDataSetEntry[]) {
    221             ChangesetDataSetEntry[] entries = (ChangesetDataSetEntry[]) val;
    222             return entries[1];
     216            return entries[n];
    223217        } else {
    224218            return (ChangesetDataSetEntry) val;
  • trunk/test/unit/org/openstreetmap/josm/data/osm/ChangesetDataSetTest.java

    r14946 r14961  
    5757
    5858    /**
     59     * Unit test of method {@link ChangesetDataSet#getFirstEntry(PrimitiveId)} and {@link ChangesetDataSet#getLastEntry(PrimitiveId)}.
     60     */
     61    @Test
     62    public void testGetEntry() {
     63        final ChangesetDataSet cds = new ChangesetDataSet();
     64        HistoryNode prim1 = new HistoryNode(1, 1, true, User.getAnonymous(), 1, new Date(), LatLon.ZERO);
     65        cds.put(prim1, ChangesetModificationType.CREATED);
     66        HistoryNode prim2 = new HistoryNode(1, 2, true, User.getAnonymous(), 1, new Date(), LatLon.ZERO);
     67        prim2.put("highway", "stop");
     68        cds.put(prim2, ChangesetModificationType.UPDATED);
     69        assertEquals(prim1, cds.getFirstEntry(prim1.getPrimitiveId()).getPrimitive());
     70        assertEquals(prim2, cds.getLastEntry(prim1.getPrimitiveId()).getPrimitive());
     71        HistoryNode prim3 = new HistoryNode(1, 3, false, User.getAnonymous(), 1, new Date(), null);
     72
     73        cds.put(prim3, ChangesetModificationType.DELETED);
     74        assertEquals(prim1, cds.getFirstEntry(prim1.getPrimitiveId()).getPrimitive());
     75        assertEquals(prim3, cds.getLastEntry(prim1.getPrimitiveId()).getPrimitive());
     76    }
     77
     78    /**
    5979     * Unit test of {@link ChangesetModificationType} enum.
    6080     */
Note: See TracChangeset for help on using the changeset viewer.