From 3b1c413d92700aff04349e2fa0edd7cf85ec8289 Mon Sep 17 00:00:00 2001
From: Michael Zangl <michael.zangl@student.kit.edu>
Date: Sat, 8 Aug 2015 15:49:21 +0200
Subject: [PATCH] Made MapCSS use the visitor pattern to retrive tags of
primitives.
---
.../josm/data/osm/AbstractPrimitive.java | 26 ++++++++++++++++++
.../gui/mappaint/mapcss/MapCSSStyleSource.java | 32 +++++++++++++++-------
2 files changed, 48 insertions(+), 10 deletions(-)
diff --git a/src/org/openstreetmap/josm/data/osm/AbstractPrimitive.java b/src/org/openstreetmap/josm/data/osm/AbstractPrimitive.java
index 2d30e5f..7276103 100644
|
a
|
b
|
import org.openstreetmap.josm.tools.Utils;
|
| 26 | 26 | */ |
| 27 | 27 | public abstract class AbstractPrimitive implements IPrimitive { |
| 28 | 28 | |
| | 29 | /** |
| | 30 | * This is a visitor that can be used to loop over the keys/values of this primitive. |
| | 31 | * @author Michael Zangl |
| | 32 | */ |
| | 33 | public interface KeyValueVisitor { |
| | 34 | /** |
| | 35 | * This method gets called for every tag recieved. |
| | 36 | * @param key The key |
| | 37 | * @param value The value |
| | 38 | */ |
| | 39 | void visitKeyValue(String key, String value); |
| | 40 | } |
| | 41 | |
| 29 | 42 | private static final AtomicLong idCounter = new AtomicLong(0); |
| 30 | 43 | |
| 31 | 44 | static long generateUniqueId() { |
| … |
… |
public abstract class AbstractPrimitive implements IPrimitive {
|
| 495 | 508 | } |
| 496 | 509 | |
| 497 | 510 | /** |
| | 511 | * Calls the visitor for every key/value pair of this primitive. |
| | 512 | * @param visitor The visitor to call. |
| | 513 | */ |
| | 514 | public void visitKeys(KeyValueVisitor visitor) { |
| | 515 | final String[] keys = this.keys; |
| | 516 | if (keys != null) { |
| | 517 | for (int i = 0; i < keys.length; i += 2) { |
| | 518 | visitor.visitKeyValue(keys[i], keys[i + 1]); |
| | 519 | } |
| | 520 | } |
| | 521 | } |
| | 522 | |
| | 523 | /** |
| 498 | 524 | * Sets the keys of this primitives to the key/value pairs in <code>keys</code>. |
| 499 | 525 | * Old key/value pairs are removed. |
| 500 | 526 | * If <code>keys</code> is null, clears existing key/value pairs. |
diff --git a/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java b/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java
index 49b2fc3..a97450b 100644
|
a
|
b
|
import java.util.zip.ZipFile;
|
| 29 | 29 | |
| 30 | 30 | import org.openstreetmap.josm.Main; |
| 31 | 31 | import org.openstreetmap.josm.data.Version; |
| | 32 | import org.openstreetmap.josm.data.osm.AbstractPrimitive.KeyValueVisitor; |
| 32 | 33 | import org.openstreetmap.josm.data.osm.Node; |
| 33 | 34 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
| 34 | 35 | import org.openstreetmap.josm.data.osm.Relation; |
| … |
… |
public class MapCSSStyleSource extends StyleSource {
|
| 144 | 145 | * |
| 145 | 146 | * @author Michael Zangl |
| 146 | 147 | */ |
| 147 | | private final class RuleCandidatesIterator implements Iterator<MapCSSRule> { |
| | 148 | private final class RuleCandidatesIterator implements Iterator<MapCSSRule>, KeyValueVisitor { |
| 148 | 149 | private final BitSet ruleCandidates; |
| 149 | 150 | private int next; |
| 150 | 151 | |
| 151 | 152 | private RuleCandidatesIterator(BitSet ruleCandidates) { |
| 152 | 153 | this.ruleCandidates = ruleCandidates; |
| 153 | | next = ruleCandidates.nextSetBit(0); |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | @Override |
| … |
… |
public class MapCSSStyleSource extends StyleSource {
|
| 169 | 169 | public void remove() { |
| 170 | 170 | throw new UnsupportedOperationException(); |
| 171 | 171 | } |
| | 172 | |
| | 173 | @Override |
| | 174 | public void visitKeyValue(String key, String value) { |
| | 175 | MapCSSKeyRules v = index.get(key); |
| | 176 | if (v != null) { |
| | 177 | BitSet rs = v.get(value); |
| | 178 | ruleCandidates.or(rs); |
| | 179 | } |
| | 180 | } |
| | 181 | |
| | 182 | /** |
| | 183 | * Call this before using the iterator. |
| | 184 | */ |
| | 185 | public void prepare() { |
| | 186 | next = ruleCandidates.nextSetBit(0); |
| | 187 | } |
| 172 | 188 | } |
| 173 | 189 | |
| 174 | 190 | /** |
| … |
… |
public class MapCSSStyleSource extends StyleSource {
|
| 317 | 333 | final BitSet ruleCandidates = new BitSet(rules.size()); |
| 318 | 334 | ruleCandidates.or(remaining); |
| 319 | 335 | |
| 320 | | for (Map.Entry<String, String> e : osm.getKeys().entrySet()) { |
| 321 | | MapCSSKeyRules v = index.get(e.getKey()); |
| 322 | | if (v != null) { |
| 323 | | BitSet rs = v.get(e.getValue()); |
| 324 | | ruleCandidates.or(rs); |
| 325 | | } |
| 326 | | } |
| 327 | | return new RuleCandidatesIterator(ruleCandidates); |
| | 336 | RuleCandidatesIterator res = new RuleCandidatesIterator(ruleCandidates); |
| | 337 | osm.visitKeys(res); |
| | 338 | res.prepare(); |
| | 339 | return res; |
| 328 | 340 | } |
| 329 | 341 | |
| 330 | 342 | /** |