Ticket #11769: documentation-key-condition.patch

File documentation-key-condition.patch, 8.0 KB (added by michael2402, 10 years ago)
  • src/org/openstreetmap/josm/gui/mappaint/mapcss/Condition.java

    diff --git a/src/org/openstreetmap/josm/gui/mappaint/mapcss/Condition.java b/src/org/openstreetmap/josm/gui/mappaint/mapcss/Condition.java
    index 94e171d..e10d8af 100644
    a b import org.openstreetmap.josm.Main;  
    1515import org.openstreetmap.josm.actions.search.SearchCompiler.InDataSourceArea;
    1616import org.openstreetmap.josm.data.osm.Node;
    1717import org.openstreetmap.josm.data.osm.OsmPrimitive;
     18import org.openstreetmap.josm.data.osm.OsmUtils;
    1819import org.openstreetmap.josm.data.osm.Relation;
    1920import org.openstreetmap.josm.data.osm.Tag;
    2021import org.openstreetmap.josm.data.osm.Way;
    public abstract class Condition {  
    8283        return new ExpressionCondition(e);
    8384    }
    8485
     86    /**
     87     * This is the operation that {@link KeyValueCondition} uses to match.
     88     */
    8589    public static enum Op {
    86         EQ, NEQ, GREATER_OR_EQUAL, GREATER, LESS_OR_EQUAL, LESS,
    87         REGEX, NREGEX, ONE_OF, BEGINS_WITH, ENDS_WITH, CONTAINS;
     90        /**
     91         * The value equals the given reference.
     92         */
     93        EQ,
     94        /**
     95         * The value does not equal the reference.
     96         */
     97        NEQ,
     98        /**
     99         * The value is greater than or equal to the given reference value (as float).
     100         */
     101        GREATER_OR_EQUAL,
     102        /**
     103         * The value is greater than the given reference value (as float).
     104         */
     105        GREATER,
     106        /**
     107         * The value is less than or equal to the given reference value (as float).
     108         */
     109        LESS_OR_EQUAL,
     110        /**
     111         * The value is less than the given reference value (as float).
     112         */
     113        LESS,
     114        /**
     115         * The reference is treated as regular expression and the value needs to match it.
     116         */
     117        REGEX,
     118        /**
     119         * The reference is treated as regular expression and the value needs to not match it.
     120         */
     121        NREGEX,
     122        /**
     123         * The reference is treated as a list separated by ';'. Spaces around the ; are ignored. The value needs to be equal one of the list elements.
     124         */
     125        ONE_OF,
     126        /**
     127         * The value needs to begin with the reference string.
     128         */
     129        BEGINS_WITH,
     130        /**
     131         * The value needs to end with the reference string.
     132         */
     133        ENDS_WITH,
     134         /**
     135          * The value needs to contain the reference string.
     136          */
     137        CONTAINS;
    88138
    89139        public static final Set<Op> NEGATED_OPS = EnumSet.of(NEQ, NREGEX);
    90140
     141        /**
     142         * Evaluates a value against a reference string.
     143         * @param testString The value. May be <code>null</code>
     144         * @param prototypeString The reference string-
     145         * @return <code>true</code> if and only if this operation matches for the given value/reference pair.
     146         */
    91147        public boolean eval(String testString, String prototypeString) {
    92148            if (testString == null && !NEGATED_OPS.contains(this))
    93149                return false;
    public abstract class Condition {  
    149205    }
    150206
    151207    /**
    152      * Most common case of a KeyValueCondition.
     208     * Most common case of a KeyValueCondition, this is the basic key=value case.
    153209     *
    154210     * Extra class for performance reasons.
    155211     */
    156212    public static class SimpleKeyValueCondition extends Condition {
     213        /**
     214         * The key to search for.
     215         */
    157216        public final String k;
     217        /**
     218         * The value to search for.
     219         */
    158220        public final String v;
    159221
     222        /**
     223         * Create a new SimpleKeyValueCondition.
     224         * @param k The key
     225         * @param v The value.
     226         */
    160227        public SimpleKeyValueCondition(String k, String v) {
    161228            this.k = k;
    162229            this.v = v;
    public abstract class Condition {  
    183250     *
    184251     */
    185252    public static class KeyValueCondition extends Condition {
    186 
     253        /**
     254         * The key to search for.
     255         */
    187256        public final String k;
     257        /**
     258         * The value to search for.
     259         */
    188260        public final String v;
     261        /**
     262         * The key/value match operation.
     263         */
    189264        public final Op op;
     265        /**
     266         * If this flag is set, {@link #v} is treated as a key and the value is the value set for that key.
     267         */
    190268        public boolean considerValAsKey;
    191269
    192270        /**
    public abstract class Condition {  
    281359        }
    282360    }
    283361
     362    /**
     363     * This defines how {@link KeyCondition} matches a given key.
     364     */
    284365    public static enum KeyMatchType {
    285         EQ, TRUE, FALSE, REGEX
     366        /**
     367         * The key needs to be equal to the given label.
     368         */
     369        EQ,
     370        /**
     371         * The key needs to have a true value (yes, ...)
     372         * @see OsmUtils#isTrue(String)
     373         */
     374        TRUE,
     375        /**
     376         * The key needs to have a false value (no, ...)
     377         * @see OsmUtils#isFalse(String)
     378         */
     379        FALSE,
     380        /**
     381         * The key needs to match the given regular expression.
     382         */
     383        REGEX
    286384    }
    287385
    288386    /**
    public abstract class Condition {  
    306404     */
    307405    public static class KeyCondition extends Condition {
    308406
     407        /**
     408         * The key name.
     409         */
    309410        public final String label;
     411        /**
     412         * If we should negate the result of the match.
     413         */
    310414        public final boolean negateResult;
     415        /**
     416         * Describes how to match the label against the key.
     417         * @see KeyMatchType
     418         */
    311419        public final KeyMatchType matchType;
    312         public Predicate<String> containsPattern;
     420        /**
     421         * A predicate used to match a the regexp against the key. Only used if the match type is regexp.
     422         */
     423        public final Predicate<String> containsPattern;
    313424
     425        /**
     426         * Creates a new KeyCondition
     427         * @param label The key name (or regexp) to use.
     428         * @param negateResult If we should negate the result.,
     429         * @param matchType The match type.
     430         */
    314431        public KeyCondition(String label, boolean negateResult, KeyMatchType matchType) {
    315432            this.label = label;
    316433            this.negateResult = negateResult;
    317             this.matchType = matchType;
     434            this.matchType = matchType == null ? KeyMatchType.EQ : matchType;
    318435            this.containsPattern = KeyMatchType.REGEX.equals(matchType)
    319436                    ? Predicates.stringContainsPattern(Pattern.compile(label))
    320437                    : null;
    public abstract class Condition {  
    324441        public boolean applies(Environment e) {
    325442            switch(e.getContext()) {
    326443            case PRIMITIVE:
    327                 if (KeyMatchType.TRUE.equals(matchType))
     444                switch (matchType) {
     445                case TRUE:
    328446                    return e.osm.isKeyTrue(label) ^ negateResult;
    329                 else if (KeyMatchType.FALSE.equals(matchType))
     447                case FALSE:
    330448                    return e.osm.isKeyFalse(label) ^ negateResult;
    331                 else if (KeyMatchType.REGEX.equals(matchType)) {
     449                case REGEX:
    332450                    return Utils.exists(e.osm.keySet(), containsPattern) ^ negateResult;
    333                 } else {
     451                default:
    334452                    return e.osm.hasKey(label) ^ negateResult;
    335453                }
    336454            case LINK:
    public abstract class Condition {  
    340458            }
    341459        }
    342460
     461        /**
     462         * Get the matched key and the corresponding value.
     463         * <p>
     464         * WARNING: This ignores {@link #negateResult}.
     465         * <p>
     466         * WARNING: For regexp, the regular expression is returned instead of a key if the match failed.
     467         * @param p The primitive to get the value from.
     468         * @return The tag.
     469         */
    343470        public Tag asTag(OsmPrimitive p) {
    344471            String key = label;
    345472            if (KeyMatchType.REGEX.equals(matchType)) {