source: josm/trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/ComputeStyleListWorker.java

Last change on this file was 17333, checked in by Don-vip, 3 years ago

see #20129 - Fix typos and misspellings in the code (patch by gaben)

  • Property svn:eol-style set to native
File size: 7.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm.visitor.paint;
3
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.List;
7import java.util.concurrent.ForkJoinTask;
8import java.util.concurrent.RecursiveTask;
9
10import org.openstreetmap.josm.data.osm.INode;
11import org.openstreetmap.josm.data.osm.IPrimitive;
12import org.openstreetmap.josm.data.osm.IRelation;
13import org.openstreetmap.josm.data.osm.IWay;
14import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
15import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer.StyleRecord;
16import org.openstreetmap.josm.gui.NavigatableComponent;
17import org.openstreetmap.josm.gui.mappaint.ElemStyles;
18import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
19import org.openstreetmap.josm.gui.mappaint.StyleElementList;
20import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
21import org.openstreetmap.josm.gui.mappaint.styleelement.AreaElement;
22import org.openstreetmap.josm.gui.mappaint.styleelement.AreaIconElement;
23import org.openstreetmap.josm.gui.mappaint.styleelement.NodeElement;
24import org.openstreetmap.josm.gui.mappaint.styleelement.StyleElement;
25import org.openstreetmap.josm.gui.mappaint.styleelement.TextElement;
26import org.openstreetmap.josm.spi.preferences.Config;
27import org.openstreetmap.josm.tools.JosmRuntimeException;
28import org.openstreetmap.josm.tools.bugreport.BugReport;
29
30/**
31 * Helper to compute style list.
32 * @since 11914 (extracted from StyledMapRenderer)
33 */
34public class ComputeStyleListWorker extends RecursiveTask<List<StyleRecord>> implements PrimitiveVisitor {
35
36 private static final long serialVersionUID = 1L;
37
38 private final transient List<? extends IPrimitive> input;
39 private final transient List<StyleRecord> output;
40
41 private final transient ElemStyles styles;
42 private final int directExecutionTaskSize;
43 private final double circum;
44 private final NavigatableComponent nc;
45
46 private final boolean drawArea;
47 private final boolean drawMultipolygon;
48 private final boolean drawRestriction;
49
50 /**
51 * Constructs a new {@code ComputeStyleListWorker}.
52 * @param circum distance on the map in meters that 100 screen pixels represent
53 * @param nc navigable component
54 * @param input the primitives to process
55 * @param output the list of styles to which styles will be added
56 * @param directExecutionTaskSize the threshold deciding whether to subdivide the tasks
57 * @since 13810 (signature)
58 */
59 ComputeStyleListWorker(double circum, NavigatableComponent nc,
60 final List<? extends IPrimitive> input, List<StyleRecord> output, int directExecutionTaskSize) {
61 this(circum, nc, input, output, directExecutionTaskSize, MapPaintStyles.getStyles());
62 }
63
64 /**
65 * Constructs a new {@code ComputeStyleListWorker}.
66 * @param circum distance on the map in meters that 100 screen pixels represent
67 * @param nc navigable component
68 * @param input the primitives to process
69 * @param output the list of styles to which styles will be added
70 * @param directExecutionTaskSize the threshold deciding whether to subdivide the tasks
71 * @param styles the {@link ElemStyles} instance used to generate primitive {@link StyleElement}s.
72 * @since 12964
73 * @since 13810 (signature)
74 */
75 ComputeStyleListWorker(double circum, NavigatableComponent nc,
76 final List<? extends IPrimitive> input, List<StyleRecord> output, int directExecutionTaskSize,
77 ElemStyles styles) {
78 this.circum = circum;
79 this.nc = nc;
80 this.input = input;
81 this.output = output;
82 this.directExecutionTaskSize = directExecutionTaskSize;
83 this.styles = styles;
84 this.drawArea = circum <= Config.getPref().getInt("mappaint.fillareas", 10_000_000);
85 this.drawMultipolygon = drawArea && Config.getPref().getBoolean("mappaint.multipolygon", true);
86 this.drawRestriction = Config.getPref().getBoolean("mappaint.restriction", true);
87 this.styles.setDrawMultipolygon(drawMultipolygon);
88 }
89
90 @Override
91 protected List<StyleRecord> compute() {
92 if (input.size() <= directExecutionTaskSize) {
93 return computeDirectly();
94 } else {
95 final Collection<ForkJoinTask<List<StyleRecord>>> tasks = new ArrayList<>();
96 for (int fromIndex = 0; fromIndex < input.size(); fromIndex += directExecutionTaskSize) {
97 final int toIndex = Math.min(fromIndex + directExecutionTaskSize, input.size());
98 tasks.add(new ComputeStyleListWorker(circum, nc, input.subList(fromIndex, toIndex),
99 new ArrayList<>(directExecutionTaskSize), directExecutionTaskSize, styles).fork());
100 }
101 for (ForkJoinTask<List<StyleRecord>> task : tasks) {
102 output.addAll(task.join());
103 }
104 return output;
105 }
106 }
107
108 /**
109 * Compute directly (without using fork/join) the style list. Only called for small input.
110 * @return list of computed style records
111 */
112 public List<StyleRecord> computeDirectly() {
113 MapCSSStyleSource.STYLE_SOURCE_LOCK.readLock().lock();
114 try {
115 for (final IPrimitive osm : input) {
116 acceptDrawable(osm);
117 }
118 return output;
119 } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
120 throw BugReport.intercept(e).put("input-size", input.size()).put("output-size", output.size());
121 } finally {
122 MapCSSStyleSource.STYLE_SOURCE_LOCK.readLock().unlock();
123 }
124 }
125
126 private void acceptDrawable(final IPrimitive osm) {
127 try {
128 if (osm.isDrawable()) {
129 osm.accept(this);
130 }
131 } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
132 throw BugReport.intercept(e).put("osm", osm);
133 }
134 }
135
136 @Override
137 public void visit(INode n) {
138 add(n, StyledMapRenderer.computeFlags(n, false));
139 }
140
141 @Override
142 public void visit(IWay<?> w) {
143 add(w, StyledMapRenderer.computeFlags(w, true));
144 }
145
146 @Override
147 public void visit(IRelation<?> r) {
148 add(r, StyledMapRenderer.computeFlags(r, true));
149 }
150
151 /**
152 * Add new style records for the given node.
153 * @param osm node
154 * @param flags flags
155 * @since 13810 (signature)
156 */
157 public void add(INode osm, int flags) {
158 StyleElementList sl = styles.get(osm, circum, nc);
159 for (StyleElement s : sl) {
160 output.add(new StyleRecord(s, osm, flags));
161 }
162 }
163
164 /**
165 * Add new style records for the given way.
166 * @param osm way
167 * @param flags flags
168 * @since 13810 (signature)
169 */
170 public void add(IWay<?> osm, int flags) {
171 StyleElementList sl = styles.get(osm, circum, nc);
172 for (StyleElement s : sl) {
173 if ((drawArea && (flags & StyledMapRenderer.FLAG_DISABLED) == 0) || !(s instanceof AreaElement)) {
174 output.add(new StyleRecord(s, osm, flags));
175 }
176 }
177 }
178
179 /**
180 * Add new style records for the given relation.
181 * @param osm relation
182 * @param flags flags
183 * @since 13810 (signature)
184 */
185 public void add(IRelation<?> osm, int flags) {
186 StyleElementList sl = styles.get(osm, circum, nc);
187 for (StyleElement s : sl) {
188 if (drawAreaElement(flags, s) ||
189 (drawMultipolygon && drawArea && s instanceof TextElement) ||
190 (drawRestriction && s instanceof NodeElement)) {
191 output.add(new StyleRecord(s, osm, flags));
192 }
193 }
194 }
195
196 private boolean drawAreaElement(int flags, StyleElement s) {
197 return drawMultipolygon && drawArea && (s instanceof AreaElement || s instanceof AreaIconElement)
198 && (flags & StyledMapRenderer.FLAG_DISABLED) == 0;
199 }
200}
Note: See TracBrowser for help on using the repository browser.