@@ -17,7 +17,9 @@ Licensed to the Apache Software Foundation (ASF) under one or more
1717package org .apache .commons .cli ;
1818
1919import java .io .File ;
20+ import java .net .MalformedURLException ;
2021import java .net .URL ;
22+ import java .nio .file .InvalidPathException ;
2123import java .nio .file .Path ;
2224import java .text .SimpleDateFormat ;
2325import java .util .Date ;
@@ -27,28 +29,36 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2729 * Like {@code Function<String,T>} but can throw an Exception.
2830 *
2931 * @param <T> The return type for the function.
32+ * @param <E> The kind of thrown exception or error.
3033 * @since 1.7.0
3134 */
3235@ FunctionalInterface
33- public interface Converter <T > {
36+ public interface Converter <T , E extends Throwable > {
37+ // See also Apache Commons Lang FailableFunction
3438
35- /** The default converter. Does nothing. */
36- Converter <?> DEFAULT = s -> s ;
39+ /**
40+ * The default converter. Does nothing.
41+ */
42+ Converter <?, RuntimeException > DEFAULT = s -> s ;
3743
38- /** Class name converter. Calls {@code Class.forName}. */
39- Converter <Class <?>> CLASS = Class ::forName ;
44+ /**
45+ * Class name converter. Calls {@code Class.forName}.
46+ */
47+ Converter <Class <?>, ClassNotFoundException > CLASS = Class ::forName ;
4048
41- /** File name converter. Calls @{code new File(s)} */
42- Converter <File > FILE = File ::new ;
49+ /**
50+ * File name converter. Calls @{code new File(s)}.
51+ */
52+ Converter <File , NullPointerException > FILE = File ::new ;
4353
4454 /** Path converter. Calls @{code new Path(s)} */
45- Converter <Path > PATH = s -> new File (s ).toPath ();
55+ Converter <Path , InvalidPathException > PATH = s -> new File (s ).toPath ();
4656
4757 /**
4858 * Number converter. Converts to a Double if a decimal point ('.') is in the
4959 * string or a Long otherwise.
5060 */
51- Converter <Number > NUMBER = s -> {
61+ Converter <Number , NumberFormatException > NUMBER = s -> {
5262 if (s .indexOf ('.' ) != -1 ) {
5363 return Double .valueOf (s );
5464 }
@@ -60,19 +70,23 @@ public interface Converter<T> {
6070 * to find the class and then call the default constructor.
6171 * @see #CLASS
6272 */
63- Converter <Object > OBJECT = s -> CLASS .apply (s ).getConstructor ().newInstance ();
73+ Converter <Object , ReflectiveOperationException > OBJECT = s -> CLASS .apply (s ).getConstructor ().newInstance ();
6474
65- /** Creates a URL. Calls {@code new URL(s)}. */
66- Converter <URL > URL = java .net .URL ::new ;
75+ /**
76+ * Creates a URL. Calls {@code new URL(s)}.
77+ */
78+ Converter <URL , MalformedURLException > URL = java .net .URL ::new ;
6779
68- /** Converts to a date using the format string Form "EEE MMM dd HH:mm:ss zzz yyyy". */
69- Converter <Date > DATE = s -> new SimpleDateFormat ("EEE MMM dd HH:mm:ss zzz yyyy" ).parse (s );
80+ /**
81+ * Converts to a date using the format string Form "EEE MMM dd HH:mm:ss zzz yyyy".
82+ */
83+ Converter <Date , java .text .ParseException > DATE = s -> new SimpleDateFormat ("EEE MMM dd HH:mm:ss zzz yyyy" ).parse (s );
7084
7185 /**
7286 * Applies the conversion function to the String argument.
7387 * @param str the String to convert
7488 * @return the Object from the conversion.
75- * @throws Exception on error.
89+ * @throws E on error.
7690 */
77- T apply (String str ) throws Exception ;
91+ T apply (String str ) throws E ;
7892}
0 commit comments