Ticket #24482: autofilter_03_fractional.patch

File autofilter_03_fractional.patch, 6.7 KB (added by tordanik, 6 months ago)
  • src/org/openstreetmap/josm/gui/autofilter/AutoFilterManager.java

    diff --git a/src/org/openstreetmap/josm/gui/autofilter/AutoFilterManager.java b/src/org/openstreetmap/josm/gui/autofilter/AutoFilterManager.java
    index 44f503ed0d..5643cf12ca 100644
    a b import java.util.NavigableSet;  
    1414import java.util.Objects;
    1515import java.util.TreeMap;
    1616import java.util.TreeSet;
    17 import java.util.function.Consumer;
    1817
    1918import org.openstreetmap.josm.actions.mapmode.MapMode;
    2019import org.openstreetmap.josm.data.osm.BBox;
    implements ZoomChangeListener, MapModeChangeListener, DataSetListener, Preferenc  
    196195
    197196        @Override
    198197        public boolean match(OsmPrimitive osm) {
    199             return rule.getTagValuesForPrimitive(osm).anyMatch(v -> v == value);
     198            return rule.getTagValuesForPrimitive(osm, false).anyMatch(v -> v == value);
    200199        }
    201200
    202201        @Override
    implements ZoomChangeListener, MapModeChangeListener, DataSetListener, Preferenc  
    260259        }
    261260        BBox bbox = MainApplication.getMap().mapView.getState().getViewArea().getLatLonBoundsBox().toBBox();
    262261        NavigableSet<Integer> values = new TreeSet<>();
    263         Consumer<OsmPrimitive> consumer = o -> enabledRule.getTagValuesForPrimitive(o).forEach(values::add);
    264         ds.searchNodes(bbox).forEach(consumer);
    265         ds.searchWays(bbox).forEach(consumer);
    266         ds.searchRelations(bbox).forEach(consumer);
     262        for (var primitiveList : List.of(ds.searchNodes(bbox), ds.searchWays(bbox), ds.searchRelations(bbox))) {
     263            // add all values that are directly mentioned
     264            primitiveList.forEach(o -> enabledRule.getTagValuesForPrimitive(o, true).forEach(values::add));
     265            // only add integer values from value ranges, not fractional values
     266            primitiveList.forEach(o -> enabledRule.getTagValuesForPrimitive(o, false)
     267                    .filter(v -> !enabledRule.formatValue(v).contains("."))
     268                    .forEach(values::add));
     269
     270        }
    267271        return values;
    268272    }
    269273
  • src/org/openstreetmap/josm/gui/autofilter/AutoFilterRule.java

    diff --git a/src/org/openstreetmap/josm/gui/autofilter/AutoFilterRule.java b/src/org/openstreetmap/josm/gui/autofilter/AutoFilterRule.java
    index f722122bd6..e78c42d156 100644
    a b public class AutoFilterRule {  
    130130
    131131    /**
    132132     * Returns the numeric values for the given OSM primitive
    133      * @param osm the primitive
     133     *
     134     * @param osm              the primitive
     135     * @param directValuesOnly whether "inner" values from ranges (such as 6 and 7 for 5-8) should be omitted
    134136     * @return a stream of numeric values
    135137     */
    136     public IntStream getTagValuesForPrimitive(OsmPrimitive osm) {
     138    public IntStream getTagValuesForPrimitive(OsmPrimitive osm, boolean directValuesOnly) {
    137139        if (allKeys.size() == 1) {
    138             IntStream values = getTagValuesForPrimitive(osm, key);
     140            IntStream values = getTagValuesForPrimitive(osm, key, directValuesOnly);
    139141            if (values != null) return values;
    140142        } else {
    141143            Set<Integer> allValues = new HashSet<>();
    142144            for (String k : allKeys) {
    143                 IntStream values = getTagValuesForPrimitive(osm, k);
     145                IntStream values = getTagValuesForPrimitive(osm, k, directValuesOnly);
    144146                if (values != null) { values.forEach(allValues::add); }
    145147            }
    146148            if (!allValues.isEmpty()) return allValues.stream().mapToInt(it -> it).sorted();
    public class AutoFilterRule {  
    148150        return Boolean.TRUE.equals(PROP_AUTO_FILTER_DEFAULTS.get()) ? defaultValueSupplier.apply(osm) : IntStream.empty();
    149151    }
    150152
    151     private IntStream getTagValuesForPrimitive(OsmPrimitive osm, String key) {
     153    private IntStream getTagValuesForPrimitive(OsmPrimitive osm, String key, boolean directValuesOnly) {
    152154        String value = osm.get(key);
    153155        if (value != null) {
    154156            Pattern p = Pattern.compile("(-?[0-9]+)-(-?[0-9]+)");
    public class AutoFilterRule {  
    157159                if (m.matches()) {
    158160                    int a = valueExtractor.applyAsInt(m.group(1));
    159161                    int b = valueExtractor.applyAsInt(m.group(2));
    160                     return IntStream.rangeClosed(Math.min(a, b), Math.max(a, b));
     162                    if (directValuesOnly && a != b) {
     163                        return IntStream.of(a, b).sorted();
     164                    } else {
     165                        return IntStream.rangeClosed(Math.min(a, b), Math.max(a, b));
     166                    }
    161167                } else {
    162168                    try {
    163169                        return IntStream.of(valueExtractor.applyAsInt(v));
  • test/unit/org/openstreetmap/josm/gui/autofilter/AutoFilterRuleTest.java

    diff --git a/test/unit/org/openstreetmap/josm/gui/autofilter/AutoFilterRuleTest.java b/test/unit/org/openstreetmap/josm/gui/autofilter/AutoFilterRuleTest.java
    index ae1b15b909..3d1914dcb6 100644
    a b import org.junit.jupiter.api.Test;  
    2222@I18n
    2323class AutoFilterRuleTest {
    2424    /**
    25      * Unit test of {@link AutoFilterRule#getTagValuesForPrimitive(OsmPrimitive)}.
     25     * Unit test of {@link AutoFilterRule#getTagValuesForPrimitive(OsmPrimitive, boolean)}.
    2626     */
    2727    @Test
    2828    void testTagValuesForPrimitive() {
    class AutoFilterRuleTest {  
    4343    }
    4444
    4545    /**
    46      * Unit test of {@link AutoFilterRule#getTagValuesForPrimitive(OsmPrimitive)} to deal with {@code %} of key {@code incline}.
     46     * Unit test of {@link AutoFilterRule#getTagValuesForPrimitive(OsmPrimitive, boolean)} to deal with {@code %} of key {@code incline}.
    4747     */
    4848    @Test
    4949    void testTagValuesForPrimitiveInclineUnit() {
    class AutoFilterRuleTest {  
    5454    }
    5555
    5656    /**
    57      * Unit test of {@link AutoFilterRule#getTagValuesForPrimitive(OsmPrimitive)} provides sensible defaults, see #17496.
     57     * Unit test of {@link AutoFilterRule#getTagValuesForPrimitive(OsmPrimitive, boolean)} provides sensible defaults, see #17496.
    5858     */
    5959    @Test
    6060    void testTagValuesForPrimitivesDefaults() {
    class AutoFilterRuleTest {  
    7171
    7272    private void assertTagValuesForPrimitive(AutoFilterRule rule, String assertion, int... expected) {
    7373        final OsmPrimitive primitive = OsmUtils.createPrimitive(assertion);
    74         final int[] actual = rule.getTagValuesForPrimitive(primitive).toArray();
     74        final int[] actual = rule.getTagValuesForPrimitive(primitive, false).toArray();
    7575        assertArrayEquals(expected, actual);
    7676    }
    7777