1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.Collections;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.data.osm.FilterMatcher.FilterType;
|
---|
8 | import org.openstreetmap.josm.tools.Utils;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | *
|
---|
12 | * @author Petr_Dlouhý
|
---|
13 | */
|
---|
14 | public final class FilterWorker {
|
---|
15 |
|
---|
16 | private FilterWorker() {
|
---|
17 | // Hide default constructor for utils classes
|
---|
18 | }
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Apply the filters to the primitives of the data set.
|
---|
22 | *
|
---|
23 | * @param all the collection of primitives for that the filter state should
|
---|
24 | * be updated
|
---|
25 | * @param filterMatcher the FilterMatcher
|
---|
26 | * @return true, if the filter state (normal / disabled / hidden)
|
---|
27 | * of any primitive has changed in the process
|
---|
28 | */
|
---|
29 | public static boolean executeFilters(Collection<OsmPrimitive> all, FilterMatcher filterMatcher) {
|
---|
30 | boolean changed = false;
|
---|
31 | // first relations, then ways and nodes last; this is required to resolve dependencies
|
---|
32 | changed = doExecuteFilters(Utils.filter(all, OsmPrimitive.relationPredicate), filterMatcher);
|
---|
33 | changed |= doExecuteFilters(Utils.filter(all, OsmPrimitive.wayPredicate), filterMatcher);
|
---|
34 | changed |= doExecuteFilters(Utils.filter(all, OsmPrimitive.nodePredicate), filterMatcher);
|
---|
35 | return changed;
|
---|
36 | }
|
---|
37 |
|
---|
38 | private static boolean doExecuteFilters(Collection<OsmPrimitive> all, FilterMatcher filterMatcher) {
|
---|
39 |
|
---|
40 | boolean changed = false;
|
---|
41 |
|
---|
42 | for (OsmPrimitive primitive: all) {
|
---|
43 | FilterType hiddenType = filterMatcher.isHidden(primitive);
|
---|
44 | if (hiddenType != FilterType.NOT_FILTERED) {
|
---|
45 | changed |= primitive.setDisabledState(true);
|
---|
46 | primitive.setHiddenType(hiddenType == FilterType.EXPLICIT);
|
---|
47 | } else {
|
---|
48 | FilterType disabledType = filterMatcher.isDisabled(primitive);
|
---|
49 | if (disabledType != FilterType.NOT_FILTERED) {
|
---|
50 | changed |= primitive.setDisabledState(false);
|
---|
51 | primitive.setDisabledType(disabledType == FilterType.EXPLICIT);
|
---|
52 | } else {
|
---|
53 | changed |= primitive.unsetDisabledState();
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 | return changed;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public static boolean executeFilters(OsmPrimitive primitive, FilterMatcher filterMatcher) {
|
---|
61 | return doExecuteFilters(Collections.singleton(primitive), filterMatcher);
|
---|
62 | }
|
---|
63 |
|
---|
64 | public static void clearFilterFlags(Collection<OsmPrimitive> prims) {
|
---|
65 | for (OsmPrimitive osm : prims) {
|
---|
66 | osm.unsetDisabledState();
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|