Converter.java

  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.       http://www.apache.org/licenses/LICENSE-2.0

  9.   Unless required by applicable law or agreed to in writing, software
  10.   distributed under the License is distributed on an "AS IS" BASIS,
  11.   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12.   See the License for the specific language governing permissions and
  13.   limitations under the License.
  14.  */
  15. package org.apache.commons.cli;

  16. import java.io.File;
  17. import java.net.MalformedURLException;
  18. import java.net.URL;
  19. import java.nio.file.InvalidPathException;
  20. import java.nio.file.Path;
  21. import java.nio.file.Paths;
  22. import java.text.SimpleDateFormat;
  23. import java.util.Date;

  24. /**
  25.  * The definition of the functional interface to call when doing a conversion. Like {@code Function<String,T>} but can throw an Exception.
  26.  *
  27.  * @param <T> The return type for the function.
  28.  * @param <E> The kind of thrown exception or error.
  29.  * @since 1.7.0
  30.  */
  31. @FunctionalInterface
  32. public interface Converter<T, E extends Throwable> {
  33.     // See also Apache Commons Lang FailableFunction

  34.     /**
  35.      * The default converter. Does nothing.
  36.      */
  37.     Converter<?, RuntimeException> DEFAULT = s -> s;

  38.     /**
  39.      * Class name converter. Calls {@link Class#forName(String)}.
  40.      */
  41.     Converter<Class<?>, ClassNotFoundException> CLASS = Class::forName;

  42.     /**
  43.      * File name converter. Calls {@link File#File(String)}.
  44.      */
  45.     Converter<File, NullPointerException> FILE = File::new;

  46.     /**
  47.      * Path converter. Calls {@link Paths#get(java.net.URI)}.
  48.      */
  49.     Converter<Path, InvalidPathException> PATH = Paths::get;

  50.     /**
  51.      * Number converter. Converts to a Double if a decimal point ('.') is in the string or a Long otherwise.
  52.      */
  53.     Converter<Number, NumberFormatException> NUMBER = s -> s.indexOf('.') != -1 ? (Number) Double.valueOf(s) : (Number) Long.valueOf(s);

  54.     /**
  55.      * Converts a class name to an instance of the class. Uses the Class converter to find the class and then call the default constructor.
  56.      *
  57.      * @see #CLASS
  58.      */
  59.     Converter<Object, ReflectiveOperationException> OBJECT = s -> CLASS.apply(s).getConstructor().newInstance();

  60.     /**
  61.      * Creates a URL. Calls {@link URL#URL(String)}.
  62.      */
  63.     Converter<URL, MalformedURLException> URL = URL::new;

  64.     /**
  65.      * Converts to a date using the format string Form "EEE MMM dd HH:mm:ss zzz yyyy".
  66.      */
  67.     Converter<Date, java.text.ParseException> DATE = s -> new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(s);

  68.     /**
  69.      * Applies the conversion function to the String argument.
  70.      *
  71.      * @param string the String to convert
  72.      * @return the Object from the conversion.
  73.      * @throws E on error.
  74.      */
  75.     T apply(String string) throws E;
  76. }