View Javadoc
1   /*
2     Licensed to the Apache Software Foundation (ASF) under one or more
3     contributor license agreements.  See the NOTICE file distributed with
4     this work for additional information regarding copyright ownership.
5     The ASF licenses this file to You under the Apache License, Version 2.0
6     (the "License"); you may not use this file except in compliance with
7     the License.  You may obtain a copy of the License at
8   
9         http://www.apache.org/licenses/LICENSE-2.0
10  
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16   */
17  package org.apache.commons.cli;
18  
19  import java.io.File;
20  import java.net.MalformedURLException;
21  import java.net.URL;
22  import java.nio.file.InvalidPathException;
23  import java.nio.file.Path;
24  import java.nio.file.Paths;
25  import java.text.SimpleDateFormat;
26  import java.util.Date;
27  
28  /**
29   * The definition of the functional interface to call when doing a conversion. Like {@code Function<String,T>} but can throw an Exception.
30   *
31   * @param <T> The return type for the function.
32   * @param <E> The kind of thrown exception or error.
33   * @since 1.7.0
34   */
35  @FunctionalInterface
36  public interface Converter<T, E extends Throwable> {
37      // See also Apache Commons Lang FailableFunction
38  
39      /**
40       * The default converter. Does nothing.
41       */
42      Converter<?, RuntimeException> DEFAULT = s -> s;
43  
44      /**
45       * Class name converter. Calls {@link Class#forName(String)}.
46       */
47      Converter<Class<?>, ClassNotFoundException> CLASS = Class::forName;
48  
49      /**
50       * File name converter. Calls {@link File#File(String)}.
51       */
52      Converter<File, NullPointerException> FILE = File::new;
53  
54      /**
55       * Path converter. Calls {@link Paths#get(java.net.URI)}.
56       */
57      Converter<Path, InvalidPathException> PATH = Paths::get;
58  
59      /**
60       * Number converter. Converts to a Double if a decimal point ('.') is in the string or a Long otherwise.
61       */
62      Converter<Number, NumberFormatException> NUMBER = s -> s.indexOf('.') != -1 ? (Number) Double.valueOf(s) : (Number) Long.valueOf(s);
63  
64      /**
65       * Converts a class name to an instance of the class. Uses the Class converter to find the class and then call the default constructor.
66       *
67       * @see #CLASS
68       */
69      Converter<Object, ReflectiveOperationException> OBJECT = s -> CLASS.apply(s).getConstructor().newInstance();
70  
71      /**
72       * Creates a URL. Calls {@link URL#URL(String)}.
73       */
74      Converter<URL, MalformedURLException> URL = URL::new;
75  
76      /**
77       * Converts to a date using the format string Form "EEE MMM dd HH:mm:ss zzz yyyy".
78       */
79      Converter<Date, java.text.ParseException> DATE = s -> new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(s);
80  
81      /**
82       * Applies the conversion function to the String argument.
83       *
84       * @param string the String to convert
85       * @return the Object from the conversion.
86       * @throws E on error.
87       */
88      T apply(String string) throws E;
89  }