garydgregory commented on code in PR #216:
URL: https://github.com/apache/commons-cli/pull/216#discussion_r1467738154
##########
src/main/java/org/apache/commons/cli/ParseException.java:
##########
@@ -21,11 +21,33 @@ Licensed to the Apache Software Foundation (ASF) under one
or more
* Base for Exceptions thrown during parsing of a command-line.
*/
public class ParseException extends Exception {
+
/**
* This exception {@code serialVersionUID}.
*/
private static final long serialVersionUID = 9112808380089253192L;
+ /**
+ * Converts any exception except {@code UnsupportedOperationException} to
a {@code ParseException}.
+ * if {@code e} is an instance of {@code ParseException} it is returned,
otherwise a {@code ParseException} is
+ * created that wraps it.
+ * <p>
Review Comment:
Hello @Claudenw
- Close HTML tags
- Add `@since` tags to new `public` and `protected` elements.
##########
src/main/java/org/apache/commons/cli/ParseException.java:
##########
@@ -21,11 +21,33 @@ Licensed to the Apache Software Foundation (ASF) under one
or more
* Base for Exceptions thrown during parsing of a command-line.
*/
public class ParseException extends Exception {
+
/**
* This exception {@code serialVersionUID}.
*/
private static final long serialVersionUID = 9112808380089253192L;
+ /**
+ * Converts any exception except {@code UnsupportedOperationException} to
a {@code ParseException}.
+ * if {@code e} is an instance of {@code ParseException} it is returned,
otherwise a {@code ParseException} is
+ * created that wraps it.
+ * <p>
+ * Note: {@code UnsupportedOperationException} are not wrapped. This is
to solve a legacy expected exception problem and will be
+ * removed in the future.</p>
+ * @param e the exception to convert.
+ * @return the ParseException.
+ * @throws UnsupportedOperationException due to legacy expectations. Will
be removed in the future.
+ */
+ public static ParseException wrap(final Exception e) throws
UnsupportedOperationException {
+ if (e instanceof UnsupportedOperationException) {
+ throw (UnsupportedOperationException) e;
+ }
+
Review Comment:
Remove extra white-space.
##########
src/site/xdoc/usage.xml:
##########
@@ -384,5 +384,94 @@ catch (ParseException exp) {
System.out.println("Unexpected exception:" + exp.getMessage());
}</source>
</section>
+ <section name="Converting (Parsing) Option Values">
Review Comment:
Nice! 😄 👏
##########
src/main/java/org/apache/commons/cli/PatternOptionBuilder.java:
##########
@@ -102,14 +102,41 @@ public class PatternOptionBuilder {
/** URL class */
public static final Class<URL> URL_VALUE = URL.class;
+
+ /** The converter to use for Unimplemented data types */
+ static final Converter<?> NOT_IMPLEMENTED = s -> {
+ throw new UnsupportedOperationException("Not yet implemented");
+ };
+
+ static {
+ registerTypes();
+ }
+
+ /**
+ * Registers custom {@code Converter}s with the {@code TypeHandler}.
+ */
+ public static void registerTypes() {
+ TypeHandler.register(PatternOptionBuilder.FILES_VALUE,
NOT_IMPLEMENTED);
+ }
/**
* Retrieve the class that {@code ch} represents.
*
* @param ch the specified character
* @return The class that {@code ch} represents
+ * @deprecated use {@link #getValueType(char)}
Review Comment:
Add `@Deprecated` annotation to the method.
##########
src/main/java/org/apache/commons/cli/PatternOptionBuilder.java:
##########
@@ -102,14 +102,41 @@ public class PatternOptionBuilder {
/** URL class */
public static final Class<URL> URL_VALUE = URL.class;
+
+ /** The converter to use for Unimplemented data types */
+ static final Converter<?> NOT_IMPLEMENTED = s -> {
+ throw new UnsupportedOperationException("Not yet implemented");
+ };
+
+ static {
+ registerTypes();
+ }
+
+ /**
+ * Registers custom {@code Converter}s with the {@code TypeHandler}.
+ */
+ public static void registerTypes() {
+ TypeHandler.register(PatternOptionBuilder.FILES_VALUE,
NOT_IMPLEMENTED);
+ }
/**
* Retrieve the class that {@code ch} represents.
*
* @param ch the specified character
* @return The class that {@code ch} represents
+ * @deprecated use {@link #getValueType(char)}
*/
public static Object getValueClass(final char ch) {
+ return getValueType(ch);
+ }
+
+ /**
+ * Retrieve the class that {@code ch} represents.
+ *
+ * @param ch the specified character
+ * @return The class that {@code ch} represents
+ */
+ public static Class<?> getValueType(final char ch) {
Review Comment:
Add `@since` tags to new public and protected elements.
##########
src/main/java/org/apache/commons/cli/PatternOptionBuilder.java:
##########
@@ -102,14 +102,41 @@ public class PatternOptionBuilder {
/** URL class */
public static final Class<URL> URL_VALUE = URL.class;
+
+ /** The converter to use for Unimplemented data types */
+ static final Converter<?> NOT_IMPLEMENTED = s -> {
+ throw new UnsupportedOperationException("Not yet implemented");
+ };
+
+ static {
+ registerTypes();
+ }
+
+ /**
+ * Registers custom {@code Converter}s with the {@code TypeHandler}.
+ */
Review Comment:
Add `@since` tags to new public and protected elements.
##########
src/main/java/org/apache/commons/cli/TypeHandler.java:
##########
@@ -19,29 +19,114 @@ Licensed to the Apache Software Foundation (ASF) under one
or more
import java.io.File;
import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.net.MalformedURLException;
+import java.math.BigDecimal;
+import java.math.BigInteger;
import java.net.URL;
+import java.nio.file.Path;
import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
/**
- * This is a temporary implementation. TypeHandler will handle the
pluggableness of OptionTypes and it will direct all
- * of these types of conversion functionalities to ConvertUtils component in
Commons already. BeanUtils I think.
+ * TypeHandler will handle the pluggable conversion and verification of
+ * Option types. It handles the mapping of classes to bot converters and
verifiers.
+ * It provides the default conversion and verification methods when converters
and verifiers
+ * are not explicitly set.
+ * <p>
+ * If Options are serialized and deserialized their converters and verifiers
will revert to the
+ * defaults defined in this class. To correctly de-serialize Options with
custom converters and/or
+ * verifiers, using the default serialization methods, this class should be
properly configured with the custom
+ * converters and verifiers for the specific class.
+ * </p>
*/
public class TypeHandler {
+
+ /** Value of hex conversion of strings */
+ private static final int HEX_RADIX = 16;
+
+ /** Map of classes to converters. */
+ private static Map<Class<?>, Converter<?>> converterMap = new HashMap<>();
+
+ static {
+ resetConverters();
+ }
+
+ /**
+ * Resets the registered Converters to the default state.
+ * @since 1.7.0
+ */
+ public static void resetConverters() {
+ converterMap.clear();
+ converterMap.put(Object.class, Converter.OBJECT);
+ converterMap.put(Class.class, Converter.CLASS);
+ converterMap.put(Date.class, Converter.DATE);
+ converterMap.put(File.class, Converter.FILE);
+ converterMap.put(Path.class, Converter.PATH);
+ converterMap.put(Number.class, Converter.NUMBER);
+ converterMap.put(URL.class, Converter.URL);
+ converterMap.put(FileInputStream.class, s -> new FileInputStream(s));
+ converterMap.put(Long.class, Long::parseLong);
+ converterMap.put(Integer.class, Integer::parseInt);
+ converterMap.put(Short.class, Short::parseShort);
+ converterMap.put(Byte.class, Byte::parseByte);
+ converterMap.put(Character.class, s -> {
+ if (s.startsWith("\\u")) {
+ return Character.toChars(Integer.parseInt(s.substring(2),
HEX_RADIX))[0];
+ } else {
+ return s.charAt(0);
+ } });
+ converterMap.put(Double.class, Double::parseDouble);
+ converterMap.put(Float.class, Float::parseFloat);
+ converterMap.put(BigInteger.class, s -> new BigInteger(s));
+ converterMap.put(BigDecimal.class, s -> new BigDecimal(s));
+ }
+
+ /**
+ * Unregisters all Converters.
+ * @since 1.7.0
+ */
+ public static void noConverters() {
+ converterMap.clear();
+ }
+
+ /**
+ * Registers a Converter for a Class. If @code converter} is null
registration is cleared for {@code clazz}, and
Review Comment:
Only one space after the `.`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]