Changeset 15764 in josm
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/search/SearchCompiler.java
r15440 r15764 19 19 import java.util.Optional; 20 20 import java.util.function.Predicate; 21 import java.util.function.Supplier; 21 22 import java.util.regex.Matcher; 22 23 import java.util.regex.Pattern; … … 264 265 265 266 /** 267 * Classes implementing this interface can provide Match instances themselves and do not rely on {@link #compile(String)}. 268 * 269 * @since 15764 270 */ 271 @FunctionalInterface 272 public interface MatchSupplier extends Supplier<Match> { 273 @Override 274 Match get(); 275 } 276 277 /** 266 278 * Base class for all search criteria. If the criterion only depends on an object's tags, 267 279 * inherit from {@link org.openstreetmap.josm.data.osm.search.SearchCompiler.TaggedMatch}. … … 1944 1956 */ 1945 1957 public static Match compile(SearchSetting setting) throws SearchParseError { 1958 if (setting instanceof MatchSupplier) { 1959 return ((MatchSupplier) setting).get(); 1960 } 1946 1961 if (setting.mapCSSSearch) { 1947 1962 return compileMapCSS(setting.text); -
trunk/src/org/openstreetmap/josm/gui/autofilter/AutoFilterManager.java
r14206 r15764 20 20 import java.util.regex.Matcher; 21 21 import java.util.regex.Pattern; 22 import java.util.stream.IntStream; 23 import java.util.stream.Stream; 22 24 23 25 import org.openstreetmap.josm.actions.mapmode.MapMode; … … 27 29 import org.openstreetmap.josm.data.osm.FilterModel; 28 30 import org.openstreetmap.josm.data.osm.OsmPrimitive; 31 import org.openstreetmap.josm.data.osm.OsmUtils; 29 32 import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent; 30 33 import org.openstreetmap.josm.data.osm.event.DataChangedEvent; … … 38 41 import org.openstreetmap.josm.data.osm.event.TagsChangedEvent; 39 42 import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent; 43 import org.openstreetmap.josm.data.osm.search.SearchCompiler; 44 import org.openstreetmap.josm.data.osm.search.SearchCompiler.MatchSupplier; 40 45 import org.openstreetmap.josm.data.preferences.BooleanProperty; 41 46 import org.openstreetmap.josm.data.preferences.StringProperty; … … 77 82 78 83 /** 84 * Property to determine if the auto filter should assume sensible defaults for values (such as layer=1 for bridge=yes). 85 */ 86 private static final BooleanProperty PROP_AUTO_FILTER_DEFAULTS = new BooleanProperty("auto.filter.defaults", true); 87 88 /** 79 89 * The unique instance. 80 90 */ … … 148 158 } 149 159 160 static class CompiledFilter extends Filter implements MatchSupplier { 161 final String key; 162 final String value; 163 164 CompiledFilter(String key, String value) { 165 this.key = key; 166 this.value = value; 167 this.enable = true; 168 this.inverted = true; 169 this.text = key + "=" + value; 170 } 171 172 @Override 173 public SearchCompiler.Match get() { 174 return new SearchCompiler.Match() { 175 @Override 176 public boolean match(OsmPrimitive osm) { 177 return getTagValuesForPrimitive(key, osm).anyMatch(value::equals); 178 } 179 }; 180 } 181 } 182 150 183 private synchronized void addNewButtons(NavigableSet<String> values) { 151 184 int i = 0; … … 153 186 MapView mapView = MainApplication.getMap().mapView; 154 187 for (final String value : values.descendingSet()) { 155 Filter filter = new Filter(); 156 filter.enable = true; 157 filter.inverted = true; 158 filter.text = enabledRule.getKey() + "=" + value; 188 Filter filter = new CompiledFilter(enabledRule.getKey(), value); 159 189 String label = enabledRule.getValueFormatter().apply(value); 160 190 AutoFilter autoFilter = new AutoFilter(label, filter.text, filter); … … 198 228 if (ds != null) { 199 229 BBox bbox = MainApplication.getMap().mapView.getState().getViewArea().getLatLonBoundsBox().toBBox(); 200 Consumer<OsmPrimitive> consumer = getTagValuesConsumer(key, values);230 Consumer<OsmPrimitive> consumer = o -> getTagValuesForPrimitive(key, o).forEach(values::add); 201 231 ds.searchNodes(bbox).forEach(consumer); 202 232 ds.searchWays(bbox).forEach(consumer); … … 206 236 } 207 237 208 static Consumer<OsmPrimitive> getTagValuesConsumer(String key, Set<String> values) { 209 return o -> { 210 String value = o.get(key); 211 if (value != null) { 212 Pattern p = Pattern.compile("(-?[0-9]+)-(-?[0-9]+)"); 213 for (String v : value.split(";")) { 214 Matcher m = p.matcher(v); 215 if (m.matches()) { 216 int a = Integer.parseInt(m.group(1)); 217 int b = Integer.parseInt(m.group(2)); 218 for (int i = Math.min(a, b); i <= Math.max(a, b); i++) { 219 values.add(Integer.toString(i)); 220 } 221 } else { 222 values.add(v); 223 } 238 static Stream<String> getTagValuesForPrimitive(String key, OsmPrimitive osm) { 239 String value = osm.get(key); 240 if (value != null) { 241 Pattern p = Pattern.compile("(-?[0-9]+)-(-?[0-9]+)"); 242 return OsmUtils.splitMultipleValues(value).flatMap(v -> { 243 Matcher m = p.matcher(v); 244 if (m.matches()) { 245 int a = Integer.parseInt(m.group(1)); 246 int b = Integer.parseInt(m.group(2)); 247 return IntStream.rangeClosed(Math.min(a, b), Math.max(a, b)) 248 .mapToObj(Integer::toString); 249 } else { 250 return Stream.of(v); 224 251 } 225 } 226 }; 252 }); 253 } else if (PROP_AUTO_FILTER_DEFAULTS.get() && "layer".equals(key)) { 254 // assume sensible defaults, see #17496 255 if (osm.hasTag("bridge") || osm.hasTag("power", "line") || osm.hasTag("location", "overhead")) { 256 return Stream.of("1"); 257 } else if (osm.isKeyTrue("tunnel") || osm.hasTag("tunnel", "culvert") || osm.hasTag("location", "underground")) { 258 return Stream.of("-1"); 259 } else if (osm.hasTag("tunnel", "building_passage") || osm.hasKey("highway", "railway", "waterway")) { 260 return Stream.of("0"); 261 } 262 } 263 return Stream.empty(); 227 264 } 228 265 -
trunk/test/unit/org/openstreetmap/josm/gui/autofilter/AutoFilterManagerTest.java
r12407 r15764 3 3 4 4 import static org.junit.Assert.assertEquals; 5 import static org.junit.Assert.assertNull; 5 6 6 7 import java.util.Arrays; 7 import java.util.Set;8 8 import java.util.TreeSet; 9 import java.util.function.Consumer; 9 import java.util.stream.Collectors; 10 import java.util.stream.Stream; 10 11 11 12 import org.junit.Rule; 12 13 import org.junit.Test; 13 import org.openstreetmap.josm.data.osm.OsmPrimitive;14 14 import org.openstreetmap.josm.data.osm.OsmUtils; 15 15 import org.openstreetmap.josm.testutils.JOSMTestRules; … … 30 30 31 31 /** 32 * Unit test of {@link AutoFilterManager#getTagValues Consumer}.32 * Unit test of {@link AutoFilterManager#getTagValuesForPrimitive}. 33 33 */ 34 34 @Test 35 public void testTagValuesConsumer() { 36 Set<String> values = new TreeSet<>(); 37 Consumer<OsmPrimitive> consumer = AutoFilterManager.getTagValuesConsumer("level", values); 38 Arrays.asList( 35 public void testTagValuesForPrimitive() { 36 final TreeSet<String> values = Stream.of( 39 37 OsmUtils.createPrimitive("way level=-4--5"), 40 38 OsmUtils.createPrimitive("way level=-2"), … … 43 41 OsmUtils.createPrimitive("way level=2;3"), 44 42 OsmUtils.createPrimitive("way level=6-9"), 45 OsmUtils.createPrimitive("way level=10;12-13") 46 ).forEach(consumer); 43 OsmUtils.createPrimitive("way level=10;12-13")) 44 .flatMap(o -> AutoFilterManager.getTagValuesForPrimitive("level", o)) 45 .collect(Collectors.toCollection(TreeSet::new)); 47 46 assertEquals(new TreeSet<>(Arrays.asList("-5", "-4", "-2", "0", "1", "2", "3", "6", "7", "8", "9", "10", "12", "13")), values); 47 48 } 49 50 /** 51 * Unit test of {@link AutoFilterManager#getTagValuesForPrimitive} provides sensible defaults, see #17496. 52 */ 53 @Test 54 public void testTagValuesForPrimitivesDefaults() { 55 assertNull(getLayer("way foo=bar")); 56 assertEquals("1", getLayer("way bridge=yes")); 57 assertEquals("1", getLayer("way power=line")); 58 assertEquals("-1", getLayer("way tunnel=yes")); 59 assertEquals("0", getLayer("way tunnel=building_passage")); 60 assertEquals("0", getLayer("way highway=residential")); 61 assertEquals("0", getLayer("way railway=rail")); 62 assertEquals("0", getLayer("way waterway=canal")); 63 } 64 65 private String getLayer(final String assertion) { 66 return AutoFilterManager.getTagValuesForPrimitive("layer", OsmUtils.createPrimitive(assertion)) 67 .findFirst() 68 .orElse(null); 48 69 } 49 70 }
Note:
See TracChangeset
for help on using the changeset viewer.