Ignore:
Timestamp:
2009-11-14T17:59:10+01:00 (15 years ago)
Author:
Gubaer
Message:

fixed #3352: History doesn't get invalidated on upload?
fixed #3912: Extend history dialog to contain the currently modified version
new: zoom to node in list of nodes in history dialog (popup menu)
new: load history of node from node list in history dialog (popup menu or double click)
fixed: close all history dialogs when the number of layers drop to 0
fixed: implemented equals() and hashCode() on SimplePrimitiveId
fixed: history features now usePrimitiveId instead of long.

Location:
trunk/src/org/openstreetmap/josm/data/osm
Files:
6 edited

Legend:

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

    r2439 r2448  
    811811     * @throws IllegalArgumentException thrown if other is null.
    812812     * @throws DataIntegrityProblemException thrown if either this is new and other is not, or other is new and this is not
    813      * @throws DataIntegrityProblemException thrown if other is new and other.getId() != this.getId()
     813     * @throws DataIntegrityProblemException thrown if other isn't new and other.getId() != this.getId()
    814814     */
    815815    public void mergeFrom(OsmPrimitive other) {
     
    10251025    public abstract void updatePosition();
    10261026
     1027    /**
     1028     * Replies the unique primitive id for this primitive
     1029     *
     1030     * @return the unique primitive id for this primitive
     1031     */
     1032    public PrimitiveId getPrimitiveId() {
     1033        return new SimplePrimitiveId(getUniqueId(), getType());
     1034    }
    10271035}
    10281036
  • trunk/src/org/openstreetmap/josm/data/osm/SimplePrimitiveId.java

    r2399 r2448  
    2020    }
    2121
     22    @Override
     23    public int hashCode() {
     24        final int prime = 31;
     25        int result = 1;
     26        result = prime * result + (int) (id ^ (id >>> 32));
     27        result = prime * result + ((type == null) ? 0 : type.hashCode());
     28        return result;
     29    }
    2230
    23 
     31    @Override
     32    public boolean equals(Object obj) {
     33        if (this == obj)
     34            return true;
     35        if (obj == null)
     36            return false;
     37        if (getClass() != obj.getClass())
     38            return false;
     39        SimplePrimitiveId other = (SimplePrimitiveId) obj;
     40        if (id != other.id)
     41            return false;
     42        if (type == null) {
     43            if (other.type != null)
     44                return false;
     45        } else if (!type.equals(other.type))
     46            return false;
     47        return true;
     48    }
    2449}
  • trunk/src/org/openstreetmap/josm/data/osm/history/History.java

    r2242 r2448  
    99import java.util.Date;
    1010import java.util.List;
    11 import java.util.NoSuchElementException;
    1211
    1312import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    14 
     13import org.openstreetmap.josm.data.osm.PrimitiveId;
     14import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
     15
     16/**
     17 * Represents the history of an OSM primitive. The history consists
     18 * of a list of object snapshots with a specific version.
     19 *
     20 */
    1521public class History{
    1622    private static interface FilterPredicate {
     
    2531            }
    2632        }
    27         return new History(history.id, out);
    28     }
    29 
     33        return new History(history.id, history.type,out);
     34    }
     35
     36    /** the list of object snapshots */
    3037    private ArrayList<HistoryOsmPrimitive> versions;
    31     long id;
    32 
    33     protected History(long id, List<HistoryOsmPrimitive> versions) {
     38    /** the object id */
     39    private long id;
     40    private OsmPrimitiveType type;
     41
     42    /**
     43     * Creates a new history for an OSM primitive
     44     *
     45     * @param id the id. >0 required.
     46     * @param type the primitive type. Must not be null.
     47     * @param versions a list of versions. Can be null.
     48     * @throws IllegalArgumentException thrown if id <= 0
     49     * @throws IllegalArgumentException if type is null
     50     *
     51     */
     52    protected History(long id, OsmPrimitiveType type, List<HistoryOsmPrimitive> versions) {
     53        if (id <= 0)
     54            throw new IllegalArgumentException(tr("Parameter ''{0}'' > 0 expected, got {1}", "id", id));
     55        if (type == null)
     56            throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "type"));
    3457        this.id = id;
     58        this.type = type;
    3559        this.versions = new ArrayList<HistoryOsmPrimitive>();
    36         this.versions.addAll(versions);
     60        if (versions != null) {
     61            this.versions.addAll(versions);
     62        }
    3763    }
    3864
     
    4773                }
    4874        );
    49         return new History(id, copy);
     75        return new History(id, type, copy);
    5076    }
    5177
     
    6086                }
    6187        );
    62         return new History(id, copy);
     88        return new History(id, type,copy);
    6389    }
    6490
     
    139165    public long getId() {
    140166        return id;
     167    }
     168
     169    /**
     170     * Replies the primitive id for this history.
     171     *
     172     * @return the primitive id
     173     */
     174    public PrimitiveId getPrimitmiveId() {
     175        return new SimplePrimitiveId(id, type);
    141176    }
    142177
     
    149184    }
    150185
     186    /**
     187     * Replies the history primitive with version <code>version</code>. null,
     188     * if no such primitive exists.
     189     *
     190     * @param version the version
     191     * @return the history primitive with version <code>version</code>
     192     */
    151193    public HistoryOsmPrimitive getByVersion(long version) {
    152194        for (HistoryOsmPrimitive primitive: versions) {
     
    154196                return primitive;
    155197        }
    156         throw new NoSuchElementException(tr("There's no primitive with version {0} in this history.", version));
     198        return null;
    157199    }
    158200
    159201    public HistoryOsmPrimitive getByDate(Date date) {
    160         sortAscending();
    161 
    162         if (versions.isEmpty())
    163             throw new NoSuchElementException(tr("There's no version valid at date ''{0}'' in this history.", date));
    164         if (get(0).getTimestamp().compareTo(date)> 0)
    165             throw new NoSuchElementException(tr("There's no version valid at date ''{0}'' in this history.", date));
    166         for (int i = 1; i < versions.size();i++) {
    167             if (get(i-1).getTimestamp().compareTo(date) <= 0
    168                     && get(i).getTimestamp().compareTo(date) >= 0)
    169                 return get(i);
    170         }
    171         return getLatest();
     202        History h = sortAscending();
     203
     204        if (h.versions.isEmpty())
     205            return null;
     206        if (h.get(0).getTimestamp().compareTo(date)> 0)
     207            return null;
     208        for (int i = 1; i < h.versions.size();i++) {
     209            if (h.get(i-1).getTimestamp().compareTo(date) <= 0
     210                    && h.get(i).getTimestamp().compareTo(date) >= 0)
     211                return h.get(i);
     212        }
     213        return h.getLatest();
    172214    }
    173215
     
    180222    public HistoryOsmPrimitive getEarliest() {
    181223        if (isEmpty())
    182             throw new NoSuchElementException(tr("No earliest version found. History is empty."));
     224            return null;
    183225        return sortAscending().versions.get(0);
    184226    }
     
    186228    public HistoryOsmPrimitive getLatest() {
    187229        if (isEmpty())
    188             throw new NoSuchElementException(tr("No latest version found. History is empty."));
     230            return null;
    189231        return sortDescending().versions.get(0);
    190232    }
     
    199241
    200242    public OsmPrimitiveType getType() {
    201         if (isEmpty())
    202             throw new NoSuchElementException(tr("No type found. History is empty."));
    203         return versions.get(0).getType();
     243        return type;
    204244    }
    205245}
  • trunk/src/org/openstreetmap/josm/data/osm/history/HistoryDataSet.java

    r2163 r2448  
    66import java.util.ArrayList;
    77import java.util.HashMap;
    8 import java.util.NoSuchElementException;
    98import java.util.concurrent.CopyOnWriteArrayList;
     9import java.util.logging.Logger;
     10
     11import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
     12import org.openstreetmap.josm.data.osm.PrimitiveId;
     13import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
    1014
    1115/**
     
    1519 */
    1620public class HistoryDataSet {
     21    private final static Logger logger = Logger.getLogger(HistoryDataSet.class.getName());
    1722
    1823    /** the unique instance */
     
    3237
    3338    /** the history data */
    34     private HashMap<Long, ArrayList<HistoryOsmPrimitive>> data;
     39    private HashMap<PrimitiveId, ArrayList<HistoryOsmPrimitive>> data;
    3540    private CopyOnWriteArrayList<HistoryDataSetListener> listeners;
    3641
    3742    public HistoryDataSet() {
    38         data = new HashMap<Long, ArrayList<HistoryOsmPrimitive>>();
     43        data = new HashMap<PrimitiveId, ArrayList<HistoryOsmPrimitive>>();
    3944        listeners = new CopyOnWriteArrayList<HistoryDataSetListener>();
    4045    }
     
    5661    }
    5762
    58     protected void fireHistoryUpdated(long id) {
     63    protected void fireHistoryUpdated(SimplePrimitiveId id) {
    5964        for (HistoryDataSetListener l : listeners) {
    6065            l.historyUpdated(this, id);
     
    6469    /**
    6570     * Replies the history primitive for the primitive with id <code>id</code>
    66      * and version <code>version</code>
     71     * and version <code>version</code>. null, if no such primitive exists.
    6772     *
    68      * @param id the id of the primitive
    69      * @param version the version of the primitive
    70      * @return the history primitive for the primitive with id <code>id</code>
    71      * and version <code>version</code>
    72      * @throws NoSuchElementException thrown if this dataset doesn't include the respective
    73      * history primitive
     73     * @param id the id of the primitive. > 0 required.
     74     * @param type the primitive type. Must not be null.
     75     * @param version the version of the primitive. > 0 required
     76     * @return the history primitive for the primitive with id <code>id</code>,
     77     * type <code>type</code>, and version <code>version</code>
    7478     */
    75     public HistoryOsmPrimitive get(long id, long version) throws NoSuchElementException{
    76         ArrayList<HistoryOsmPrimitive> versions = data.get(id);
     79    public HistoryOsmPrimitive get(long id, OsmPrimitiveType type, long version){
     80        if (id <= 0)
     81            throw new IllegalArgumentException(tr("Parameter ''{0}'' > 0 expected, got {1}", "id", id));
     82        if (type == null)
     83            throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "type"));
     84        if (version <= 0)
     85            throw new IllegalArgumentException(tr("Parameter ''{0}'' > 0 expected, got {1}", "version", version));
     86
     87        SimplePrimitiveId pid = new SimplePrimitiveId(id, type);
     88        ArrayList<HistoryOsmPrimitive> versions = data.get(pid);
    7789        if (versions == null)
    78             throw new NoSuchElementException(tr("Didn't find an primitive with id {0} in this dataset", id));
    79 
     90            return null;
    8091        for (HistoryOsmPrimitive primitive: versions) {
    8192            if (primitive.matches(id, version))
    8293                return primitive;
    8394        }
    84         throw new NoSuchElementException(tr("Didn't find an primitive with id {0} and version {1} in this dataset", id, version));
     95        return null;
    8596    }
    8697
     
    91102     */
    92103    public void put(HistoryOsmPrimitive primitive) {
    93         if (data.get(primitive.getId()) == null) {
    94             data.put(primitive.getId(), new ArrayList<HistoryOsmPrimitive>());
     104        SimplePrimitiveId id = new SimplePrimitiveId(primitive.getId(), primitive.getType());
     105        if (data.get(id) == null) {
     106            data.put(id, new ArrayList<HistoryOsmPrimitive>());
    95107        }
    96         data.get(primitive.getId()).add(primitive);
    97         fireHistoryUpdated(primitive.getId());
     108        data.get(id).add(primitive);
     109        fireHistoryUpdated(id);
    98110    }
    99111
    100112    /**
    101113     * Replies the history for a given primitive with id <code>id</code>
     114     * and type <code>type</code>.
    102115     *
    103      * @param id the id
    104      * @return the history
     116     * @param id the id the if of the primitive. > 0 required
     117     * @param type the type of the primitive. Must not be null.
     118     * @return the history. null, if there isn't a history for <code>id</code> and
     119     * <code>type</code>.
     120     * @throws IllegalArgumentException thrown if id <= 0
     121     * @throws IllegalArgumentException thrown if type is null
    105122     */
    106     public History getHistory(long id) {
    107         ArrayList<HistoryOsmPrimitive> versions = data.get(id);
     123    public History getHistory(long id, OsmPrimitiveType type) throws IllegalArgumentException{
     124        if (id <= 0)
     125            throw new IllegalArgumentException(tr("Parameter ''{0}'' > 0 expected, got {1}", "id", id));
     126        if (type == null)
     127            throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "type"));
     128        SimplePrimitiveId pid = new SimplePrimitiveId(id, type);
     129        return getHistory(pid);
     130    }
     131
     132    /**
     133     * Replies the history for a primitive with id <code>id</code>. null, if no
     134     * such history exists.
     135     *
     136     * @param pid the primitive id. Must not be null.
     137     * @return the history for a primitive with id <code>id</code>. null, if no
     138     * such history exists
     139     * @throws IllegalArgumentException thrown if pid is null
     140     */
     141    public History getHistory(PrimitiveId pid) throws IllegalArgumentException{
     142        if (pid == null)
     143            throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "pid"));
     144        ArrayList<HistoryOsmPrimitive> versions = data.get(pid);
    108145        if (versions == null)
    109146            return null;
    110         return new History(id, versions);
     147        return new History(pid.getUniqueId(), pid.getType(), versions);
    111148    }
    112149
     
    119156        if (other == null)
    120157            return;
    121         for (Long id : other.data.keySet()) {
     158        for (PrimitiveId id : other.data.keySet()) {
    122159            this.data.put(id, other.data.get(id));
    123160        }
    124         fireHistoryUpdated(0);
     161        fireHistoryUpdated(null);
    125162    }
    126163}
  • trunk/src/org/openstreetmap/josm/data/osm/history/HistoryDataSetListener.java

    r2019 r2448  
    22package org.openstreetmap.josm.data.osm.history;
    33
     4import org.openstreetmap.josm.data.osm.PrimitiveId;
     5
    46public interface HistoryDataSetListener {
    5     void historyUpdated(HistoryDataSet source, long primitiveId);
     7    void historyUpdated(HistoryDataSet source, PrimitiveId id);
    68}
  • trunk/src/org/openstreetmap/josm/data/osm/history/HistoryOsmPrimitive.java

    r2242 r2448  
    1010
    1111import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
     12import org.openstreetmap.josm.data.osm.PrimitiveId;
     13import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
    1214
    1315/**
     
    4547     * @param user  the user (! null required)
    4648     * @param uid the user id (> 0 required)
    47      * @param changesetId the changeset id (> 0 required)
     49     * @param changesetId the changeset id (may be null if the changeset isn't known)
    4850     * @param timestamp the timestamp (! null required)
    4951     *
     
    5355        ensurePositiveLong(id, "id");
    5456        ensurePositiveLong(version, "version");
    55         if(uid != -1) /* allow -1 for anonymous users */
     57        if(uid != -1) {
    5658            ensurePositiveLong(uid, "uid");
    57         ensurePositiveLong(changesetId, "changesetId");
     59        }
    5860        ensureNotNull(user, "user");
    5961        ensureNotNull(timestamp, "timestamp");
     
    6365        this.user = user;
    6466        this.uid = uid;
     67        // FIXME: restrict to IDs > 0 as soon as OsmPrimitive holds the
     68        // changeset id too
    6569        this.changesetId  = changesetId;
    6670        this.timestamp = timestamp;
     
    7175        return id;
    7276    }
     77
     78    public PrimitiveId getPrimitiveId() {
     79        return new SimplePrimitiveId(id, getType());
     80    }
     81
    7382    public boolean isVisible() {
    7483        return visible;
     
    103112    public int compareTo(HistoryOsmPrimitive o) {
    104113        if (this.id != o.id)
    105             throw new ClassCastException(tr("Can't compare primitive with ID ''{0}'' to primitive with ID ''{1}''.", o.id, this.id));
     114            throw new ClassCastException(tr("Can''t compare primitive with ID ''{0}'' to primitive with ID ''{1}''.", o.id, this.id));
    106115        return new Long(this.version).compareTo(o.version);
    107116    }
     
    121130    public Map<String,String> getTags() {
    122131        return Collections.unmodifiableMap(tags);
     132    }
     133
     134    /**
     135     * Sets the tags for this history primitive. Removes all
     136     * tags if <code>tags</code> is null.
     137     *
     138     * @param tags the tags. May be null.
     139     */
     140    public void setTags(Map<String,String> tags) {
     141        if (tags == null) {
     142            this.tags = new HashMap<String, String>();
     143        } else {
     144            this.tags = new HashMap<String, String>(tags);
     145        }
    123146    }
    124147
Note: See TracChangeset for help on using the changeset viewer.