Changeset 3207 in josm for trunk/src/org


Ignore:
Timestamp:
2010-04-25T15:05:40+02:00 (14 years ago)
Author:
jttt
Message:

Fix #4861 Coding error reported on uploading changeset (GPS survey additions and changes to existing dataset

Location:
trunk/src/org/openstreetmap/josm
Files:
5 edited

Legend:

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

    r3206 r3207  
    2828import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
    2929import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
     30import org.openstreetmap.josm.tools.Predicate;
    3031
    3132/**
     
    5253    }
    5354
    54     private Storage<OsmPrimitive> allPrimitives = new Storage<OsmPrimitive>(new IdHash());
     55    private Storage<OsmPrimitive> allPrimitives = new Storage<OsmPrimitive>(new IdHash(), 16, true);
    5556    private Map<PrimitiveId, OsmPrimitive> primitivesMap = allPrimitives.foreignKey(new IdHash());
    5657    private List<DataSetListener> listeners = new ArrayList<DataSetListener>();
     
    9899    private QuadBuckets<Node> nodes = new QuadBuckets<Node>();
    99100
     101    private <T extends OsmPrimitive> Collection<T> getPrimitives(Predicate<OsmPrimitive> predicate) {
     102        return new DatasetCollection<T>(allPrimitives, predicate);
     103    }
     104
    100105    /**
    101106     * Replies an unmodifiable collection of nodes in this dataset
     
    104109     */
    105110    public Collection<Node> getNodes() {
    106         return Collections.unmodifiableCollection(nodes);
     111        return getPrimitives(OsmPrimitive.nodePredicate);
    107112    }
    108113
     
    124129     */
    125130    public Collection<Way> getWays() {
    126         return Collections.unmodifiableCollection(ways);
     131        return getPrimitives(OsmPrimitive.wayPredicate);
    127132    }
    128133
     
    134139     * All relations/relationships
    135140     */
    136     private Collection<Relation> relations = new LinkedList<Relation>();
     141    private Collection<Relation> relations = new ArrayList<Relation>();
    137142
    138143    /**
     
    142147     */
    143148    public Collection<Relation> getRelations() {
    144         return Collections.unmodifiableCollection(relations);
     149        return getPrimitives(OsmPrimitive.relationPredicate);
    145150    }
    146151
     
    165170     */
    166171    public Collection<OsmPrimitive> allPrimitives() {
    167         return Collections.unmodifiableCollection(allPrimitives);
     172        return getPrimitives(OsmPrimitive.allPredicate);
    168173    }
    169174
     
    172177     */
    173178    public Collection<OsmPrimitive> allNonDeletedPrimitives() {
    174         return new DatasetCollection(allPrimitives, OsmPrimitive.nonDeletedPredicate);
     179        return getPrimitives(OsmPrimitive.nonDeletedPredicate);
    175180    }
    176181
    177182    public Collection<OsmPrimitive> allNonDeletedCompletePrimitives() {
    178         return new DatasetCollection(allPrimitives, OsmPrimitive.nonDeletedCompletePredicate);
     183        return getPrimitives(OsmPrimitive.nonDeletedCompletePredicate);
    179184    }
    180185
    181186    public Collection<OsmPrimitive> allNonDeletedPhysicalPrimitives() {
    182         return new DatasetCollection(allPrimitives, OsmPrimitive.nonDeletedPhysicalPredicate);
     187        return getPrimitives(OsmPrimitive.nonDeletedPhysicalPredicate);
    183188    }
    184189
    185190    public Collection<OsmPrimitive> allModifiedPrimitives() {
    186         return new DatasetCollection(allPrimitives, OsmPrimitive.modifiedPredicate);
     191        return getPrimitives(OsmPrimitive.modifiedPredicate);
    187192    }
    188193
     
    471476            return;
    472477        }
     478        clearDisabled(allPrimitives());
     479        for (OsmPrimitive o : osm)
     480            if (o != null) {
     481                o.setDisabled(true);
     482            }
     483    }
     484
     485    public void setDisabled(Collection<? extends OsmPrimitive> selection) {
    473486        clearDisabled(nodes);
    474487        clearDisabled(ways);
    475488        clearDisabled(relations);
    476         for (OsmPrimitive o : osm)
    477             if (o != null) {
    478                 o.setDisabled(true);
    479             }
    480     }
     489        for (OsmPrimitive osm : selection) {
     490            osm.setDisabled(true);
     491        }
     492    }
     493
     494    /**
     495     * Remove the disabled parameter from every value in the collection.
     496     * @param list The collection to remove the disabled parameter from.
     497     */
     498    private void clearDisabled(Collection<? extends OsmPrimitive> list) {
     499        for (OsmPrimitive osm : list) {
     500            osm.setDisabled(false);
     501        }
     502    }
     503
    481504
    482505    public void setFiltered(Collection<? extends OsmPrimitive> selection) {
     
    503526    }
    504527
    505     public void setDisabled(Collection<? extends OsmPrimitive> selection) {
    506         clearDisabled(nodes);
    507         clearDisabled(ways);
    508         clearDisabled(relations);
    509         for (OsmPrimitive osm : selection) {
    510             osm.setDisabled(true);
    511         }
    512     }
    513 
    514528    /**
    515529     * Remove the filtered parameter from every value in the collection.
     
    521535        for (OsmPrimitive osm : list) {
    522536            osm.setFiltered(false);
    523         }
    524     }
    525     /**
    526      * Remove the disabled parameter from every value in the collection.
    527      * @param list The collection to remove the disabled parameter from.
    528      */
    529     private void clearDisabled(Collection<? extends OsmPrimitive> list) {
    530         if (list == null)
    531             return;
    532         for (OsmPrimitive osm : list) {
    533             osm.setDisabled(false);
    534537        }
    535538    }
  • trunk/src/org/openstreetmap/josm/data/osm/DatasetCollection.java

    r3206 r3207  
    88import org.openstreetmap.josm.tools.Predicate;
    99
    10 public class DatasetCollection extends AbstractCollection<OsmPrimitive> {
     10public class DatasetCollection<T extends OsmPrimitive> extends AbstractCollection<T> {
    1111
    12     private class FilterIterator implements Iterator<OsmPrimitive> {
     12    private class FilterIterator implements Iterator<T> {
    1313
    1414        private final Iterator<? extends OsmPrimitive> iterator;
     
    3535        }
    3636
    37         public OsmPrimitive next() {
     37        @SuppressWarnings("unchecked")
     38        public T next() {
    3839            findNext();
    3940            OsmPrimitive old = current;
    4041            current = null;
    41             return old;
     42            return (T)old;
    4243        }
    4344
     
    5657
    5758    @Override
    58     public Iterator<OsmPrimitive> iterator() {
     59    public Iterator<T> iterator() {
    5960        return new FilterIterator(primitives.iterator());
    6061    }
     
    6364    public int size() {
    6465        int size = 0;
    65         Iterator<OsmPrimitive> it = iterator();
     66        Iterator<T> it = iterator();
    6667        while (it.hasNext()) {
    6768            size++;
  • trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java

    r3177 r3207  
    438438        }
    439439    };
     440
     441    public static Predicate<OsmPrimitive> nodePredicate = new Predicate<OsmPrimitive>() {
     442        public boolean evaluate(OsmPrimitive primitive) {
     443            return primitive.getClass() == Node.class;
     444        }
     445    };
     446
     447    public static Predicate<OsmPrimitive> wayPredicate = new Predicate<OsmPrimitive>() {
     448        public boolean evaluate(OsmPrimitive primitive) {
     449            return primitive.getClass() == Way.class;
     450        }
     451    };
     452
     453    public static Predicate<OsmPrimitive> relationPredicate = new Predicate<OsmPrimitive>() {
     454        public boolean evaluate(OsmPrimitive primitive) {
     455            return primitive.getClass() == Relation.class;
     456        }
     457    };
     458
     459    public static Predicate<OsmPrimitive> allPredicate = new Predicate<OsmPrimitive>() {
     460        public boolean evaluate(OsmPrimitive primitive) {
     461            return true;
     462        }
     463    };
     464
    440465
    441466    /**
  • trunk/src/org/openstreetmap/josm/data/osm/Storage.java

    r3206 r3207  
    2323import java.util.AbstractSet;
    2424import java.util.Collection;
     25import java.util.ConcurrentModificationException;
    2526import java.util.Iterator;
    2627import java.util.Map;
     
    9293    private transient volatile int modCount = 0;
    9394    private float loadFactor = 0.6f;
     95    private final boolean safeIterator;
     96    private boolean arrayCopyNecessary;
    9497
    9598    public Storage() {
     
    98101
    99102    public Storage(int capacity) {
    100         this(Storage.<T>defaultHash(), capacity);
     103        this(Storage.<T>defaultHash(), capacity, false);
    101104    }
    102105
    103106    public Storage(Hash<? super T,? super T> ha) {
    104         this(ha, 16);
    105     }
    106 
    107     public Storage(Hash<? super T, ? super T> ha, int capacity) {
     107        this(ha, 16, false);
     108    }
     109
     110    public Storage(Hash<? super T, ? super T> ha, int capacity, boolean safeIterator) {
    108111        this.hash = ha;
    109112        int cap = 1 << (int)(Math.ceil(Math.log(capacity/loadFactor) / Math.log(2)));
    110113        data = new Object[cap];
    111114        mask = data.length - 1;
     115        this.safeIterator = safeIterator;
     116    }
     117
     118    private void copyArray() {
     119        if (arrayCopyNecessary) {
     120            Object[] newData = new Object[data.length];
     121            System.arraycopy(data, 0, newData, 0, data.length);
     122            data = newData;
     123            arrayCopyNecessary = false;
     124        }
    112125    }
    113126
    114127    // --------------- Collection implementation ------------------------
    115128    @Override
    116     public int size() {
     129    public synchronized int size() {
    117130        return size;
    118131    }
    119132
    120133    @Override
    121     public Iterator<T> iterator() {
    122         return new Iter();
    123     }
    124 
    125     public @Override boolean contains(Object o) {
     134    public synchronized Iterator<T> iterator() {
     135        if (safeIterator) {
     136            arrayCopyNecessary = true;
     137            return new SafeReadonlyIter(data);
     138        } else
     139            return new Iter();
     140
     141    }
     142
     143    public synchronized @Override boolean contains(Object o) {
    126144        int bucket = getBucket(hash, (T)o);
    127145        return bucket >= 0;
    128146    }
    129147
    130     public @Override boolean add(T t) {
     148    public synchronized @Override boolean add(T t) {
    131149        T orig = putUnique(t);
    132150        return orig == t;
    133151    }
    134152
    135     public @Override boolean remove(Object o) {
     153    public synchronized @Override boolean remove(Object o) {
    136154        T orig = removeElem((T)o);
    137155        return orig != null;
    138156    }
    139157
    140     public @Override void clear() {
     158    public synchronized @Override void clear() {
     159        copyArray();
    141160        modCount++;
    142161        size = 0;
     
    146165    }
    147166
    148     public @Override int hashCode() {
     167    public synchronized @Override int hashCode() {
    149168        int h = 0;
    150169        for (T t : this) {
     
    156175    // ----------------- Extended API ----------------------------
    157176
    158     public T put(T t) {
     177    public synchronized T put(T t) {
     178        copyArray();
    159179        modCount++;
    160180        ensureSpace();
     
    173193    }
    174194
    175     public T get(T t) {
     195    public synchronized T get(T t) {
    176196        int bucket = getBucket(hash, t);
    177197        return bucket < 0 ? null : (T)data[bucket];
    178198    }
    179199
    180     public T putUnique(T t) {
     200    public synchronized T putUnique(T t) {
     201        copyArray();
    181202        modCount++;
    182203        ensureSpace();
     
    193214    }
    194215
    195     public T removeElem(T t) {
     216    public synchronized T removeElem(T t) {
     217        copyArray();
    196218        modCount++;
    197219        int bucket = getBucket(hash, t);
     
    381403    }
    382404
     405    private final class SafeReadonlyIter implements Iterator<T> {
     406        final Object[] data;
     407        int slot = 0;
     408
     409        SafeReadonlyIter(Object[] data) {
     410            this.data = data;
     411        }
     412
     413        public boolean hasNext() {
     414            align();
     415            return slot < data.length;
     416        }
     417
     418        public T next() {
     419            if (!hasNext()) throw new NoSuchElementException();
     420            return (T)data[slot++];
     421        }
     422
     423        public void remove() {
     424            throw new UnsupportedOperationException();
     425        }
     426
     427        private void align() {
     428            while (slot < data.length && data[slot] == null) {
     429                slot++;
     430            }
     431        }
     432    }
     433
     434
    383435    private final class Iter implements Iterator<T> {
    384436        private final int mods;
     
    410462
    411463        private void align() {
    412             if (mods != modCount) {
    413                 System.err.println("Warning: ConcurrentModification");
    414                 Thread.dumpStack();
    415                 //throw new ConcurrentModificationException();
    416             }
     464            if (mods != modCount)
     465                throw new ConcurrentModificationException();
    417466            while (slot < data.length && data[slot] == null) {
    418467                slot++;
  • trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java

    r3206 r3207  
    270270
    271271    @Override public String getToolTipText() {
    272         int nodes = new DatasetCollection(data.getNodes(), OsmPrimitive.nonDeletedPredicate).size();
    273         int ways = new DatasetCollection(data.getWays(), OsmPrimitive.nonDeletedPredicate).size();
     272        int nodes = new DatasetCollection<OsmPrimitive>(data.getNodes(), OsmPrimitive.nonDeletedPredicate).size();
     273        int ways = new DatasetCollection<OsmPrimitive>(data.getWays(), OsmPrimitive.nonDeletedPredicate).size();
    274274
    275275        String tool = trn("{0} node", "{0} nodes", nodes, nodes)+", ";
Note: See TracChangeset for help on using the changeset viewer.