Changeset 3207 in josm for trunk/src/org/openstreetmap/josm/data
- Timestamp:
- 2010-04-25T15:05:40+02:00 (15 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/data/osm
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/DataSet.java
r3206 r3207 28 28 import org.openstreetmap.josm.data.osm.event.TagsChangedEvent; 29 29 import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent; 30 import org.openstreetmap.josm.tools.Predicate; 30 31 31 32 /** … … 52 53 } 53 54 54 private Storage<OsmPrimitive> allPrimitives = new Storage<OsmPrimitive>(new IdHash() );55 private Storage<OsmPrimitive> allPrimitives = new Storage<OsmPrimitive>(new IdHash(), 16, true); 55 56 private Map<PrimitiveId, OsmPrimitive> primitivesMap = allPrimitives.foreignKey(new IdHash()); 56 57 private List<DataSetListener> listeners = new ArrayList<DataSetListener>(); … … 98 99 private QuadBuckets<Node> nodes = new QuadBuckets<Node>(); 99 100 101 private <T extends OsmPrimitive> Collection<T> getPrimitives(Predicate<OsmPrimitive> predicate) { 102 return new DatasetCollection<T>(allPrimitives, predicate); 103 } 104 100 105 /** 101 106 * Replies an unmodifiable collection of nodes in this dataset … … 104 109 */ 105 110 public Collection<Node> getNodes() { 106 return Collections.unmodifiableCollection(nodes);111 return getPrimitives(OsmPrimitive.nodePredicate); 107 112 } 108 113 … … 124 129 */ 125 130 public Collection<Way> getWays() { 126 return Collections.unmodifiableCollection(ways);131 return getPrimitives(OsmPrimitive.wayPredicate); 127 132 } 128 133 … … 134 139 * All relations/relationships 135 140 */ 136 private Collection<Relation> relations = new LinkedList<Relation>();141 private Collection<Relation> relations = new ArrayList<Relation>(); 137 142 138 143 /** … … 142 147 */ 143 148 public Collection<Relation> getRelations() { 144 return Collections.unmodifiableCollection(relations);149 return getPrimitives(OsmPrimitive.relationPredicate); 145 150 } 146 151 … … 165 170 */ 166 171 public Collection<OsmPrimitive> allPrimitives() { 167 return Collections.unmodifiableCollection(allPrimitives);172 return getPrimitives(OsmPrimitive.allPredicate); 168 173 } 169 174 … … 172 177 */ 173 178 public Collection<OsmPrimitive> allNonDeletedPrimitives() { 174 return new DatasetCollection(allPrimitives,OsmPrimitive.nonDeletedPredicate);179 return getPrimitives(OsmPrimitive.nonDeletedPredicate); 175 180 } 176 181 177 182 public Collection<OsmPrimitive> allNonDeletedCompletePrimitives() { 178 return new DatasetCollection(allPrimitives,OsmPrimitive.nonDeletedCompletePredicate);183 return getPrimitives(OsmPrimitive.nonDeletedCompletePredicate); 179 184 } 180 185 181 186 public Collection<OsmPrimitive> allNonDeletedPhysicalPrimitives() { 182 return new DatasetCollection(allPrimitives,OsmPrimitive.nonDeletedPhysicalPredicate);187 return getPrimitives(OsmPrimitive.nonDeletedPhysicalPredicate); 183 188 } 184 189 185 190 public Collection<OsmPrimitive> allModifiedPrimitives() { 186 return new DatasetCollection(allPrimitives,OsmPrimitive.modifiedPredicate);191 return getPrimitives(OsmPrimitive.modifiedPredicate); 187 192 } 188 193 … … 471 476 return; 472 477 } 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) { 473 486 clearDisabled(nodes); 474 487 clearDisabled(ways); 475 488 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 481 504 482 505 public void setFiltered(Collection<? extends OsmPrimitive> selection) { … … 503 526 } 504 527 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 514 528 /** 515 529 * Remove the filtered parameter from every value in the collection. … … 521 535 for (OsmPrimitive osm : list) { 522 536 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);534 537 } 535 538 } -
trunk/src/org/openstreetmap/josm/data/osm/DatasetCollection.java
r3206 r3207 8 8 import org.openstreetmap.josm.tools.Predicate; 9 9 10 public class DatasetCollection extends AbstractCollection<OsmPrimitive> {10 public class DatasetCollection<T extends OsmPrimitive> extends AbstractCollection<T> { 11 11 12 private class FilterIterator implements Iterator< OsmPrimitive> {12 private class FilterIterator implements Iterator<T> { 13 13 14 14 private final Iterator<? extends OsmPrimitive> iterator; … … 35 35 } 36 36 37 public OsmPrimitive next() { 37 @SuppressWarnings("unchecked") 38 public T next() { 38 39 findNext(); 39 40 OsmPrimitive old = current; 40 41 current = null; 41 return old;42 return (T)old; 42 43 } 43 44 … … 56 57 57 58 @Override 58 public Iterator< OsmPrimitive> iterator() {59 public Iterator<T> iterator() { 59 60 return new FilterIterator(primitives.iterator()); 60 61 } … … 63 64 public int size() { 64 65 int size = 0; 65 Iterator< OsmPrimitive> it = iterator();66 Iterator<T> it = iterator(); 66 67 while (it.hasNext()) { 67 68 size++; -
trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java
r3177 r3207 438 438 } 439 439 }; 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 440 465 441 466 /** -
trunk/src/org/openstreetmap/josm/data/osm/Storage.java
r3206 r3207 23 23 import java.util.AbstractSet; 24 24 import java.util.Collection; 25 import java.util.ConcurrentModificationException; 25 26 import java.util.Iterator; 26 27 import java.util.Map; … … 92 93 private transient volatile int modCount = 0; 93 94 private float loadFactor = 0.6f; 95 private final boolean safeIterator; 96 private boolean arrayCopyNecessary; 94 97 95 98 public Storage() { … … 98 101 99 102 public Storage(int capacity) { 100 this(Storage.<T>defaultHash(), capacity );103 this(Storage.<T>defaultHash(), capacity, false); 101 104 } 102 105 103 106 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) { 108 111 this.hash = ha; 109 112 int cap = 1 << (int)(Math.ceil(Math.log(capacity/loadFactor) / Math.log(2))); 110 113 data = new Object[cap]; 111 114 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 } 112 125 } 113 126 114 127 // --------------- Collection implementation ------------------------ 115 128 @Override 116 public int size() {129 public synchronized int size() { 117 130 return size; 118 131 } 119 132 120 133 @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) { 126 144 int bucket = getBucket(hash, (T)o); 127 145 return bucket >= 0; 128 146 } 129 147 130 public @Override boolean add(T t) {148 public synchronized @Override boolean add(T t) { 131 149 T orig = putUnique(t); 132 150 return orig == t; 133 151 } 134 152 135 public @Override boolean remove(Object o) {153 public synchronized @Override boolean remove(Object o) { 136 154 T orig = removeElem((T)o); 137 155 return orig != null; 138 156 } 139 157 140 public @Override void clear() { 158 public synchronized @Override void clear() { 159 copyArray(); 141 160 modCount++; 142 161 size = 0; … … 146 165 } 147 166 148 public @Override int hashCode() {167 public synchronized @Override int hashCode() { 149 168 int h = 0; 150 169 for (T t : this) { … … 156 175 // ----------------- Extended API ---------------------------- 157 176 158 public T put(T t) { 177 public synchronized T put(T t) { 178 copyArray(); 159 179 modCount++; 160 180 ensureSpace(); … … 173 193 } 174 194 175 public T get(T t) {195 public synchronized T get(T t) { 176 196 int bucket = getBucket(hash, t); 177 197 return bucket < 0 ? null : (T)data[bucket]; 178 198 } 179 199 180 public T putUnique(T t) { 200 public synchronized T putUnique(T t) { 201 copyArray(); 181 202 modCount++; 182 203 ensureSpace(); … … 193 214 } 194 215 195 public T removeElem(T t) { 216 public synchronized T removeElem(T t) { 217 copyArray(); 196 218 modCount++; 197 219 int bucket = getBucket(hash, t); … … 381 403 } 382 404 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 383 435 private final class Iter implements Iterator<T> { 384 436 private final int mods; … … 410 462 411 463 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(); 417 466 while (slot < data.length && data[slot] == null) { 418 467 slot++;
Note:
See TracChangeset
for help on using the changeset viewer.