Changeset 7275 in josm for trunk


Ignore:
Timestamp:
2014-06-29T17:35:10+02:00 (10 years ago)
Author:
Don-vip
Message:

see #9518 - refactor internals of MapCSSTagChecker to prepare later work, improve javadoc

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java

    r7248 r7275  
    1818import java.util.List;
    1919import java.util.Map;
     20import java.util.Set;
    2021import java.util.regex.Matcher;
    2122import java.util.regex.Pattern;
     
    4950import org.openstreetmap.josm.io.UTFInputStreamReader;
    5051import org.openstreetmap.josm.tools.CheckParameterUtil;
     52import org.openstreetmap.josm.tools.MultiMap;
    5153import org.openstreetmap.josm.tools.Predicate;
    5254import org.openstreetmap.josm.tools.Utils;
     
    5860public class MapCSSTagChecker extends Test.TagTest {
    5961
     62    /**
     63     * A grouped MapCSSRule with multiple selectors for a single declaration.
     64     * @see MapCSSRule
     65     */
    6066    public static class GroupedMapCSSRule {
     67        /** MapCSS selectors **/
    6168        final public List<Selector> selectors;
     69        /** MapCSS declaration **/
    6270        final public Declaration declaration;
    6371
     72        /**
     73         * Constructs a new {@code GroupedMapCSSRule}.
     74         * @param selectors MapCSS selectors
     75         * @param declaration MapCSS declaration
     76         */
    6477        public GroupedMapCSSRule(List<Selector> selectors, Declaration declaration) {
    6578            this.selectors = selectors;
     
    112125    }
    113126
    114     final List<TagCheck> checks = new ArrayList<>();
     127    final MultiMap<String, TagCheck> checks = new MultiMap<>();
    115128
    116129    static class TagCheck implements Predicate<OsmPrimitive> {
     
    204217                        check.change.add(toTag);
    205218                    } else if ("fixRemove".equals(ai.key)) {
    206                         CheckParameterUtil.ensureThat(!(ai.val instanceof String) || !val.contains("="), "Unexpected '='. Please only specify the key to remove!");
     219                        CheckParameterUtil.ensureThat(!(ai.val instanceof String) || !(val != null && val.contains("=")),
     220                                "Unexpected '='. Please only specify the key to remove!");
    207221                        final PrimitiveToTag toTag = PrimitiveToTag.ofMapCSSObject(ai.val, true);
    208222                        check.change.add(toTag);
     
    323337
    324338        /**
    325          * Replaces occurrences of {@code {i.key}}, {@code {i.value}}, {@code {i.tag}} in {@code s} by the corresponding
     339         * Replaces occurrences of <code>{i.key}</code>, <code>{i.value}</code>, <code>{i.tag}</code> in {@code s} by the corresponding
    326340         * key/value/tag of the {@code index}-th {@link Condition} of {@code matchingSelector}.
    327341         */
     
    484498    /**
    485499     * Obtains all {@link TestError}s for the {@link OsmPrimitive} {@code p}.
     500     * @param p The OSM primitive
     501     * @param includeOtherSeverity if {@code true}, errors of severity {@link Severity#OTHER} (info) will also be returned
     502     * @return all errors for the given primitive, with or without those of "info" severity
    486503     */
    487504    public Collection<TestError> getErrorsForPrimitive(OsmPrimitive p, boolean includeOtherSeverity) {
    488505        final ArrayList<TestError> r = new ArrayList<>();
    489506        final Environment env = new Environment(p, new MultiCascade(), Environment.DEFAULT_LAYER, null);
    490         for (TagCheck check : checks) {
    491             if (Severity.OTHER.equals(check.getSeverity()) && !includeOtherSeverity) {
    492                 continue;
    493             }
    494             final Selector selector = check.whichSelectorMatchesEnvironment(env);
    495             if (selector != null) {
    496                 check.rule.declaration.execute(env);
    497                 final TestError error = check.getErrorForPrimitive(p, selector, env);
    498                 if (error != null) {
    499                     error.setTester(new MapCSSTagCheckerAndRule(check.rule));
    500                     r.add(error);
     507        for (Set<TagCheck> schecks : checks.values()) {
     508            for (TagCheck check : schecks) {
     509                if (Severity.OTHER.equals(check.getSeverity()) && !includeOtherSeverity) {
     510                    continue;
     511                }
     512                final Selector selector = check.whichSelectorMatchesEnvironment(env);
     513                if (selector != null) {
     514                    check.rule.declaration.execute(env);
     515                    final TestError error = check.getErrorForPrimitive(p, selector, env);
     516                    if (error != null) {
     517                        error.setTester(new MapCSSTagCheckerAndRule(check.rule));
     518                        r.add(error);
     519                    }
    501520                }
    502521            }
     
    516535
    517536    /**
    518      * Adds a new MapCSS config file from the given {@code Reader}.
    519      * @param css The reader
     537     * Adds a new MapCSS config file from the given URL.
     538     * @param url The unique URL of the MapCSS config file
    520539     * @throws ParseException if the config file does not match MapCSS syntax
     540     * @throws IOException if any I/O error occurs
     541     * @since
    521542     */
    522     public void addMapCSS(Reader css) throws ParseException {
    523         checks.addAll(TagCheck.readMapCSS(css));
     543    public void addMapCSS(String url) throws ParseException, IOException {
     544        CheckParameterUtil.ensureParameterNotNull(url, "url");
     545        try (InputStream s = new CachedFile(url).getInputStream()) {
     546            checks.putAll(url, TagCheck.readMapCSS(new BufferedReader(UTFInputStreamReader.create(s))));
     547        }
    524548    }
    525549
     
    534558                    Main.info(tr("Adding {0} to tag checker", i));
    535559                }
    536                 try (InputStream s = new CachedFile(i).getInputStream()) {
    537                     addMapCSS(new BufferedReader(UTFInputStreamReader.create(s)));
    538                 }
     560                addMapCSS(i);
    539561            } catch (IOException ex) {
    540562                Main.warn(tr("Failed to add {0} to tag checker", i));
  • trunk/src/org/openstreetmap/josm/tools/MultiMap.java

    r7005 r7275  
    1212
    1313/**
    14  * MultiMap - maps keys to multiple values
     14 * MultiMap - maps keys to multiple values.
    1515 *
    1616 * Corresponds to Google guava LinkedHashMultimap and Apache Collections MultiValueMap
    1717 * but it is an independent (simple) implementation.
    1818 *
     19 * @param <A> Key type
     20 * @param <B> Value type
     21 *
     22 * @since 2702
    1923 */
    2024public class MultiMap<A, B> {
     
    3034
    3135    /**
    32      * Constructs a new {@code MultiMap} with the specified initial capacity. 
     36     * Constructs a new {@code MultiMap} with the specified initial capacity.
    3337     * @param capacity the initial capacity
    3438     */
     
    4145     *
    4246     * Can be called multiple times with the same key, but different value.
     47     * @param key key with which the specified value is to be associated
     48     * @param value value to be associated with the specified key
    4349     */
    4450    public void put(A key, B value) {
     
    5662     * Afterwards containsKey(key) will return true and get(key) will return
    5763     * an empty Set instead of null.
     64     * @param key key with which an empty set is to be associated
    5865     */
    5966    public void putVoid(A key) {
     
    6774     *
    6875     * Adds to the mappings that are already there.
     76     * @param key key with which the specified values are to be associated
     77     * @param values values to be associated with the specified key
    6978     */
    7079    public void putAll(A key, Collection<B> values) {
     
    7988    /**
    8089     * Get the keySet.
     90     * @return a set view of the keys contained in this map
     91     * @see Map#keySet()
    8192     */
    8293    public Set<A> keySet() {
     
    90101     * Modifications of the returned list changes the underling map,
    91102     * but you should better not do that.
     103     * @param key the key whose associated value is to be returned
     104     * @return the set of values to which the specified key is mapped, or {@code null} if this map contains no mapping for the key
     105     * @see Map#get(Object)
    92106     */
    93107    public Set<B> get(A key) {
     
    97111    /**
    98112     * Like get, but returns an empty Set if nothing has been mapped to the key.
     113     * @param key the key whose associated value is to be returned
     114     * @return the set of values to which the specified key is mapped, or an empty set if this map contains no mapping for the key
    99115     */
    100116    public Set<B> getValues(A key) {
     
    104120    }
    105121
     122    /**
     123     * Returns {@code true} if this map contains no key-value mappings.
     124     * @return {@code true} if this map contains no key-value mappings
     125     * @see Map#isEmpty()
     126     */
    106127    public boolean isEmpty() {
    107128        return map.isEmpty();
    108129    }
    109130
     131    /**
     132     * Returns {@code true} if this map contains a mapping for the specified key.
     133     * @param key key whose presence in this map is to be tested
     134     * @return {@code true} if this map contains a mapping for the specified key
     135     * @see Map#containsKey(Object)
     136     */
    110137    public boolean containsKey(A key) {
    111138        return map.containsKey(key);
     
    124151    }
    125152
     153    /**
     154     * Removes all of the mappings from this map. The map will be empty after this call returns.
     155     * @see Map#clear()
     156     */
    126157    public void clear() {
    127158        map.clear();
    128159    }
    129160
     161    /**
     162     * Returns a Set view of the mappings contained in this map.
     163     * The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.
     164     * @return a set view of the mappings contained in this map
     165     * @see Map#entrySet()
     166     */
    130167    public Set<Entry<A, Set<B>>> entrySet() {
    131168        return map.entrySet();
     
    134171    /**
    135172     * Returns the number of keys.
     173     * @return the number of key-value mappings in this map
     174     * @see Map#size()
    136175     */
    137176    public int size() {
     
    141180    /**
    142181     * Returns a collection of all value sets.
     182     * @return a collection view of the values contained in this map
     183     * @see Map#values()
    143184     */
    144185    public Collection<Set<B>> values() {
     
    147188
    148189    /**
    149      * Removes a cerain key=value mapping.
    150      *
    151      * @return true, if something was removed
     190     * Removes a certain key=value mapping.
     191     * @param key key whose mapping is to be removed from the map
     192     * @param value value whose mapping is to be removed from the map
     193     *
     194     * @return {@code true}, if something was removed
    152195     */
    153196    public boolean remove(A key, B value) {
     
    161204    /**
    162205     * Removes all mappings for a certain key.
     206     * @param key key whose mapping is to be removed from the map
     207     * @return the previous value associated with key, or {@code null} if there was no mapping for key.
     208     * @see Map#remove(Object)
    163209     */
    164210    public Set<B> remove(A key) {
  • trunk/test/unit/org/openstreetmap/josm/data/validation/tests/MapCSSTagCheckerTest.java

    r7081 r7275  
    1212import java.util.List;
    1313import java.util.Map;
     14import java.util.Set;
    1415
    1516import org.junit.BeforeClass;
     
    2324import org.openstreetmap.josm.data.validation.Severity;
    2425import org.openstreetmap.josm.data.validation.TestError;
     26import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.TagCheck;
    2527import org.openstreetmap.josm.tools.Predicate;
    2628import org.openstreetmap.josm.tools.Utils;
     
    7476
    7577        LinkedHashSet<String> assertionErrors = new LinkedHashSet<>();
    76         for (final MapCSSTagChecker.TagCheck check : c.checks) {
    77             System.out.println("Check: "+check);
    78             for (final Map.Entry<String, Boolean> i : check.assertions.entrySet()) {
    79                 System.out.println("- Assertion: "+i);
    80                 final OsmPrimitive p = TestUtils.createPrimitive(i.getKey());
    81                 final boolean isError = Utils.exists(c.getErrorsForPrimitive(p, true), new Predicate<TestError>() {
    82                     @Override
    83                     public boolean evaluate(TestError e) {
    84                         //noinspection EqualsBetweenInconvertibleTypes
    85                         return e.getTester().equals(check.rule);
     78        for (final Set<TagCheck> schecks : c.checks.values()) {
     79            for (final TagCheck check : schecks) {
     80                System.out.println("Check: "+check);
     81                for (final Map.Entry<String, Boolean> i : check.assertions.entrySet()) {
     82                    System.out.println("- Assertion: "+i);
     83                    final OsmPrimitive p = TestUtils.createPrimitive(i.getKey());
     84                    final boolean isError = Utils.exists(c.getErrorsForPrimitive(p, true), new Predicate<TestError>() {
     85                        @Override
     86                        public boolean evaluate(TestError e) {
     87                            //noinspection EqualsBetweenInconvertibleTypes
     88                            return e.getTester().equals(check.rule);
     89                        }
     90                    });
     91                    if (isError != i.getValue()) {
     92                        final String error = MessageFormat.format("Expecting test ''{0}'' (i.e., {1}) to {2} {3} (i.e., {4})",
     93                                check.getMessage(p), check.rule.selectors, i.getValue() ? "match" : "not match", i.getKey(), p.getKeys());
     94                        System.err.println(error);
     95                        assertionErrors.add(error);
    8696                    }
    87                 });
    88                 if (isError != i.getValue()) {
    89                     final String error = MessageFormat.format("Expecting test ''{0}'' (i.e., {1}) to {2} {3} (i.e., {4})",
    90                             check.getMessage(p), check.rule.selectors, i.getValue() ? "match" : "not match", i.getKey(), p.getKeys());
    91                     System.err.println(error);
    92                     assertionErrors.add(error);
    9397                }
    9498            }
Note: See TracChangeset for help on using the changeset viewer.