001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.vector; 003 004import java.util.ArrayList; 005import java.util.Arrays; 006import java.util.Collection; 007import java.util.Collections; 008import java.util.HashSet; 009import java.util.LinkedHashSet; 010import java.util.List; 011import java.util.Map; 012import java.util.Objects; 013import java.util.Optional; 014import java.util.Set; 015import java.util.concurrent.ConcurrentHashMap; 016import java.util.concurrent.locks.Lock; 017import java.util.concurrent.locks.ReentrantReadWriteLock; 018import java.util.function.Function; 019import java.util.function.Predicate; 020import java.util.function.Supplier; 021import java.util.stream.Collectors; 022import java.util.stream.Stream; 023 024import org.openstreetmap.josm.data.DataSource; 025import org.openstreetmap.josm.data.imagery.vectortile.mapbox.MVTTile; 026import org.openstreetmap.josm.data.osm.BBox; 027import org.openstreetmap.josm.data.osm.DataSelectionListener; 028import org.openstreetmap.josm.data.osm.DownloadPolicy; 029import org.openstreetmap.josm.data.osm.HighlightUpdateListener; 030import org.openstreetmap.josm.data.osm.IPrimitive; 031import org.openstreetmap.josm.data.osm.OsmData; 032import org.openstreetmap.josm.data.osm.PrimitiveId; 033import org.openstreetmap.josm.data.osm.Storage; 034import org.openstreetmap.josm.data.osm.UploadPolicy; 035import org.openstreetmap.josm.data.osm.WaySegment; 036import org.openstreetmap.josm.data.osm.event.IDataSelectionEventSource; 037import org.openstreetmap.josm.data.osm.event.IDataSelectionListener; 038import org.openstreetmap.josm.data.osm.event.IDataSelectionListener.SelectionAddEvent; 039import org.openstreetmap.josm.data.osm.event.IDataSelectionListener.SelectionChangeEvent; 040import org.openstreetmap.josm.data.osm.event.IDataSelectionListener.SelectionRemoveEvent; 041import org.openstreetmap.josm.data.osm.event.IDataSelectionListener.SelectionReplaceEvent; 042import org.openstreetmap.josm.data.osm.event.IDataSelectionListener.SelectionToggleEvent; 043import org.openstreetmap.josm.gui.mappaint.ElemStyles; 044import org.openstreetmap.josm.tools.ListenerList; 045import org.openstreetmap.josm.tools.Logging; 046import org.openstreetmap.josm.tools.SubclassFilteredCollection; 047 048/** 049 * A data class for Vector Data 050 * 051 * @author Taylor Smock 052 * @since 17862 053 */ 054public class VectorDataSet implements OsmData<VectorPrimitive, VectorNode, VectorWay, VectorRelation>, 055 IDataSelectionEventSource<VectorPrimitive, VectorNode, VectorWay, VectorRelation, VectorDataSet> { 056 // Note: In Java 8, computeIfAbsent is blocking for both pre-existing and new values. In Java 9, it is only blocking 057 // for new values (perf increase). See JDK-8161372 for more info. 058 private final Map<Integer, Storage<MVTTile>> dataStoreMap = new ConcurrentHashMap<>(); 059 // This is for "custom" data 060 private final VectorDataStore customDataStore = new VectorDataStore(); 061 // Both of these listener lists are useless, since they expect OsmPrimitives at this time 062 private final ListenerList<HighlightUpdateListener> highlightUpdateListenerListenerList = ListenerList.create(); 063 private final ListenerList<DataSelectionListener> dataSelectionListenerListenerList = ListenerList.create(); 064 private boolean lock = true; 065 private String name; 066 private short mappaintCacheIdx = 1; 067 068 private final Object selectionLock = new Object(); 069 /** 070 * The current selected primitives. This is always a unmodifiable set. 071 * 072 * The set should be ordered in the order in which the primitives have been added to the selection. 073 */ 074 private Set<PrimitiveId> currentSelectedPrimitives = Collections.emptySet(); 075 076 private final ListenerList<IDataSelectionListener<VectorPrimitive, VectorNode, VectorWay, VectorRelation, VectorDataSet>> listeners = 077 ListenerList.create(); 078 079 private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(); 080 081 /** 082 * The distance to consider nodes duplicates -- mostly a memory saving measure. 083 * 0.000_000_1 ~1.2 cm (+- 5.57 mm) 084 * Descriptions from <a href="https://xkcd.com/2170/">https://xkcd.com/2170/</a> 085 * Notes on <a href="https://wiki.openstreetmap.org/wiki/Node">https://wiki.openstreetmap.org/wiki/Node</a> indicate 086 * that IEEE 32-bit floats should not be used at high longitude (0.000_01 precision) 087 */ 088 protected static final float DUPE_NODE_DISTANCE = 0.000_000_1f; 089 090 /** 091 * The current zoom we are getting/adding to 092 */ 093 private int zoom; 094 /** 095 * Default to normal download policy 096 */ 097 private DownloadPolicy downloadPolicy = DownloadPolicy.NORMAL; 098 /** 099 * Default to a blocked upload policy 100 */ 101 private UploadPolicy uploadPolicy = UploadPolicy.BLOCKED; 102 /** 103 * The paint style for this layer 104 */ 105 private ElemStyles styles; 106 private final Collection<PrimitiveId> highlighted = new HashSet<>(); 107 108 @Override 109 public Collection<DataSource> getDataSources() { 110 // TODO 111 return Collections.emptyList(); 112 } 113 114 @Override 115 public void lock() { 116 this.lock = true; 117 } 118 119 @Override 120 public void unlock() { 121 this.lock = false; 122 } 123 124 @Override 125 public boolean isLocked() { 126 return this.lock; 127 } 128 129 @Override 130 public String getVersion() { 131 return "8"; // TODO get this dynamically. Not critical, as this is currently the _only_ version. 132 } 133 134 @Override 135 public String getName() { 136 return this.name; 137 } 138 139 @Override 140 public void setName(String name) { 141 this.name = name; 142 } 143 144 /** 145 * Add a primitive to the custom data store 146 * @param primitive the primitive to add 147 */ 148 @Override 149 public void addPrimitive(VectorPrimitive primitive) { 150 tryWrite(this.readWriteLock, () -> { 151 this.customDataStore.addPrimitive(primitive); 152 primitive.setDataSet(this); 153 }); 154 } 155 156 /** 157 * Remove a primitive from the custom data store 158 * @param primitive The primitive to add to the custom data store 159 */ 160 public void removePrimitive(VectorPrimitive primitive) { 161 this.customDataStore.removePrimitive(primitive); 162 primitive.setDataSet(null); 163 } 164 165 @Override 166 public void clear() { 167 this.dataStoreMap.clear(); 168 } 169 170 @Override 171 public List<VectorNode> searchNodes(BBox bbox) { 172 return tryRead(this.readWriteLock, () -> { 173 final Storage<MVTTile> dataStore = this.getBestZoomDataStore().orElse(null); 174 final Stream<VectorDataStore> dataStoreStream = dataStore != null ? dataStore.stream().map(MVTTile::getData) : Stream.empty(); 175 return Stream.concat(dataStoreStream, Stream.of(this.customDataStore)).map(VectorDataStore::getStore) 176 .flatMap(store -> store.searchNodes(bbox).stream()).collect(Collectors.toList()); 177 }).orElseGet(Collections::emptyList); 178 } 179 180 @Override 181 public boolean containsNode(VectorNode vectorNode) { 182 return tryRead(this.readWriteLock, () -> { 183 final Storage<MVTTile> dataStore = this.getBestZoomDataStore().orElse(null); 184 final Stream<VectorDataStore> dataStoreStream = dataStore != null ? dataStore.stream().map(MVTTile::getData) : Stream.empty(); 185 return Stream.concat(dataStoreStream, Stream.of(this.customDataStore)).map(VectorDataStore::getStore) 186 .anyMatch(store -> store.containsNode(vectorNode)); 187 }).orElse(Boolean.FALSE); 188 } 189 190 @Override 191 public List<VectorWay> searchWays(BBox bbox) { 192 return tryRead(this.readWriteLock, () -> { 193 final Storage<MVTTile> dataStore = this.getBestZoomDataStore().orElse(null); 194 final Stream<VectorDataStore> dataStoreStream = dataStore != null ? dataStore.stream().map(MVTTile::getData) : Stream.empty(); 195 return Stream.concat(dataStoreStream, Stream.of(this.customDataStore)).map(VectorDataStore::getStore) 196 .flatMap(store -> store.searchWays(bbox).stream()).collect(Collectors.toList()); 197 }).orElseGet(Collections::emptyList); 198 } 199 200 @Override 201 public boolean containsWay(VectorWay vectorWay) { 202 return tryRead(this.readWriteLock, () -> { 203 final Storage<MVTTile> dataStore = this.getBestZoomDataStore().orElse(null); 204 final Stream<VectorDataStore> dataStoreStream = dataStore != null ? dataStore.stream().map(MVTTile::getData) : Stream.empty(); 205 return Stream.concat(dataStoreStream, Stream.of(this.customDataStore)).map(VectorDataStore::getStore) 206 .anyMatch(store -> store.containsWay(vectorWay)); 207 }).orElse(Boolean.FALSE); 208 } 209 210 @Override 211 public List<VectorRelation> searchRelations(BBox bbox) { 212 return tryRead(this.readWriteLock, () -> { 213 final Storage<MVTTile> dataStore = this.getBestZoomDataStore().orElse(null); 214 final Stream<VectorDataStore> dataStoreStream = dataStore != null ? dataStore.stream().map(MVTTile::getData) : Stream.empty(); 215 return Stream.concat(dataStoreStream, Stream.of(this.customDataStore)).map(VectorDataStore::getStore) 216 .flatMap(store -> store.searchRelations(bbox).stream()).collect(Collectors.toList()); 217 }).orElseGet(Collections::emptyList); 218 } 219 220 @Override 221 public boolean containsRelation(VectorRelation vectorRelation) { 222 return tryRead(this.readWriteLock, () -> { 223 final Storage<MVTTile> dataStore = this.getBestZoomDataStore().orElse(null); 224 final Stream<VectorDataStore> dataStoreStream = dataStore != null ? dataStore.stream().map(MVTTile::getData) : Stream.empty(); 225 return Stream.concat(dataStoreStream, Stream.of(this.customDataStore)).map(VectorDataStore::getStore) 226 .anyMatch(store -> store.containsRelation(vectorRelation)); 227 }).orElse(Boolean.FALSE); 228 } 229 230 /** 231 * Get a primitive for an id 232 * @param primitiveId type and uniqueId of the primitive. Might be < 0 for newly created primitives 233 * @return The primitive for the id. Please note that since this is vector data, there may be more primitives with this id. 234 * Please use {@link #getPrimitivesById(PrimitiveId...)} to get all primitives for that {@link PrimitiveId}. 235 */ 236 @Override 237 public VectorPrimitive getPrimitiveById(PrimitiveId primitiveId) { 238 return this.getPrimitivesById(primitiveId).findFirst().orElse(null); 239 } 240 241 /** 242 * Get all primitives for ids 243 * @param primitiveIds The ids to search for 244 * @return The primitives for the ids (note: as this is vector data, a {@link PrimitiveId} may have multiple associated primitives) 245 */ 246 public Stream<VectorPrimitive> getPrimitivesById(PrimitiveId... primitiveIds) { 247 final Storage<MVTTile> dataStore = this.getBestZoomDataStore().orElse(null); 248 return Stream.concat(dataStore != null ? dataStore.stream().map(MVTTile::getData) : Stream.empty(), 249 Stream.of(this.customDataStore)).map(VectorDataStore::getPrimitivesMap) 250 .flatMap(m -> Stream.of(primitiveIds).map(m::get)).filter(Objects::nonNull); 251 } 252 253 @Override 254 public <T extends VectorPrimitive> Collection<T> getPrimitives(Predicate<? super VectorPrimitive> predicate) { 255 final Collection<VectorPrimitive> primitives = tryRead(this.readWriteLock, () -> { 256 final Storage<MVTTile> dataStore = this.getBestZoomDataStore().orElse(null); 257 final Stream<VectorDataStore> dataStoreStream = dataStore != null ? dataStore.stream().map(MVTTile::getData) : Stream.empty(); 258 return Stream.concat(dataStoreStream, Stream.of(this.customDataStore)) 259 .map(VectorDataStore::getAllPrimitives).flatMap(Collection::stream).distinct().collect(Collectors.toList()); 260 261 }).orElseGet(Collections::emptyList); 262 return new SubclassFilteredCollection<>(primitives, predicate); 263 } 264 265 @Override 266 public Collection<VectorNode> getNodes() { 267 return this.getPrimitives(VectorNode.class::isInstance); 268 } 269 270 @Override 271 public Collection<VectorWay> getWays() { 272 return this.getPrimitives(VectorWay.class::isInstance); 273 } 274 275 @Override 276 public Collection<VectorRelation> getRelations() { 277 return this.getPrimitives(VectorRelation.class::isInstance); 278 } 279 280 @Override 281 public DownloadPolicy getDownloadPolicy() { 282 return this.downloadPolicy; 283 } 284 285 @Override 286 public void setDownloadPolicy(DownloadPolicy downloadPolicy) { 287 this.downloadPolicy = downloadPolicy; 288 } 289 290 @Override 291 public UploadPolicy getUploadPolicy() { 292 return this.uploadPolicy; 293 } 294 295 @Override 296 public void setUploadPolicy(UploadPolicy uploadPolicy) { 297 this.uploadPolicy = uploadPolicy; 298 } 299 300 /** 301 * Get the current Read/Write lock. 302 * This changes based off of zoom level. Please do not use this in a finally block 303 * @return The current read/write lock 304 */ 305 @Override 306 public Lock getReadLock() { 307 return this.readWriteLock.readLock(); 308 } 309 310 @Override 311 public Collection<WaySegment> getHighlightedVirtualNodes() { 312 // TODO? This requires a change to WaySegment so that it isn't Way/Node specific 313 return Collections.emptyList(); 314 } 315 316 @Override 317 public void setHighlightedVirtualNodes(Collection<WaySegment> waySegments) { 318 // TODO? This requires a change to WaySegment so that it isn't Way/Node specific 319 } 320 321 @Override 322 public Collection<WaySegment> getHighlightedWaySegments() { 323 // TODO? This requires a change to WaySegment so that it isn't Way/Node specific 324 return Collections.emptyList(); 325 } 326 327 @Override 328 public void setHighlightedWaySegments(Collection<WaySegment> waySegments) { 329 // TODO? This requires a change to WaySegment so that it isn't Way/Node specific 330 } 331 332 /** 333 * Mark some primitives as highlighted. 334 * API is *highly likely* to change, as the inherited methods are modified to accept primitives other than OSM primitives. 335 * @param primitives The primitives to highlight 336 */ 337 public void setHighlighted(Collection<PrimitiveId> primitives) { 338 this.highlighted.clear(); 339 this.highlighted.addAll(primitives); 340 // The highlight event updates are very OSM specific, and require a DataSet. 341 this.highlightUpdateListenerListenerList.fireEvent(event -> event.highlightUpdated(null)); 342 } 343 344 /** 345 * Get the highlighted objects 346 * @return The highlighted objects 347 */ 348 public Collection<PrimitiveId> getHighlighted() { 349 return Collections.unmodifiableCollection(this.highlighted); 350 } 351 352 @Override 353 public void addHighlightUpdateListener(HighlightUpdateListener listener) { 354 this.highlightUpdateListenerListenerList.addListener(listener); 355 } 356 357 @Override 358 public void removeHighlightUpdateListener(HighlightUpdateListener listener) { 359 this.highlightUpdateListenerListenerList.removeListener(listener); 360 } 361 362 @Override 363 public Collection<VectorPrimitive> getAllSelected() { 364 return tryRead(this.readWriteLock, () -> { 365 final Storage<MVTTile> dataStore = this.getBestZoomDataStore().orElse(null); 366 final Stream<VectorDataStore> dataStoreStream = dataStore != null 367 ? dataStore.stream().map(MVTTile::getData) 368 : Stream.empty(); 369 370 return Stream.concat(dataStoreStream, Stream.ofNullable(this.customDataStore)) 371 .map(VectorDataStore::getPrimitivesMap) 372 .filter(Objects::nonNull) 373 .flatMap(dataMap -> { 374 final Map<PrimitiveId, VectorPrimitive> lockObject = dataMap; 375 synchronized (lockObject) { 376 final List<VectorPrimitive> selectedInMap = new ArrayList<>(); 377 for (PrimitiveId id : this.currentSelectedPrimitives) { 378 final VectorPrimitive primitive = lockObject.get(id); 379 if (primitive != null) { 380 selectedInMap.add(primitive); 381 } 382 } 383 return selectedInMap.stream(); 384 } 385 }) 386 .collect(Collectors.toList()); 387 }).orElseGet(Collections::emptyList); 388 } 389 390 /** 391 * Get the best zoom datastore 392 * @return A datastore with data, or {@code null} if no good datastore exists. 393 */ 394 private Optional<Storage<MVTTile>> getBestZoomDataStore() { 395 final int currentZoom = this.zoom; 396 if (this.dataStoreMap.containsKey(currentZoom)) { 397 return Optional.of(this.dataStoreMap.get(currentZoom)); 398 } 399 // Check up to two zooms higher (may cause perf hit) 400 for (int tZoom = currentZoom + 1; tZoom < currentZoom + 3; tZoom++) { 401 if (this.dataStoreMap.containsKey(tZoom)) { 402 return Optional.of(this.dataStoreMap.get(tZoom)); 403 } 404 } 405 // Return *any* lower zoom data (shouldn't cause a perf hit...) 406 for (int tZoom = currentZoom - 1; tZoom >= 0; tZoom--) { 407 if (this.dataStoreMap.containsKey(tZoom)) { 408 return Optional.of(this.dataStoreMap.get(tZoom)); 409 } 410 } 411 // Check higher level zooms. May cause perf issues if selected datastore has a lot of data. 412 for (int tZoom = currentZoom + 3; tZoom < 34; tZoom++) { 413 if (this.dataStoreMap.containsKey(tZoom)) { 414 return Optional.of(this.dataStoreMap.get(tZoom)); 415 } 416 } 417 return Optional.empty(); 418 } 419 420 @Override 421 public boolean selectionEmpty() { 422 return this.currentSelectedPrimitives.isEmpty(); 423 } 424 425 @Override 426 public boolean isSelected(VectorPrimitive osm) { 427 return this.currentSelectedPrimitives.contains(osm.getPrimitiveId()); 428 } 429 430 @Override 431 public void toggleSelected(Collection<? extends PrimitiveId> osm) { 432 this.toggleSelectedImpl(osm.stream()); 433 } 434 435 @Override 436 public void toggleSelected(PrimitiveId... osm) { 437 this.toggleSelectedImpl(Stream.of(osm)); 438 } 439 440 private void toggleSelectedImpl(Stream<? extends PrimitiveId> osm) { 441 this.doSelectionChange(old -> new SelectionToggleEvent<>(this, old, 442 osm.flatMap(this::getPrimitivesById).filter(Objects::nonNull))); 443 } 444 445 @Override 446 public void setSelected(Collection<? extends PrimitiveId> selection) { 447 this.setSelectedImpl(selection.stream()); 448 } 449 450 @Override 451 public void setSelected(PrimitiveId... osm) { 452 this.setSelectedImpl(Stream.of(osm)); 453 } 454 455 private void setSelectedImpl(Stream<? extends PrimitiveId> osm) { 456 this.doSelectionChange(old -> new SelectionReplaceEvent<>(this, old, 457 osm.filter(Objects::nonNull).flatMap(this::getPrimitivesById).filter(Objects::nonNull))); 458 } 459 460 @Override 461 public void addSelected(Collection<? extends PrimitiveId> selection) { 462 this.addSelectedImpl(selection.stream()); 463 } 464 465 @Override 466 public void addSelected(PrimitiveId... osm) { 467 this.addSelectedImpl(Stream.of(osm)); 468 } 469 470 private void addSelectedImpl(Stream<? extends PrimitiveId> osm) { 471 this.doSelectionChange(old -> new SelectionAddEvent<>(this, old, 472 osm.flatMap(this::getPrimitivesById).filter(Objects::nonNull))); 473 } 474 475 @Override 476 public void clearSelection(PrimitiveId... osm) { 477 this.clearSelectionImpl(Stream.of(osm)); 478 } 479 480 @Override 481 public void clearSelection(Collection<? extends PrimitiveId> list) { 482 this.clearSelectionImpl(list.stream()); 483 } 484 485 @Override 486 public void clearSelection() { 487 this.clearSelectionImpl(new ArrayList<>(this.currentSelectedPrimitives).stream()); 488 } 489 490 private void clearSelectionImpl(Stream<? extends PrimitiveId> osm) { 491 this.doSelectionChange(old -> new SelectionRemoveEvent<>(this, old, 492 osm.flatMap(this::getPrimitivesById).filter(Objects::nonNull))); 493 } 494 495 /** 496 * Do a selection change. 497 * <p> 498 * This is the only method that changes the current selection state. 499 * @param command A generator that generates the {@link SelectionChangeEvent} 500 * for the given base set of currently selected primitives. 501 * @return true iff the command did change the selection. 502 */ 503 private boolean doSelectionChange(final Function<Set<VectorPrimitive>, 504 SelectionChangeEvent<VectorPrimitive, VectorNode, VectorWay, VectorRelation, VectorDataSet>> command) { 505 synchronized (this.selectionLock) { 506 final SelectionChangeEvent<VectorPrimitive, VectorNode, VectorWay, VectorRelation, VectorDataSet> event = 507 command.apply(currentSelectedPrimitives.stream().map(this::getPrimitiveById).collect(Collectors.toSet())); 508 if (event.isNop()) { 509 return false; 510 } 511 this.currentSelectedPrimitives = event.getSelection().stream().map(IPrimitive::getPrimitiveId) 512 .collect(Collectors.toCollection(LinkedHashSet::new)); 513 this.listeners.fireEvent(l -> l.selectionChanged(event)); 514 return true; 515 } 516 } 517 518 @Override 519 public void addSelectionListener(DataSelectionListener listener) { 520 this.dataSelectionListenerListenerList.addListener(listener); 521 } 522 523 @Override 524 public void removeSelectionListener(DataSelectionListener listener) { 525 this.dataSelectionListenerListenerList.removeListener(listener); 526 } 527 528 public short getMappaintCacheIndex() { 529 return this.mappaintCacheIdx; 530 } 531 532 @Override 533 public void clearMappaintCache() { 534 this.mappaintCacheIdx++; 535 } 536 537 public void setZoom(int zoom) { 538 if (zoom == this.zoom) { 539 return; // Do nothing -- zoom isn't actually changing 540 } 541 this.zoom = zoom; 542 this.clearMappaintCache(); 543 final int[] nearestZoom = {-1, -1, -1, -1}; 544 nearestZoom[0] = zoom; 545 // Create a new list to avoid concurrent modification issues 546 final int[] keys = new ArrayList<>(this.dataStoreMap.keySet()).stream().filter(Objects::nonNull) 547 .mapToInt(Integer::intValue).sorted().toArray(); 548 final int index; 549 if (this.dataStoreMap.containsKey(zoom)) { 550 index = Arrays.binarySearch(keys, zoom); 551 } else { 552 // (-(insertion point) - 1) = return -> insertion point = -(return + 1) 553 index = -(Arrays.binarySearch(keys, zoom) + 1); 554 } 555 if (index > 0) { 556 nearestZoom[1] = keys[index - 1]; 557 } 558 if (index < keys.length - 2) { 559 nearestZoom[2] = keys[index + 1]; 560 } 561 562 // TODO cleanup zooms for memory 563 } 564 565 public int getZoom() { 566 return this.zoom; 567 } 568 569 /** 570 * Add tile data to this dataset 571 * @param tile The tile to add 572 */ 573 public void addTileData(MVTTile tile) { 574 tryWrite(this.readWriteLock, () -> { 575 final int currentZoom = tile.getZoom(); 576 // computeIfAbsent should be thread safe (ConcurrentHashMap indicates it is, anyway) 577 final Storage<MVTTile> dataStore = this.dataStoreMap.computeIfAbsent(currentZoom, tZoom -> new Storage<>()); 578 tile.getData().getAllPrimitives().forEach(primitive -> primitive.setDataSet(this)); 579 dataStore.add(tile); 580 }); 581 } 582 583 /** 584 * Try to read something (here to avoid boilerplate) 585 * 586 * @param lock The lock 587 * @param supplier The reading function 588 * @param <T> The return type 589 * @return The optional return 590 */ 591 private static <T> Optional<T> tryRead(ReentrantReadWriteLock lock, Supplier<T> supplier) { 592 try { 593 lock.readLock().lockInterruptibly(); 594 return Optional.ofNullable(supplier.get()); 595 } catch (InterruptedException e) { 596 Logging.error(e); 597 Thread.currentThread().interrupt(); 598 } finally { 599 lock.readLock().unlock(); 600 } 601 return Optional.empty(); 602 } 603 604 /** 605 * Try to write something (here to avoid boilerplate) 606 * @param lock lock 607 * @param runnable The writing function 608 */ 609 private static void tryWrite(ReentrantReadWriteLock lock, Runnable runnable) { 610 try { 611 lock.writeLock().lockInterruptibly(); 612 runnable.run(); 613 } catch (InterruptedException e) { 614 Logging.error(e); 615 Thread.currentThread().interrupt(); 616 } finally { 617 if (lock.isWriteLockedByCurrentThread()) { 618 lock.writeLock().unlock(); 619 } 620 } 621 } 622 623 /** 624 * Get the styles for this layer 625 * 626 * @return The styles 627 */ 628 public ElemStyles getStyles() { 629 return this.styles; 630 } 631 632 /** 633 * Set the styles for this layer 634 * @param styles The styles to set for this layer 635 */ 636 public void setStyles(Collection<ElemStyles> styles) { 637 if (styles.size() == 1) { 638 this.styles = styles.iterator().next(); 639 } else if (!styles.isEmpty()) { 640 this.styles = new ElemStyles(styles.stream().flatMap(style -> style.getStyleSources().stream()).collect(Collectors.toList())); 641 } else { 642 this.styles = null; 643 } 644 } 645 646 /** 647 * Mark some layers as invisible 648 * @param invisibleLayers The layer to not show 649 */ 650 public void setInvisibleLayers(Collection<String> invisibleLayers) { 651 final String[] currentInvisibleLayers = invisibleLayers.stream().filter(Objects::nonNull).toArray(String[]::new); 652 final List<String> temporaryList = Arrays.asList(currentInvisibleLayers); 653 this.dataStoreMap.values().stream().flatMap(Collection::stream).map(MVTTile::getData) 654 .forEach(dataStore -> dataStore.getAllPrimitives().parallelStream() 655 .forEach(primitive -> primitive.setVisible(!temporaryList.contains(primitive.getLayer())))); 656 } 657 658 @Override 659 public boolean addSelectionListener(IDataSelectionListener<VectorPrimitive, VectorNode, VectorWay, VectorRelation, VectorDataSet> listener) { 660 if (!this.listeners.containsListener(listener)) { 661 this.listeners.addListener(listener); 662 } 663 return this.listeners.containsListener(listener); 664 } 665 666 @Override 667 public boolean removeSelectionListener( 668 IDataSelectionListener<VectorPrimitive, VectorNode, VectorWay, VectorRelation, VectorDataSet> listener) { 669 if (this.listeners.containsListener(listener)) { 670 this.listeners.removeListener(listener); 671 } 672 return this.listeners.containsListener(listener); 673 } 674}