Ignore:
Timestamp:
2018-11-28T08:26:29+01:00 (5 years ago)
Author:
GerdP
Message:

see #17021: use constants for selector base strings

This reduces the memory footprint a bit and probably also improves speed of Selector.matchesBase()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Selector.java

    r14214 r14461  
    5151public interface Selector {
    5252
     53    /** selector base that matches anything. */
     54    String BASE_ANY = "*";
     55
     56    /** selector base that matches on OSM object node. */
     57    String BASE_NODE = "node";
     58
     59    /** selector base that matches on OSM object way. */
     60    String BASE_WAY = "way";
     61
     62    /** selector base that matches on OSM object relation. */
     63    String BASE_RELATION = "relation";
     64
     65    /** selector base that matches with any area regardless of whether the area border is only modelled with a single way or with a set of ways glued together with a relation.*/
     66    String BASE_AREA = "area";
     67
     68    /** selector base for special rules containing meta information. */
     69    String BASE_META = "meta";
     70
     71    /** selector base for style information not specific to nodes, ways or relations. */
     72    String BASE_CANVAS = "canvas";
     73
     74    /** selector base for artificial bases created to use preferences. */
     75    String BASE_SETTING = "setting";
     76
    5377    /**
    5478     * Apply the selector to the primitive and check if it matches.
     
    576600        public OptimizedGeneralSelector(String base, Pair<Integer, Integer> zoom, List<Condition> conds, Subpart subpart) {
    577601            super(conds);
    578             this.base = base;
     602            this.base = checkBase(base);
    579603            if (zoom != null) {
    580604                int a = zoom.a == null ? 0 : zoom.a;
     
    593617        public OptimizedGeneralSelector(String base, Range range, List<Condition> conds, Subpart subpart) {
    594618            super(conds);
    595             this.base = base;
     619            this.base = checkBase(base);
    596620            this.range = range;
    597621            this.subpart = subpart != null ? subpart : Subpart.DEFAULT_SUBPART;
     
    612636        }
    613637
     638        /**
     639         * Check if this is a known base and return the corresponding string constant.
     640         * @param base
     641         * @return the matching String constant
     642         */
     643        private static String checkBase(String base) {
     644            switch(base) {
     645            case "*": return BASE_ANY;
     646            case "node": return BASE_NODE;
     647            case "way": return BASE_WAY;
     648            case "relation": return BASE_RELATION;
     649            case "area": return BASE_AREA;
     650            case "meta": return BASE_META;
     651            case "canvas": return BASE_CANVAS;
     652            case "setting": return BASE_SETTING;
     653            default:
     654                throw new IllegalArgumentException("unknown selector " + base);
     655            }
     656        }
     657
    614658        public String getBase() {
    615659            return base;
     
    617661
    618662        public boolean matchesBase(OsmPrimitiveType type) {
    619             if ("*".equals(base)) {
     663            if (BASE_ANY.equals(base)) {
    620664                return true;
    621665            } else if (OsmPrimitiveType.NODE == type) {
    622                 return "node".equals(base);
     666                return BASE_NODE.equals(base);
    623667            } else if (OsmPrimitiveType.WAY == type) {
    624                 return "way".equals(base) || "area".equals(base);
     668                return BASE_WAY.equals(base) || BASE_AREA.equals(base);
    625669            } else if (OsmPrimitiveType.RELATION == type) {
    626                 return "area".equals(base) || "relation".equals(base) || "canvas".equals(base);
     670                return BASE_AREA.equals(base) || BASE_RELATION.equals(base) || BASE_CANVAS.equals(base);
    627671            }
    628672            return false;
     
    634678            } else {
    635679                if (p instanceof IRelation) {
    636                     if ("area".equals(base)) {
     680                    if (BASE_AREA.equals(base)) {
    637681                        return ((IRelation<?>) p).isMultipolygon();
    638                     } else if ("canvas".equals(base)) {
     682                    } else if (BASE_CANVAS.equals(base)) {
    639683                        return p.get("#canvas") != null;
    640684                    }
Note: See TracChangeset for help on using the changeset viewer.