Changeset 8250 in josm for trunk


Ignore:
Timestamp:
2015-04-23T18:59:15+02:00 (4 years ago)
Author:
simon04
Message:

fix #11358 - Search: allow negative indices for nth: to search backward

Location:
trunk
Files:
3 edited

Legend:

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

    r8220 r8250  
    397397                .addKeyword("child <i>expr</i>", "child ", tr("all children of objects matching the expression"), "child building")
    398398                .addKeyword("parent <i>expr</i>", "parent ", tr("all parents of objects matching the expression"), "parent bus_stop")
    399                 .addKeyword("nth:<i>7</i>", "nth: ", tr("n-th member of relation and/or n-th node of way"), "nth:5 (child type:relation)")
     399                .addKeyword("nth:<i>7</i>", "nth: ", tr("n-th member of relation and/or n-th node of way"), "nth:5 (child type:relation)", "nth:-1")
    400400                .addKeyword("nth%:<i>7</i>", "nth%: ", tr("every n-th member of relation and/or every n-th node of way"), "nth%:100 (child waterway)")
    401401                , GBC.eol());
  • trunk/src/org/openstreetmap/josm/actions/search/SearchCompiler.java

    r8231 r8250  
    846846
    847847        private Nth(int nth, boolean modulo) throws ParseError {
    848             if (nth <= 0) {
    849                 throw new ParseError(tr("Positive integer expected"));
    850             }
    851848            this.nth = nth;
    852849            this.modulo = modulo;
     
    856853        public boolean match(OsmPrimitive osm) {
    857854            for (OsmPrimitive p : osm.getReferrers()) {
    858                 Integer idx = null;
     855                final int idx;
     856                final int maxIndex;
    859857                if (p instanceof Way) {
    860858                    Way w = (Way) p;
    861859                    idx = w.getNodes().indexOf(osm);
     860                    maxIndex = w.getNodesCount();
    862861                } else if (p instanceof Relation) {
    863862                    Relation r = (Relation) p;
    864863                    idx = r.getMemberPrimitivesList().indexOf(osm);
    865                 }
    866                 if (idx != null) {
    867                     if (idx.intValue() == nth || (modulo && idx.intValue() % nth == 0)) {
    868                         return true;
    869                     }
    870                 }
     864                    maxIndex = r.getMembersCount();
     865                } else {
     866                    continue;
     867                }
     868                if (nth < 0 && idx - maxIndex == nth) {
     869                    return true;
     870                } else if (idx == nth || (modulo && idx % nth == 0))
     871                    return true;
    871872            }
    872873            return false;
     874        }
     875
     876        @Override
     877        public String toString() {
     878            return "Nth{nth=" + nth + ", modulo=" + modulo + '}';
    873879        }
    874880    }
  • trunk/test/unit/org/openstreetmap/josm/actions/search/SearchCompilerTest.java

    r7937 r8250  
    11package org.openstreetmap.josm.actions.search;
    22
     3import org.hamcrest.CoreMatchers;
    34import org.junit.Assert;
    45import org.junit.Before;
    56import org.junit.Test;
    67import org.openstreetmap.josm.JOSMFixture;
     8import org.openstreetmap.josm.data.coor.LatLon;
     9import org.openstreetmap.josm.data.osm.DataSet;
    710import org.openstreetmap.josm.data.osm.Node;
    811import org.openstreetmap.josm.data.osm.OsmPrimitive;
     12import org.openstreetmap.josm.data.osm.Way;
    913
    1014public class SearchCompilerTest {
     
    6367
    6468    }
     69
     70    @Test
     71    public void testNth() throws Exception {
     72        final DataSet dataSet = new DataSet();
     73        final Way way = new Way();
     74        final Node node0 = new Node(new LatLon(1, 1));
     75        final Node node1 = new Node(new LatLon(2, 2));
     76        final Node node2 = new Node(new LatLon(3, 3));
     77        dataSet.addPrimitive(way);
     78        dataSet.addPrimitive(node0);
     79        dataSet.addPrimitive(node1);
     80        dataSet.addPrimitive(node2);
     81        way.addNode(node0);
     82        way.addNode(node1);
     83        way.addNode(node2);
     84        Assert.assertFalse(SearchCompiler.compile("nth:2", false, false).match(node1));
     85        Assert.assertTrue(SearchCompiler.compile("nth:1", false, false).match(node1));
     86        Assert.assertFalse(SearchCompiler.compile("nth:0", false, false).match(node1));
     87        Assert.assertTrue(SearchCompiler.compile("nth:0", false, false).match(node0));
     88        Assert.assertTrue(SearchCompiler.compile("nth:2", false, false).match(node2));
     89        Assert.assertTrue(SearchCompiler.compile("nth:-1", false, false).match(node2));
     90        Assert.assertTrue(SearchCompiler.compile("nth:-2", false, false).match(node1));
     91        Assert.assertTrue(SearchCompiler.compile("nth:-3", false, false).match(node0));
     92    }
     93
     94    @Test
     95    public void testNthParseNegative() throws Exception {
     96        Assert.assertThat(SearchCompiler.compile("nth:-1", false, false).toString(), CoreMatchers.is("Nth{nth=-1, modulo=false}"));
     97
     98    }
    6599}
Note: See TracChangeset for help on using the changeset viewer.