Changeset 13434 in josm for trunk/src/org/openstreetmap/josm/data/osm/DataSet.java
- Timestamp:
- 2018-02-18T05:02:23+01:00 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/DataSet.java
r13420 r13434 17 17 import java.util.Set; 18 18 import java.util.concurrent.CopyOnWriteArrayList; 19 import java.util.concurrent.atomic.AtomicBoolean; 19 20 import java.util.concurrent.locks.Lock; 20 21 import java.util.concurrent.locks.ReadWriteLock; … … 103 104 * @author imi 104 105 */ 105 public final class DataSet extends QuadBucketPrimitiveStore implements Data, ProjectionChangeListener {106 public final class DataSet extends QuadBucketPrimitiveStore implements Data, ProjectionChangeListener, ReadOnly { 106 107 107 108 /** … … 143 144 return xmlFlag; 144 145 } 146 147 /** 148 * Returns the {@code UploadPolicy} for the given <code>upload='...'</code> XML-attribute 149 * @param xmlFlag <code>upload='...'</code> XML-attribute to convert 150 * @return {@code UploadPolicy} value 151 * @throws IllegalArgumentException for invalid values 152 * @since 13434 153 */ 154 public static UploadPolicy of(String xmlFlag) { 155 for (UploadPolicy policy : values()) { 156 if (policy.getXmlFlag().equalsIgnoreCase(xmlFlag)) { 157 return policy; 158 } 159 } 160 throw new IllegalArgumentException(xmlFlag); 161 } 145 162 } 146 163 … … 171 188 private String name; 172 189 private UploadPolicy uploadPolicy; 190 /** Flag used to know if the dataset should not be editable */ 191 private final AtomicBoolean isReadOnly = new AtomicBoolean(false); 173 192 174 193 private final ReadWriteLock lock = new ReentrantReadWriteLock(); … … 259 278 version = copyFrom.version; 260 279 uploadPolicy = copyFrom.uploadPolicy; 280 isReadOnly.set(copyFrom.isReadOnly.get()); 261 281 } finally { 262 282 copyFrom.getReadLock().unlock(); … … 353 373 * 354 374 * @param version the API version, i.e. "0.6" 375 * @throws IllegalStateException if the dataset is read-only 355 376 */ 356 377 public void setVersion(String version) { 378 checkModifiable(); 357 379 this.version = version; 358 380 } … … 533 555 * 534 556 * @param primitive the primitive. 557 * @throws IllegalStateException if the dataset is read-only 535 558 */ 536 559 @Override 537 560 public void addPrimitive(OsmPrimitive primitive) { 538 561 Objects.requireNonNull(primitive, "primitive"); 562 checkModifiable(); 539 563 beginUpdate(); 540 564 try { … … 561 585 * 562 586 * @param primitiveId the id of the primitive 587 * @throws IllegalStateException if the dataset is read-only 563 588 */ 564 589 public void removePrimitive(PrimitiveId primitiveId) { 590 checkModifiable(); 565 591 beginUpdate(); 566 592 try { … … 587 613 @Override 588 614 protected void removePrimitive(OsmPrimitive primitive) { 615 checkModifiable(); 589 616 beginUpdate(); 590 617 try { … … 998 1025 * @param node the node 999 1026 * @return The set of ways that have been modified 1027 * @throws IllegalStateException if the dataset is read-only 1000 1028 */ 1001 1029 public Set<Way> unlinkNodeFromWays(Node node) { 1030 checkModifiable(); 1002 1031 Set<Way> result = new HashSet<>(); 1003 1032 beginUpdate(); … … 1025 1054 * @param primitive the primitive 1026 1055 * @return The set of relations that have been modified 1056 * @throws IllegalStateException if the dataset is read-only 1027 1057 */ 1028 1058 public Set<Relation> unlinkPrimitiveFromRelations(OsmPrimitive primitive) { 1059 checkModifiable(); 1029 1060 Set<Relation> result = new HashSet<>(); 1030 1061 beginUpdate(); … … 1059 1090 * @param referencedPrimitive the referenced primitive 1060 1091 * @return The set of primitives that have been modified 1092 * @throws IllegalStateException if the dataset is read-only 1061 1093 */ 1062 1094 public Set<OsmPrimitive> unlinkReferencesToPrimitive(OsmPrimitive referencedPrimitive) { 1095 checkModifiable(); 1063 1096 Set<OsmPrimitive> result = new HashSet<>(); 1064 1097 beginUpdate(); … … 1131 1164 * } 1132 1165 * </pre> 1166 * @see #endUpdate() 1133 1167 */ 1134 1168 public void beginUpdate() { … … 1138 1172 1139 1173 /** 1174 * Must be called after a previous call to {@link #beginUpdate()} to fire change events. 1175 * <br> 1176 * Typical usecase should look like this: 1177 * <pre> 1178 * ds.beginUpdate(); 1179 * try { 1180 * ... 1181 * } finally { 1182 * ds.endUpdate(); 1183 * } 1184 * </pre> 1140 1185 * @see DataSet#beginUpdate() 1141 1186 */ … … 1273 1318 * Removes all primitives from the dataset and resets the currently selected primitives 1274 1319 * to the empty collection. Also notifies selection change listeners if necessary. 1320 * @throws IllegalStateException if the dataset is read-only 1275 1321 */ 1276 1322 @Override 1277 1323 public void clear() { 1324 checkModifiable(); 1278 1325 beginUpdate(); 1279 1326 try { … … 1292 1339 * Marks all "invisible" objects as deleted. These objects should be always marked as 1293 1340 * deleted when downloaded from the server. They can be undeleted later if necessary. 1294 * 1341 * @throws IllegalStateException if the dataset is read-only 1295 1342 */ 1296 1343 public void deleteInvisible() { 1344 checkModifiable(); 1297 1345 for (OsmPrimitive primitive:allPrimitives) { 1298 1346 if (!primitive.isVisible()) { … … 1314 1362 * @param from The source DataSet 1315 1363 * @param progressMonitor The progress monitor 1364 * @throws IllegalStateException if the dataset is read-only 1316 1365 */ 1317 1366 public synchronized void mergeFrom(DataSet from, ProgressMonitor progressMonitor) { 1318 1367 if (from != null) { 1368 checkModifiable(); 1319 1369 new DataSetMerger(this, from).merge(progressMonitor); 1320 1370 synchronized (from) { … … 1402 1452 mappaintCacheIdx++; 1403 1453 } 1454 1455 @Override 1456 public void setReadOnly() { 1457 if (!isReadOnly.compareAndSet(false, true)) { 1458 Logging.warn("Trying to set readOnly flag on a readOnly dataset ", getName()); 1459 } 1460 } 1461 1462 @Override 1463 public void unsetReadOnly() { 1464 if (!isReadOnly.compareAndSet(true, false)) { 1465 Logging.warn("Trying to unset readOnly flag on a non-readOnly dataset ", getName()); 1466 } 1467 } 1468 1469 @Override 1470 public boolean isReadOnly() { 1471 return isReadOnly.get(); 1472 } 1473 1474 /** 1475 * Checks the dataset is modifiable (not read-only). 1476 * @throws IllegalStateException if the dataset is read-only 1477 */ 1478 private void checkModifiable() { 1479 if (isReadOnly()) { 1480 throw new IllegalStateException("DataSet is read-only"); 1481 } 1482 } 1404 1483 }
Note:
See TracChangeset
for help on using the changeset viewer.