Changeset 9385 in josm


Ignore:
Timestamp:
2016-01-10T13:48:42+01:00 (9 years ago)
Author:
simon04
Message:

Refactoring: introduce UncheckedParseException

In addition, DateUtils#fromString does no longer return "now" when
date cannot be parsed, but throws an UncheckedParseException instead.

Location:
trunk
Files:
1 added
5 edited

Legend:

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

    r9242 r9385  
    5050import org.openstreetmap.josm.tools.OverpassTurboQueryWizard;
    5151import org.openstreetmap.josm.tools.Shortcut;
     52import org.openstreetmap.josm.tools.UncheckedParseException;
    5253import org.openstreetmap.josm.tools.Utils;
    5354
     
    157158                    try {
    158159                        overpassQuery.setText(OverpassTurboQueryWizard.getInstance().constructQuery(overpassWizardText));
    159                     } catch (OverpassTurboQueryWizard.ParseException ex) {
     160                    } catch (UncheckedParseException ex) {
    160161                        HelpAwareOptionPane.showOptionDialog(
    161162                                Main.parent,
  • trunk/src/org/openstreetmap/josm/tools/OverpassTurboQueryWizard.java

    r8855 r9385  
    2424    private static OverpassTurboQueryWizard instance;
    2525    private final ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
    26 
    27     /**
    28      * An exception to indicate a failed parse.
    29      */
    30     public static class ParseException extends RuntimeException {
    31     }
    3226
    3327    /**
     
    6054     * @param search the {@link org.openstreetmap.josm.actions.search.SearchAction} like query
    6155     * @return an Overpass QL query
    62      * @throws ParseException when the parsing fails
     56     * @throws UncheckedParseException when the parsing fails
    6357     */
    64     public String constructQuery(String search) throws ParseException {
     58    public String constructQuery(String search) throws UncheckedParseException {
    6559        try {
    6660            final Object result = ((Invocable) engine).invokeFunction("construct_query", search);
    6761            if (result == Boolean.FALSE) {
    68                 throw new ParseException();
     62                throw new UncheckedParseException();
    6963            }
    7064            String query = (String) result;
  • trunk/src/org/openstreetmap/josm/tools/date/DateUtils.java

    r9383 r9385  
    1818import org.openstreetmap.josm.data.preferences.BooleanProperty;
    1919import org.openstreetmap.josm.tools.CheckParameterUtil;
     20import org.openstreetmap.josm.tools.UncheckedParseException;
    2021
    2122/**
     
    6667     * @param str The XML date as string
    6768     * @return The date
    68      */
    69     public static synchronized Date fromString(String str) {
     69     * @throws UncheckedParseException if the date does not match any of the supported date formats
     70     */
     71    public static synchronized Date fromString(String str) throws UncheckedParseException {
    7072        return new Date(tsFromString(str));
    7173    }
     
    7577     * @param str The XML date as string
    7678     * @return The date in milliseconds since epoch
    77      */
    78     public static synchronized long tsFromString(String str) {
     79     * @throws UncheckedParseException if the date does not match any of the supported date formats
     80     */
     81    public static synchronized long tsFromString(String str) throws UncheckedParseException {
    7982        // "2007-07-25T09:26:24{Z|{+|-}01:00}"
    8083        if (checkLayout(str, "xxxx-xx-xxTxx:xx:xxZ") ||
     
    130133            return XML_DATE.newXMLGregorianCalendar(str).toGregorianCalendar().getTimeInMillis();
    131134        } catch (Exception ex) {
    132             return System.currentTimeMillis();
     135            throw new UncheckedParseException("The date string (" + str + ") could not be parsed.");
    133136        }
    134137    }
  • trunk/test/unit/org/openstreetmap/josm/tools/OverpassTurboQueryWizardTest.java

    r8876 r9385  
    4646     * Test erroneous value.
    4747     */
    48     @Test(expected = OverpassTurboQueryWizard.ParseException.class)
     48    @Test(expected = UncheckedParseException.class)
    4949    public void testErroneous() {
    5050        OverpassTurboQueryWizard.getInstance().constructQuery("foo");
  • trunk/test/unit/org/openstreetmap/josm/tools/date/DateUtilsTest.java

    r9383 r9385  
    44import static org.junit.Assert.assertEquals;
    55
     6import java.util.Date;
    67import java.util.TimeZone;
    78
    89import org.junit.BeforeClass;
    910import org.junit.Test;
     11import org.openstreetmap.josm.tools.UncheckedParseException;
    1012
    1113/**
     
    6567        assertEquals(-1041337172130L, DateUtils.fromString("1937-01-01T12:00:27.87+00:20").getTime());
    6668    }
     69
     70    /**
     71     * Verifies that parsing an illegal date throws a {@link UncheckedParseException}
     72     */
     73    @Test(expected = UncheckedParseException.class)
     74    public void testIllegalDate() {
     75        DateUtils.fromString("2014-");
     76    }
    6777}
Note: See TracChangeset for help on using the changeset viewer.