Ignore:
Timestamp:
2014-01-06T18:40:17+01:00 (10 years ago)
Author:
simon04
Message:

see #9414 - MapCSS validator: some performance imrovements (pre-compile regular expressions, drop regular expressions for key presence checks)

File:
1 edited

Legend:

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

    r6630 r6645  
    1616import org.openstreetmap.josm.gui.mappaint.Environment;
    1717import org.openstreetmap.josm.tools.CheckParameterUtil;
     18import org.openstreetmap.josm.tools.Predicate;
    1819import org.openstreetmap.josm.tools.Predicates;
    1920import org.openstreetmap.josm.tools.Utils;
     
    2627        switch (context) {
    2728        case PRIMITIVE:
    28             return new KeyValueCondition(k, v, op, considerValAsKey);
     29            return KeyValueRegexpCondition.SUPPORTED_OPS.contains(op) && !considerValAsKey
     30                    ? new KeyValueRegexpCondition(k, v, op, false)
     31                    : new KeyValueCondition(k, v, op, considerValAsKey);
    2932        case LINK:
    3033            if (considerValAsKey)
     
    180183        public String toString() {
    181184            return "[" + k + "'" + op + "'" + v + "]";
     185        }
     186    }
     187
     188    public static class KeyValueRegexpCondition extends KeyValueCondition {
     189
     190        public final Pattern pattern;
     191        public static final EnumSet<Op> SUPPORTED_OPS = EnumSet.of(Op.REGEX, Op.NREGEX);
     192
     193        public KeyValueRegexpCondition(String k, String v, Op op, boolean considerValAsKey) {
     194            super(k, v, op, considerValAsKey);
     195            CheckParameterUtil.ensureThat(!considerValAsKey, "considerValAsKey is not supported");
     196            CheckParameterUtil.ensureThat(SUPPORTED_OPS.contains(op), "Op must be REGEX or NREGEX");
     197            this.pattern = Pattern.compile(v);
     198        }
     199
     200        @Override
     201        public boolean applies(Environment env) {
     202            final String value = env.osm.get(k);
     203            return value != null && (op.equals(Op.REGEX)
     204                    ? pattern.matcher(value).find()
     205                    : !pattern.matcher(value).find());
    182206        }
    183207    }
     
    244268        public final boolean negateResult;
    245269        public final KeyMatchType matchType;
     270        public Predicate<String> containsPattern;
    246271
    247272        public KeyCondition(String label, boolean negateResult, KeyMatchType matchType){
     
    249274            this.negateResult = negateResult;
    250275            this.matchType = matchType;
     276            this.containsPattern = KeyMatchType.REGEX.equals(matchType)
     277                    ? Predicates.stringContainsPattern(Pattern.compile(label))
     278                    : null;
    251279        }
    252280
     
    259287                else if (KeyMatchType.FALSE.equals(matchType))
    260288                    return e.osm.isKeyFalse(label) ^ negateResult;
    261                 else if (KeyMatchType.REGEX.equals(matchType))
    262                     return Utils.exists(e.osm.keySet(), Predicates.stringContainsPattern(Pattern.compile(label))) ^ negateResult;
    263                 else
     289                else if (KeyMatchType.REGEX.equals(matchType)) {
     290                    return Utils.exists(e.osm.keySet(), containsPattern) ^ negateResult;
     291                } else {
    264292                    return e.osm.hasKey(label) ^ negateResult;
     293                }
    265294            case LINK:
    266295                Utils.ensure(false, "Illegal state: KeyCondition not supported in LINK context");
Note: See TracChangeset for help on using the changeset viewer.