1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.vector;
|
---|
3 |
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.Collections;
|
---|
6 | import java.util.HashSet;
|
---|
7 | import java.util.LinkedList;
|
---|
8 | import java.util.Map;
|
---|
9 | import java.util.Set;
|
---|
10 | import java.util.concurrent.locks.ReentrantReadWriteLock;
|
---|
11 |
|
---|
12 | import org.openstreetmap.gui.jmapviewer.Tile;
|
---|
13 | import org.openstreetmap.josm.data.DataSource;
|
---|
14 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
15 | import org.openstreetmap.josm.data.osm.INode;
|
---|
16 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
17 | import org.openstreetmap.josm.data.osm.IRelation;
|
---|
18 | import org.openstreetmap.josm.data.osm.IWay;
|
---|
19 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
20 | import org.openstreetmap.josm.data.osm.QuadBucketPrimitiveStore;
|
---|
21 | import org.openstreetmap.josm.data.osm.Storage;
|
---|
22 | import 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 | */
|
---|
29 | class 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 | }
|
---|