source: josm/trunk/src/org/openstreetmap/josm/data/vector/DataStore.java@ 17862

Last change on this file since 17862 was 17862, checked in by simon04, 3 years ago

fix #17177 - Add support for Mapbox Vector Tile (patch by taylor.smock)

Signed-off-by: Taylor Smock <tsmock@…>

File size: 4.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.vector;
3
4import java.util.Collection;
5import java.util.Collections;
6import java.util.HashSet;
7import java.util.LinkedList;
8import java.util.Map;
9import java.util.Set;
10import java.util.concurrent.locks.ReentrantReadWriteLock;
11
12import org.openstreetmap.gui.jmapviewer.Tile;
13import org.openstreetmap.josm.data.DataSource;
14import org.openstreetmap.josm.data.osm.DataSet;
15import org.openstreetmap.josm.data.osm.INode;
16import org.openstreetmap.josm.data.osm.IPrimitive;
17import org.openstreetmap.josm.data.osm.IRelation;
18import org.openstreetmap.josm.data.osm.IWay;
19import org.openstreetmap.josm.data.osm.PrimitiveId;
20import org.openstreetmap.josm.data.osm.QuadBucketPrimitiveStore;
21import org.openstreetmap.josm.data.osm.Storage;
22import org.openstreetmap.josm.tools.Logging;
23
24/**
25 * A class that stores data (essentially a simple {@link DataSet})
26 * @author Taylor Smock
27 * @since xxx
28 */
29class DataStore<O extends IPrimitive, N extends INode, W extends IWay<N>, R extends IRelation<?>> {
30 /**
31 * This literally only exists to make {@link QuadBucketPrimitiveStore#removePrimitive} public
32 *
33 * @param <N> The node type
34 * @param <W> The way type
35 * @param <R> The relation type
36 */
37 static class LocalQuadBucketPrimitiveStore<N extends INode, W extends IWay<N>, R extends IRelation<?>>
38 extends QuadBucketPrimitiveStore<N, W, R> {
39 // Allow us to remove primitives (protected in {@link QuadBucketPrimitiveStore})
40 @Override
41 public void removePrimitive(IPrimitive primitive) {
42 super.removePrimitive(primitive);
43 }
44 }
45
46 protected final LocalQuadBucketPrimitiveStore<N, W, R> store = new LocalQuadBucketPrimitiveStore<>();
47 protected final Storage<O> allPrimitives = new Storage<>(new Storage.PrimitiveIdHash(), true);
48 // TODO what happens when I use hashCode?
49 protected final Set<Tile> addedTiles = Collections.synchronizedSet(new HashSet<>());
50 protected final Map<PrimitiveId, O> primitivesMap = Collections.synchronizedMap(allPrimitives
51 .foreignKey(new Storage.PrimitiveIdHash()));
52 protected final Collection<DataSource> dataSources = new LinkedList<>();
53 private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
54
55 public QuadBucketPrimitiveStore<N, W, R> getStore() {
56 return this.store;
57 }
58
59 public Storage<O> getAllPrimitives() {
60 return this.allPrimitives;
61 }
62
63 /**
64 * Get the primitives map.
65 * @implNote The returned map is a {@link Collections#synchronizedMap}. Please synchronize on it.
66 * @return The Primitives map.
67 */
68 public Map<PrimitiveId, O> getPrimitivesMap() {
69 return this.primitivesMap;
70 }
71
72 public Collection<DataSource> getDataSources() {
73 return Collections.unmodifiableCollection(dataSources);
74 }
75
76 /**
77 * Add a datasource to this data set
78 * @param dataSource The data soure to add
79 */
80 public void addDataSource(DataSource dataSource) {
81 this.dataSources.add(dataSource);
82 }
83
84 /**
85 * Add a primitive to this dataset
86 * @param primitive The primitive to remove
87 */
88 @SuppressWarnings("squid:S2445")
89 protected void removePrimitive(O primitive) {
90 if (primitive == null) {
91 return;
92 }
93 try {
94 this.readWriteLock.writeLock().lockInterruptibly();
95 if (this.allPrimitives.contains(primitive)) {
96 this.store.removePrimitive(primitive);
97 this.allPrimitives.remove(primitive);
98 this.primitivesMap.remove(primitive.getPrimitiveId());
99 }
100 } catch (InterruptedException e) {
101 Logging.error(e);
102 Thread.currentThread().interrupt();
103 } finally {
104 if (this.readWriteLock.isWriteLockedByCurrentThread()) {
105 this.readWriteLock.writeLock().unlock();
106 }
107 }
108 }
109
110 /**
111 * Add a primitive to this dataset
112 * @param primitive The primitive to add
113 */
114 protected void addPrimitive(O primitive) {
115 this.store.addPrimitive(primitive);
116 this.allPrimitives.add(primitive);
117 this.primitivesMap.put(primitive.getPrimitiveId(), primitive);
118 }
119
120 /**
121 * Get the read/write lock for this dataset
122 * @return The read/write lock
123 */
124 protected ReentrantReadWriteLock getReadWriteLock() {
125 return this.readWriteLock;
126 }
127}
Note: See TracBrowser for help on using the repository browser.