Changeset 17642 in josm for trunk/src


Ignore:
Timestamp:
2021-03-23T00:47:34+01:00 (3 years ago)
Author:
simon04
Message:

see #19078 - Common interface TagCondition

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagCheckerRule.java

    r17640 r17642  
    1717import org.openstreetmap.josm.gui.mappaint.Keyword;
    1818import org.openstreetmap.josm.gui.mappaint.mapcss.Condition;
     19import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.TagCondition;
    1920import org.openstreetmap.josm.gui.mappaint.mapcss.Expression;
    2021import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction;
     
    237238        try {
    238239            final Condition c = matchingSelector.getConditions().get(index);
    239             final Tag tag = c instanceof Condition.ToTagConvertable
    240                     ? ((Condition.ToTagConvertable) c).asTag(p)
     240            final Tag tag = c instanceof TagCondition
     241                    ? ((TagCondition) c).asTag(p)
    241242                    : null;
    242243            if (tag == null) {
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Condition.java

    r11713 r17642  
    22package org.openstreetmap.josm.gui.mappaint.mapcss;
    33
    4 import org.openstreetmap.josm.data.osm.OsmPrimitive;
    54import org.openstreetmap.josm.data.osm.Tag;
     5import org.openstreetmap.josm.data.osm.Tagged;
    66import org.openstreetmap.josm.gui.mappaint.Environment;
     7import org.openstreetmap.josm.tools.CheckParameterUtil;
    78
    89/**
     
    1819     */
    1920    boolean applies(Environment e);
     21
     22    /**
     23     * Checks if the condition applies in the given {@link Tagged} element.
     24     * @param tagged The tagged to check.
     25     * @return <code>true</code> if the condition applies.
     26     */
     27    default boolean applies(Tagged tagged) {
     28        return false;
     29    }
    2030
    2131    /**
     
    3747     * This is a condition that can be converted to a tag
    3848     * @author Michael Zangl
    39      * @since 10674
     49     * @since 10674 (ToTagConvertable), 17642 (TagCondition)
    4050     */
    41     @FunctionalInterface
    42     interface ToTagConvertable {
     51    interface TagCondition extends Condition {
     52
     53        @Override
     54        default boolean applies(Environment e) {
     55            CheckParameterUtil.ensureThat(!e.isLinkContext(), "Illegal state: TagCondition not supported in LINK context");
     56            return applies(e.osm);
     57        }
     58
     59        @Override
     60        boolean applies(Tagged tagged);
     61
    4362        /**
    4463         * Converts the current condition to a tag
    45          * @param primitive A primitive to use as context. May be ignored.
     64         * @param tagged A tagged object to use as context. May be ignored.
    4665         * @return A tag with the key/value of this condition.
    4766         */
    48         Tag asTag(OsmPrimitive primitive);
     67        Tag asTag(Tagged tagged);
    4968    }
    5069}
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/ConditionFactory.java

    r17602 r17642  
    2323import org.openstreetmap.josm.data.osm.Relation;
    2424import org.openstreetmap.josm.data.osm.Tag;
     25import org.openstreetmap.josm.data.osm.Tagged;
    2526import org.openstreetmap.josm.data.osm.search.SearchCompiler.InDataSourceArea;
    2627import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon;
     
    3031import org.openstreetmap.josm.gui.mappaint.Environment;
    3132import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context;
    32 import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.ToTagConvertable;
     33import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.TagCondition;
    3334import org.openstreetmap.josm.tools.CheckParameterUtil;
    3435import org.openstreetmap.josm.tools.JosmRuntimeException;
     
    257258     * Extra class for performance reasons.
    258259     */
    259     public static class SimpleKeyValueCondition implements Condition, ToTagConvertable {
     260    public static class SimpleKeyValueCondition implements TagCondition {
    260261        /**
    261262         * The key to search for.
     
    278279
    279280        @Override
    280         public boolean applies(Environment e) {
    281             return v.equals(e.osm.get(k));
    282         }
    283 
    284         @Override
    285         public Tag asTag(OsmPrimitive primitive) {
     281        public boolean applies(Tagged osm) {
     282            return v.equals(osm.get(k));
     283        }
     284
     285        @Override
     286        public Tag asTag(Tagged primitive) {
    286287            return new Tag(k, v);
    287288        }
     
    298299     *
    299300     */
    300     public static class KeyValueCondition implements Condition, ToTagConvertable {
     301    public static class KeyValueCondition implements TagCondition {
    301302        /**
    302303         * The key to search for.
     
    341342
    342343        @Override
    343         public boolean applies(Environment env) {
    344             return op.eval(env.osm.get(k), considerValAsKey ? env.osm.get(v) : v);
    345         }
    346 
    347         @Override
    348         public Tag asTag(OsmPrimitive primitive) {
     344        public boolean applies(Tagged osm) {
     345            return op.eval(osm.get(k), considerValAsKey ? osm.get(v) : v);
     346        }
     347
     348        @Override
     349        public Tag asTag(Tagged primitive) {
    349350            return new Tag(k, v);
    350351        }
     
    379380        }
    380381
    381         protected boolean matches(Environment env) {
    382             final String value = env.osm.get(k);
     382        protected boolean matches(Tagged osm) {
     383            final String value = osm.get(k);
    383384            return value != null && pattern.matcher(value).find();
    384385        }
    385386
    386387        @Override
    387         public boolean applies(Environment env) {
     388        public boolean applies(Tagged osm) {
    388389            if (Op.REGEX == op) {
    389                 return matches(env);
     390                return matches(osm);
    390391            } else if (Op.NREGEX == op) {
    391                 return !matches(env);
     392                return !matches(osm);
    392393            } else {
    393394                throw new IllegalStateException();
     
    420421
    421422        @Override
    422         protected boolean matches(Environment env) {
    423             return env.osm.getKeys().entrySet().stream()
     423        protected boolean matches(Tagged osm) {
     424            return osm.getKeys().entrySet().stream()
    424425                    .anyMatch(kv -> keyPattern.matcher(kv.getKey()).find() && pattern.matcher(kv.getValue()).find());
    425426        }
     
    537538     * @see KeyRegexpCondition
    538539     */
    539     public static class KeyCondition implements Condition, ToTagConvertable {
     540    public static class KeyCondition implements TagCondition {
    540541
    541542        /**
     
    567568
    568569        @Override
    569         public boolean applies(Environment e) {
    570             CheckParameterUtil.ensureThat(!e.isLinkContext(), "Illegal state: KeyCondition not supported in LINK context");
     570        public boolean applies(Tagged osm) {
    571571            switch (matchType) {
    572572                case TRUE:
    573                     return e.osm.isKeyTrue(label) ^ negateResult;
     573                    return osm.isKeyTrue(label) ^ negateResult;
    574574                case FALSE:
    575                     return e.osm.isKeyFalse(label) ^ negateResult;
     575                    return osm.isKeyFalse(label) ^ negateResult;
    576576                case ANY_CONTAINS:
    577577                case ANY_STARTS_WITH:
    578578                case ANY_ENDS_WITH:
    579                     return e.osm.keys().anyMatch(keyPredicate()) ^ negateResult;
     579                    return osm.keys().anyMatch(keyPredicate()) ^ negateResult;
    580580                default:
    581                     return e.osm.hasKey(label) ^ negateResult;
     581                    return osm.hasKey(label) ^ negateResult;
    582582            }
    583583        }
     
    604604         */
    605605        @Override
    606         public Tag asTag(OsmPrimitive p) {
     606        public Tag asTag(Tagged p) {
    607607            String key = label;
    608608            Predicate<String> keyPredicate = keyPredicate();
     
    622622     * KeyPatternCondition represents a conditions matching keys based on a pattern.
    623623     */
    624     public static class KeyRegexpCondition implements Condition, ToTagConvertable {
     624    public static class KeyRegexpCondition implements TagCondition {
    625625
    626626        /**
     
    644644
    645645        @Override
    646         public boolean applies(Environment e) {
    647             CheckParameterUtil.ensureThat(!e.isLinkContext(), "Illegal state: KeyCondition not supported in LINK context");
    648             return e.osm.keys().anyMatch(pattern.asPredicate()) ^ negateResult;
     646        public boolean applies(Tagged osm) {
     647            return osm.keys().anyMatch(pattern.asPredicate()) ^ negateResult;
    649648        }
    650649
     
    659658         */
    660659        @Override
    661         public Tag asTag(OsmPrimitive p) {
     660        public Tag asTag(Tagged p) {
    662661            String key = p.keys().filter(pattern.asPredicate()).findAny().orElse(pattern.pattern());
    663662            return new Tag(key, p.get(key));
Note: See TracChangeset for help on using the changeset viewer.