Changeset 16086 in josm for trunk/src/org


Ignore:
Timestamp:
2020-03-08T19:21:11+01:00 (5 years ago)
Author:
simon04
Message:

see #18140 - Get rid of OpeningHoursParser.OpeningHoursTestError

Location:
trunk/src/org/openstreetmap/josm/data/validation/tests
Files:
2 edited

Legend:

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

    r15978 r16086  
    99import java.util.HashSet;
    1010import java.util.List;
    11 import java.util.Locale;
    1211import java.util.Set;
    1312import java.util.regex.Matcher;
     
    204203                for (final String condition : conditional.conditions) {
    205204                    if (condition.matches(".*[0-9]:[0-9]{2}.*")) {
    206                         final List<OpeningHourTest.OpeningHoursTestError> errors = openingHourTest.checkOpeningHourSyntax(
    207                                 "", condition, true, Locale.getDefault());
     205                        final List<TestError> errors = openingHourTest.checkOpeningHourSyntax("", condition);
    208206                        if (!errors.isEmpty()) {
    209                             return errors.get(0).getMessage();
     207                            return errors.get(0).getDescription();
    210208                        }
    211209                    }
  • trunk/src/org/openstreetmap/josm/data/validation/tests/OpeningHourTest.java

    r16085 r16086  
    55
    66import java.io.StringReader;
     7import java.util.Arrays;
     8import java.util.Collection;
    79import java.util.Collections;
    810import java.util.List;
     
    2931public class OpeningHourTest extends TagTest {
    3032
     33    private static final Collection<String> KEYS_TO_CHECK = Arrays.asList("opening_hours", "collection_times", "service_times");
     34
    3135    /**
    3236     * Constructs a new {@code OpeningHourTest}.
     
    3842
    3943    /**
    40      * An error concerning invalid syntax for an "opening_hours"-like tag.
     44     * Returns the real test error given to JOSM validator.
     45     * @param severity The error severity
     46     * @param message The error message
     47     * @param key The incriminated key, used for display.
     48     * @param prettifiedValue The prettified value
     49     * @param p The incriminated OSM primitive.
     50     * @return The real test error given to JOSM validator. Can be fixable or not if a prettified values has been determined.
    4151     */
    42     public class OpeningHoursTestError {
    43         private final Severity severity;
    44         private final String message;
    45         private final String prettifiedValue;
    46 
    47         /**
    48          * Constructs a new {@code OpeningHoursTestError} with a known prettified value.
    49          * @param message The error message
    50          * @param severity The error severity
    51          * @param prettifiedValue The prettified value
    52          */
    53         public OpeningHoursTestError(String message, Severity severity, String prettifiedValue) {
    54             this.message = message;
    55             this.severity = severity;
    56             this.prettifiedValue = prettifiedValue;
    57         }
    58 
    59         /**
    60          * Returns the real test error given to JOSM validator.
    61          * @param p The incriminated OSM primitive.
    62          * @param key The incriminated key, used for display.
    63          * @return The real test error given to JOSM validator. Can be fixable or not if a prettified values has been determined.
    64          */
    65         public TestError getTestError(final OsmPrimitive p, final String key) {
    66             final TestError.Builder error = TestError.builder(OpeningHourTest.this, severity, 2901)
    67                     .message(tr("Opening hours syntax"), message) // todo obtain English message for ignore functionality
    68                     .primitives(p);
    69             if (prettifiedValue == null || prettifiedValue.equals(p.get(key))) {
    70                 return error.build();
    71             } else {
    72                 return error.fix(() -> new ChangePropertyCommand(p, key, prettifiedValue)).build();
    73             }
    74         }
    75 
    76         /**
    77          * Returns the error message.
    78          * @return The error message.
    79          */
    80         public String getMessage() {
    81             return message;
    82         }
    83 
    84         /**
    85          * Returns the prettified value.
    86          * @return The prettified value.
    87          */
    88         public String getPrettifiedValue() {
    89             return prettifiedValue;
    90         }
    91 
    92         /**
    93          * Returns the error severity.
    94          * @return The error severity.
    95          */
    96         public Severity getSeverity() {
    97             return severity;
    98         }
    99 
    100         @Override
    101         public String toString() {
    102             return getMessage() + " => " + getPrettifiedValue();
     52    private TestError createTestError(Severity severity, String message, String key, String prettifiedValue, OsmPrimitive p) {
     53        final TestError.Builder error = TestError.builder(this, severity, 2901)
     54                .message(tr("Opening hours syntax"), message) // todo obtain English message for ignore functionality
     55                .primitives(p);
     56        if (prettifiedValue == null || prettifiedValue.equals(p.get(key))) {
     57            return error.build();
     58        } else {
     59            return error.fix(() -> new ChangePropertyCommand(p, key, prettifiedValue)).build();
    10360        }
    10461    }
     
    11168     * @return a list of {@link TestError} or an empty list
    11269     */
    113     public List<OpeningHoursTestError> checkOpeningHourSyntax(final String key, final String value) {
    114         return checkOpeningHourSyntax(key, value, false, Locale.getDefault());
     70    public List<TestError> checkOpeningHourSyntax(final String key, final String value) {
     71        return checkOpeningHourSyntax(key, value, null, Locale.getDefault());
    11572    }
    11673
     
    12077     * @param key the OSM key (should be "opening_hours", "collection_times" or "service_times").
    12178     * @param value the opening hour value to be checked.
    122      * @param ignoreOtherSeverity whether to ignore errors with {@link Severity#OTHER}.
     79     * @param p the primitive to check/fix.
    12380     * @param locale the locale code used for localizing messages
    12481     * @return a list of {@link TestError} or an empty list
    12582     */
    126     public List<OpeningHoursTestError> checkOpeningHourSyntax(final String key, final String value, boolean ignoreOtherSeverity, Locale locale) {
     83    List<TestError> checkOpeningHourSyntax(final String key, final String value, OsmPrimitive p, Locale locale) {
    12784        if (value == null || value.isEmpty()) {
    12885            return Collections.emptyList();
     
    13996            }
    14097        } catch (ParseException e) {
    141             return Collections.singletonList(new OpeningHoursTestError(e.getMessage(), Severity.WARNING, prettifiedValue));
     98            return Collections.singletonList(createTestError(Severity.WARNING, e.getMessage(), key, prettifiedValue, p));
    14299        }
    143100
    144         if (ignoreOtherSeverity || Objects.equals(value, prettifiedValue)) {
     101        if (!includeOtherSeverityChecks() || Objects.equals(value, prettifiedValue) || p == null) {
    145102            return Collections.emptyList();
    146103        } else {
    147             return Collections.singletonList(
    148                     new OpeningHoursTestError(tr("{0} value can be prettified", key), Severity.OTHER, prettifiedValue));
    149         }
    150     }
    151 
    152     protected void check(final OsmPrimitive p, final String key) {
    153         for (OpeningHoursTestError e : checkOpeningHourSyntax(key, p.get(key))) {
    154             errors.add(e.getTestError(p, key));
     104            final String message = tr("{0} value can be prettified", key);
     105            return Collections.singletonList(createTestError(Severity.OTHER, message, key, prettifiedValue, p));
    155106        }
    156107    }
     
    158109    @Override
    159110    public void check(final OsmPrimitive p) {
    160         check(p, "opening_hours");
    161         check(p, "collection_times");
    162         check(p, "service_times");
     111        for (String key : KEYS_TO_CHECK) {
     112            errors.addAll(checkOpeningHourSyntax(key, p.get(key), p, Locale.getDefault()));
     113        }
    163114    }
    164115}
Note: See TracChangeset for help on using the changeset viewer.