Ticket #11834: 0001-Made-MapCSS-use-the-visitor-pattern-to-retrive-tags-.patch

File 0001-Made-MapCSS-use-the-visitor-pattern-to-retrive-tags-.patch, 4.8 KB (added by michael2402, 11 years ago)
  • src/org/openstreetmap/josm/data/osm/AbstractPrimitive.java

    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;  
    2626*/
    2727public abstract class AbstractPrimitive implements IPrimitive {
    2828
     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
    2942    private static final AtomicLong idCounter = new AtomicLong(0);
    3043
    3144    static long generateUniqueId() {
    public abstract class AbstractPrimitive implements IPrimitive {  
    495508    }
    496509
    497510    /**
     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    /**
    498524     * Sets the keys of this primitives to the key/value pairs in <code>keys</code>.
    499525     * Old key/value pairs are removed.
    500526     * If <code>keys</code> is null, clears existing key/value pairs.
  • src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java

    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;  
    2929
    3030import org.openstreetmap.josm.Main;
    3131import org.openstreetmap.josm.data.Version;
     32import org.openstreetmap.josm.data.osm.AbstractPrimitive.KeyValueVisitor;
    3233import org.openstreetmap.josm.data.osm.Node;
    3334import org.openstreetmap.josm.data.osm.OsmPrimitive;
    3435import org.openstreetmap.josm.data.osm.Relation;
    public class MapCSSStyleSource extends StyleSource {  
    144145         *
    145146         * @author Michael Zangl
    146147         */
    147         private final class RuleCandidatesIterator implements Iterator<MapCSSRule> {
     148        private final class RuleCandidatesIterator implements Iterator<MapCSSRule>, KeyValueVisitor {
    148149            private final BitSet ruleCandidates;
    149150            private int next;
    150151
    151152            private RuleCandidatesIterator(BitSet ruleCandidates) {
    152153                this.ruleCandidates = ruleCandidates;
    153                 next = ruleCandidates.nextSetBit(0);
    154154            }
    155155
    156156            @Override
    public class MapCSSStyleSource extends StyleSource {  
    169169            public void remove() {
    170170                throw new UnsupportedOperationException();
    171171            }
     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            }
    172188        }
    173189
    174190        /**
    public class MapCSSStyleSource extends StyleSource {  
    317333            final BitSet ruleCandidates = new BitSet(rules.size());
    318334            ruleCandidates.or(remaining);
    319335
    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;
    328340        }
    329341
    330342        /**