source: josm/trunk/src/org/openstreetmap/josm/data/osm/OsmData.java@ 13914

Last change on this file since 13914 was 13914, checked in by Don-vip, 6 years ago

move getPrimitiveById() default implementation from DataSet to OsmData

  • Property svn:eol-style set to native
File size: 15.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.util.Collection;
5import java.util.List;
6import java.util.concurrent.locks.Lock;
7import java.util.function.Predicate;
8
9import org.openstreetmap.josm.data.Data;
10import org.openstreetmap.josm.data.DataSource;
11import org.openstreetmap.josm.data.ProjectionBounds;
12import org.openstreetmap.josm.data.SelectionChangedListener;
13import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
14import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
15
16/**
17 * Abstraction of {@link DataSet}.
18 * This class holds OSM data but does not rely on implementation types,
19 * allowing plugins to define their own representation of OSM data if needed.
20 * @param <O> the base type of OSM primitives
21 * @param <N> type representing OSM nodes
22 * @param <W> type representing OSM ways
23 * @param <R> type representing OSM relations
24 * @since 13764
25 */
26public interface OsmData<O extends IPrimitive, N extends INode, W extends IWay<N>, R extends IRelation<?>> extends Data, Lockable {
27
28 // --------------
29 // Metadata
30 // --------------
31
32 /**
33 * Replies the API version this dataset was created from. May be null.
34 * @return the API version this dataset was created from. May be null.
35 */
36 String getVersion();
37
38 /**
39 * Returns the name of this data set (optional).
40 * @return the name of this data set. Can be {@code null}
41 * @since 12718
42 */
43 String getName();
44
45 /**
46 * Sets the name of this data set.
47 * @param name the new name of this data set. Can be {@code null} to reset it
48 * @since 12718
49 */
50 void setName(String name);
51
52 // --------------------
53 // OSM primitives
54 // --------------------
55
56 /**
57 * Adds a primitive.
58 * @param primitive the primitive
59 */
60 void addPrimitive(O primitive);
61
62 /**
63 * Removes all primitives.
64 */
65 void clear();
66
67 /**
68 * Searches for nodes in the given bounding box.
69 * @param bbox the bounding box
70 * @return List of nodes in the given bbox. Can be empty but not null
71 */
72 List<N> searchNodes(BBox bbox);
73
74 /**
75 * Determines if the given node can be retrieved in the data set through its bounding box. Useful for dataset consistency test.
76 * @param n The node to search
77 * @return {@code true} if {@code n} can be retrieved in this data set, {@code false} otherwise
78 */
79 boolean containsNode(N n);
80
81 /**
82 * Searches for ways in the given bounding box.
83 * @param bbox the bounding box
84 * @return List of ways in the given bbox. Can be empty but not null
85 */
86 List<W> searchWays(BBox bbox);
87
88 /**
89 * Determines if the given way can be retrieved in the data set through its bounding box. Useful for dataset consistency test.
90 * @param w The way to search
91 * @return {@code true} if {@code w} can be retrieved in this data set, {@code false} otherwise
92 */
93 boolean containsWay(W w);
94
95 /**
96 * Searches for relations in the given bounding box.
97 * @param bbox the bounding box
98 * @return List of relations in the given bbox. Can be empty but not null
99 */
100 List<R> searchRelations(BBox bbox);
101
102 /**
103 * Determines if the given relation can be retrieved in the data set through its bounding box. Useful for dataset consistency test.
104 * @param r The relation to search
105 * @return {@code true} if {@code r} can be retrieved in this data set, {@code false} otherwise
106 */
107 boolean containsRelation(R r);
108
109 /**
110 * Returns a primitive with a given id from the data set. null, if no such primitive exists
111 *
112 * @param id uniqueId of the primitive. Might be &lt; 0 for newly created primitives
113 * @param type the type of the primitive. Must not be null.
114 * @return the primitive
115 * @throws NullPointerException if type is null
116 */
117 default O getPrimitiveById(long id, OsmPrimitiveType type) {
118 return getPrimitiveById(new SimplePrimitiveId(id, type));
119 }
120
121 /**
122 * Returns a primitive with a given id from the data set. null, if no such primitive exists
123 *
124 * @param primitiveId type and uniqueId of the primitive. Might be &lt; 0 for newly created primitives
125 * @return the primitive
126 */
127 O getPrimitiveById(PrimitiveId primitiveId);
128
129 /**
130 * Gets a filtered collection of primitives matching the given predicate.
131 * @param <T> The primitive type.
132 * @param predicate The predicate to match
133 * @return The list of primtives.
134 * @since 10590
135 */
136 <T extends O> Collection<T> getPrimitives(Predicate<? super O> predicate);
137
138 /**
139 * Replies an unmodifiable collection of nodes in this dataset
140 *
141 * @return an unmodifiable collection of nodes in this dataset
142 */
143 Collection<N> getNodes();
144
145 /**
146 * Replies an unmodifiable collection of ways in this dataset
147 *
148 * @return an unmodifiable collection of ways in this dataset
149 */
150 Collection<W> getWays();
151
152 /**
153 * Replies an unmodifiable collection of relations in this dataset
154 *
155 * @return an unmodifiable collection of relations in this dataset
156 */
157 Collection<R> getRelations();
158
159 /**
160 * Returns a collection containing all primitives of the dataset.
161 * @return A collection containing all primitives of the dataset. Data is not ordered
162 */
163 default Collection<O> allPrimitives() {
164 return getPrimitives(o -> true);
165 }
166
167 /**
168 * Returns a collection containing all not-deleted primitives.
169 * @return A collection containing all not-deleted primitives.
170 * @see OsmPrimitive#isDeleted
171 */
172 default Collection<O> allNonDeletedPrimitives() {
173 return getPrimitives(p -> !p.isDeleted());
174 }
175
176 /**
177 * Returns a collection containing all not-deleted complete primitives.
178 * @return A collection containing all not-deleted complete primitives.
179 * @see OsmPrimitive#isDeleted
180 * @see OsmPrimitive#isIncomplete
181 */
182 default Collection<O> allNonDeletedCompletePrimitives() {
183 return getPrimitives(primitive -> !primitive.isDeleted() && !primitive.isIncomplete());
184 }
185
186 /**
187 * Returns a collection containing all not-deleted complete physical primitives.
188 * @return A collection containing all not-deleted complete physical primitives (nodes and ways).
189 * @see OsmPrimitive#isDeleted
190 * @see OsmPrimitive#isIncomplete
191 */
192 default Collection<O> allNonDeletedPhysicalPrimitives() {
193 return getPrimitives(
194 primitive -> !primitive.isDeleted() && !primitive.isIncomplete() && !(primitive instanceof IRelation));
195 }
196
197 /**
198 * Returns a collection containing all modified primitives.
199 * @return A collection containing all modified primitives.
200 * @see OsmPrimitive#isModified
201 */
202 default Collection<O> allModifiedPrimitives() {
203 return getPrimitives(IPrimitive::isModified);
204 }
205
206 /**
207 * Returns a collection containing all primitives preserved from filtering.
208 * @return A collection containing all primitives preserved from filtering.
209 * @see OsmPrimitive#isPreserved
210 * @since 13309
211 */
212 default Collection<O> allPreservedPrimitives() {
213 return getPrimitives(IPrimitive::isPreserved);
214 }
215
216 // --------------
217 // Policies
218 // --------------
219
220 /**
221 * Get the download policy.
222 * @return the download policy
223 * @see #setDownloadPolicy(DownloadPolicy)
224 * @since 13453
225 */
226 DownloadPolicy getDownloadPolicy();
227
228 /**
229 * Sets the download policy.
230 * @param downloadPolicy the download policy
231 * @see #getUploadPolicy()
232 * @since 13453
233 */
234 void setDownloadPolicy(DownloadPolicy downloadPolicy);
235
236 /**
237 * Get the upload policy.
238 * @return the upload policy
239 * @see #setUploadPolicy(UploadPolicy)
240 */
241 UploadPolicy getUploadPolicy();
242
243 /**
244 * Sets the upload policy.
245 * @param uploadPolicy the upload policy
246 * @see #getUploadPolicy()
247 */
248 void setUploadPolicy(UploadPolicy uploadPolicy);
249
250 // --------------
251 // Locks
252 // --------------
253
254 /**
255 * Returns the lock used for reading.
256 * @return the lock used for reading
257 */
258 Lock getReadLock();
259
260 // ---------------
261 // Highlight
262 // ---------------
263
264 /**
265 * Returns an unmodifiable collection of *WaySegments* whose virtual
266 * nodes should be highlighted. WaySegments are used to avoid having
267 * to create a VirtualNode class that wouldn't have much purpose otherwise.
268 *
269 * @return unmodifiable collection of WaySegments
270 */
271 Collection<WaySegment> getHighlightedVirtualNodes();
272
273 /**
274 * Returns an unmodifiable collection of WaySegments that should be highlighted.
275 *
276 * @return unmodifiable collection of WaySegments
277 */
278 Collection<WaySegment> getHighlightedWaySegments();
279
280 /**
281 * clear all highlights of virtual nodes
282 */
283 void clearHighlightedVirtualNodes();
284
285 /**
286 * clear all highlights of way segments
287 */
288 void clearHighlightedWaySegments();
289
290 /**
291 * set what virtual nodes should be highlighted. Requires a Collection of
292 * *WaySegments* to avoid a VirtualNode class that wouldn't have much use otherwise.
293 * @param waySegments Collection of way segments
294 */
295 void setHighlightedVirtualNodes(Collection<WaySegment> waySegments);
296
297 /**
298 * set what virtual ways should be highlighted.
299 * @param waySegments Collection of way segments
300 */
301 void setHighlightedWaySegments(Collection<WaySegment> waySegments);
302
303 /**
304 * Adds a listener that gets notified whenever way segment / virtual nodes highlights change.
305 * @param listener The Listener
306 * @since 12014
307 */
308 void addHighlightUpdateListener(HighlightUpdateListener listener);
309
310 /**
311 * Removes a listener that was added with {@link #addHighlightUpdateListener(HighlightUpdateListener)}
312 * @param listener The Listener
313 * @since 12014
314 */
315 void removeHighlightUpdateListener(HighlightUpdateListener listener);
316
317 // ---------------
318 // Selection
319 // ---------------
320
321 /**
322 * Replies an unmodifiable collection of primitives currently selected
323 * in this dataset, except deleted ones. May be empty, but not null.
324 *
325 * When iterating through the set it is ordered by the order in which the primitives were added to the selection.
326 *
327 * @return unmodifiable collection of primitives
328 */
329 Collection<O> getSelected();
330
331 /**
332 * Replies an unmodifiable collection of primitives currently selected
333 * in this dataset, including deleted ones. May be empty, but not null.
334 *
335 * When iterating through the set it is ordered by the order in which the primitives were added to the selection.
336 *
337 * @return unmodifiable collection of primitives
338 */
339 Collection<O> getAllSelected();
340
341 /**
342 * Returns selected nodes.
343 * @return selected nodes
344 */
345 Collection<N> getSelectedNodes();
346
347 /**
348 * Returns selected ways.
349 * @return selected ways
350 */
351 Collection<W> getSelectedWays();
352
353 /**
354 * Returns selected relations.
355 * @return selected relations
356 */
357 Collection<R> getSelectedRelations();
358
359 /**
360 * Determines whether the selection is empty or not
361 * @return whether the selection is empty or not
362 */
363 boolean selectionEmpty();
364
365 /**
366 * Determines whether the given primitive is selected or not
367 * @param osm the primitive
368 * @return whether {@code osm} is selected or not
369 */
370 boolean isSelected(O osm);
371
372 /**
373 * Toggles the selected state of the given collection of primitives.
374 * @param osm The primitives to toggle
375 */
376 void toggleSelected(Collection<? extends PrimitiveId> osm);
377
378 /**
379 * Toggles the selected state of the given collection of primitives.
380 * @param osm The primitives to toggle
381 */
382 void toggleSelected(PrimitiveId... osm);
383
384 /**
385 * Sets the current selection to the primitives in <code>selection</code>
386 * and notifies all {@link SelectionChangedListener}.
387 *
388 * @param selection the selection
389 */
390 void setSelected(Collection<? extends PrimitiveId> selection);
391
392 /**
393 * Sets the current selection to the primitives in <code>osm</code>
394 * and notifies all {@link SelectionChangedListener}.
395 *
396 * @param osm the primitives to set. <code>null</code> values are ignored for now, but this may be removed in the future.
397 */
398 void setSelected(PrimitiveId... osm);
399
400 /**
401 * Adds the primitives in <code>selection</code> to the current selection
402 * and notifies all {@link SelectionChangedListener}.
403 *
404 * @param selection the selection
405 */
406 void addSelected(Collection<? extends PrimitiveId> selection);
407
408 /**
409 * Adds the primitives in <code>osm</code> to the current selection
410 * and notifies all {@link SelectionChangedListener}.
411 *
412 * @param osm the primitives to add
413 */
414 void addSelected(PrimitiveId... osm);
415
416 /**
417 * Removes the selection from every value in the collection.
418 * @param osm The collection of ids to remove the selection from.
419 */
420 void clearSelection(PrimitiveId... osm);
421
422 /**
423 * Removes the selection from every value in the collection.
424 * @param list The collection of ids to remove the selection from.
425 */
426 void clearSelection(Collection<? extends PrimitiveId> list);
427
428 /**
429 * Clears the current selection.
430 */
431 void clearSelection();
432
433 /**
434 * Add a listener that listens to selection changes in this specific data set.
435 * @param listener The listener.
436 * @see #removeSelectionListener(DataSelectionListener)
437 * @see SelectionEventManager#addSelectionListener(SelectionChangedListener,
438 * org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode)
439 * To add a global listener.
440 */
441 void addSelectionListener(DataSelectionListener listener);
442
443 /**
444 * Remove a listener that listens to selection changes in this specific data set.
445 * @param listener The listener.
446 * @see #addSelectionListener(DataSelectionListener)
447 */
448 void removeSelectionListener(DataSelectionListener listener);
449
450 // -------------------
451 // Miscellaneous
452 // -------------------
453
454 /**
455 * Returns the data sources bounding box.
456 * @return the data sources bounding box
457 */
458 default ProjectionBounds getDataSourceBoundingBox() {
459 BoundingXYVisitor bbox = new BoundingXYVisitor();
460 for (DataSource source : getDataSources()) {
461 bbox.visit(source.bounds);
462 }
463 if (bbox.hasExtend()) {
464 return bbox.getBounds();
465 }
466 return null;
467 }
468
469 /**
470 * Clear the mappaint cache for this DataSet.
471 * @since 13420
472 */
473 void clearMappaintCache();
474
475 /**
476 * Replies true if there is at least one primitive in this dataset with
477 * {@link IPrimitive#isModified()} == <code>true</code>.
478 *
479 * @return true if there is at least one primitive in this dataset with
480 * {@link IPrimitive#isModified()} == <code>true</code>.
481 */
482 default boolean isModified() {
483 return false;
484 }
485}
Note: See TracBrowser for help on using the repository browser.