Changeset 6429 in josm for trunk/src/org


Ignore:
Timestamp:
2013-11-30T23:37:57+01:00 (11 years ago)
Author:
simon04
Message:

fix #8850 - search: support less/greater-than, e.g., start_date>1950

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/search/PushbackTokenizer.java

    r6380 r6429  
    5959        RIGHT_PARENT(marktr("<right parent>")), COLON(marktr("<colon>")), EQUALS(marktr("<equals>")),
    6060        KEY(marktr("<key>")), QUESTION_MARK(marktr("<question mark>")),
    61         EOF(marktr("<end-of-file>"));
     61        EOF(marktr("<end-of-file>")), LESS_THAN("<less-than>"), GREATER_THAN("<greater-than>");
    6262
    6363        private Token(String name) {
     
    8282    }
    8383
    84     private static final List<Character> specialChars = Arrays.asList(new Character[] {'"', ':', '(', ')', '|', '^', '=', '?'});
    85     private static final List<Character> specialCharsQuoted = Arrays.asList(new Character[] {'"'});
     84    private static final List<Character> specialChars = Arrays.asList('"', ':', '(', ')', '|', '^', '=', '?', '<', '>');
     85    private static final List<Character> specialCharsQuoted = Arrays.asList('"');
    8686
    8787    private String getString(boolean quoted) {
     
    134134            getChar();
    135135            return Token.EQUALS;
     136        case '<':
     137            getChar();
     138            return Token.LESS_THAN;
     139        case '>':
     140            getChar();
     141            return Token.GREATER_THAN;
    136142        case '(':
    137143            getChar();
  • trunk/src/org/openstreetmap/josm/actions/search/SearchAction.java

    r6380 r6429  
    395395                .addKeyword("*=<i>value</i>", null, tr("''value'' in any key"))
    396396                .addKeyword("<i>key</i>=", null, tr("matches if ''key'' exists"))
     397                .addKeyword("<i>key</i>><i>value</i>", null, tr("matches if ''key'' is greater than ''value'' (analogously, less than)"))
    397398                , GBC.eol());
    398399        right.add(new SearchKeywordRow(hcbSearchString)
  • trunk/src/org/openstreetmap/josm/actions/search/SearchCompiler.java

    r6380 r6429  
    2828import org.openstreetmap.josm.tools.DateUtils;
    2929import org.openstreetmap.josm.tools.Geometry;
     30import org.openstreetmap.josm.tools.Predicate;
    3031
    3132/**
     
    207208     * Base class for all search operators.
    208209     */
    209     abstract public static class Match {
     210    abstract public static class Match implements Predicate<OsmPrimitive> {
    210211
    211212        abstract public boolean match(OsmPrimitive osm);
     
    231232            }
    232233            return true;
     234        }
     235
     236        @Override
     237        public final boolean evaluate(OsmPrimitive object) {
     238            return match(object);
    233239        }
    234240    }
     
    515521        }
    516522        @Override public String toString() {return key+"="+value;}
     523    }
     524
     525    public static class ValueComparison extends Match {
     526        private final String key;
     527        private final String referenceValue;
     528        private final int compareMode;
     529
     530        public ValueComparison(String key, String referenceValue, int compareMode) {
     531            this.key = key;
     532            this.referenceValue = referenceValue;
     533            this.compareMode = compareMode;
     534        }
     535
     536        @Override
     537        public boolean match(OsmPrimitive osm) {
     538            int compareResult;
     539            try {
     540                compareResult = Double.compare(
     541                        Double.parseDouble(osm.get(key)),
     542                        Double.parseDouble(referenceValue)
     543                );
     544            } catch (Exception ignore) {
     545                compareResult = osm.get(key).compareTo(referenceValue);
     546            }
     547            return compareMode < 0 ? compareResult < 0 : compareMode > 0 ? compareResult > 0 : compareResult == 0;
     548        }
    517549    }
    518550
     
    12311263            // factor consists of key:value or key=value
    12321264            String key = tokenizer.getText();
    1233             if (tokenizer.readIfEqual(Token.EQUALS))
     1265            if (tokenizer.readIfEqual(Token.EQUALS)) {
    12341266                return new ExactKeyValue(regexSearch, key, tokenizer.readTextOrNumber());
    1235             else if (tokenizer.readIfEqual(Token.COLON)) {
     1267            } else if (tokenizer.readIfEqual(Token.LESS_THAN)) {
     1268                return new ValueComparison(key, tokenizer.readTextOrNumber(), -1);
     1269            } else if (tokenizer.readIfEqual(Token.GREATER_THAN)) {
     1270                return new ValueComparison(key, tokenizer.readTextOrNumber(), +1);
     1271            } else if (tokenizer.readIfEqual(Token.COLON)) {
    12361272                // see if we have a Match that takes a data parameter
    12371273                SimpleMatchFactory factory = simpleMatchFactoryMap.get(key);
Note: See TracChangeset for help on using the changeset viewer.