Ignore:
Timestamp:
2009-08-31T08:06:25+02:00 (15 years ago)
Author:
Gubaer
Message:

Improved history feature

File:
1 edited

Legend:

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

    r1709 r2019  
    77import java.util.HashMap;
    88import java.util.NoSuchElementException;
     9import java.util.concurrent.CopyOnWriteArrayList;
    910
     11/**
     12 * A data set holding histories of OSM primitives.
     13 *
     14 *
     15 */
    1016public class HistoryDataSet {
    1117
     18    /** the unique instance */
    1219    private static HistoryDataSet historyDataSet;
    1320
     21    /**
     22     * Replies the unique instance of the history data set
     23     *
     24     * @return the unique instance of the history data set
     25     */
    1426    public static HistoryDataSet getInstance() {
    1527        if (historyDataSet == null) {
     
    1931    }
    2032
     33    /** the history data */
    2134    private HashMap<Long, ArrayList<HistoryOsmPrimitive>> data;
     35    private CopyOnWriteArrayList<HistoryDataSetListener> listeners;
    2236
    2337    public HistoryDataSet() {
    2438        data = new HashMap<Long, ArrayList<HistoryOsmPrimitive>>();
     39        listeners = new CopyOnWriteArrayList<HistoryDataSetListener>();
    2540    }
    2641
    27     public HistoryOsmPrimitive get(long id, long version) {
     42    public void addHistoryDataSetListener(HistoryDataSetListener listener) {
     43        synchronized(listeners) {
     44            if (!listeners.contains(listener)) {
     45                listeners.add(listener);
     46            }
     47        }
     48    }
     49
     50    public void removeHistoryDataSetListener(HistoryDataSetListener listener) {
     51        synchronized(listeners) {
     52            if (listeners.contains(listener)) {
     53                listeners.remove(listener);
     54            }
     55        }
     56    }
     57
     58    protected void fireHistoryUpdated(long id) {
     59        for (HistoryDataSetListener l : listeners) {
     60            l.historyUpdated(this, id);
     61        }
     62    }
     63
     64    /**
     65     * Replies the history primitive for the primitive with id <code>id</code>
     66     * and version <code>version</code>
     67     *
     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
     74     */
     75    public HistoryOsmPrimitive get(long id, long version) throws NoSuchElementException{
    2876        ArrayList<HistoryOsmPrimitive> versions = data.get(id);
    2977        if (versions == null)
     
    3785    }
    3886
     87    /**
     88     * Adds a history primitive to the data set
     89     *
     90     * @param primitive  the history primitive to add
     91     */
    3992    public void put(HistoryOsmPrimitive primitive) {
    4093        if (data.get(primitive.getId()) == null) {
     
    4295        }
    4396        data.get(primitive.getId()).add(primitive);
     97        fireHistoryUpdated(primitive.getId());
    4498    }
    4599
     
    66120            return;
    67121        for (Long id : other.data.keySet()) {
    68             if (!this.data.keySet().contains(id)) {
    69                 this.data.put(id, other.data.get(id));
    70             }
     122            this.data.put(id, other.data.get(id));
    71123        }
     124        fireHistoryUpdated(0);
    72125    }
    73126}
Note: See TracChangeset for help on using the changeset viewer.