This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-beanutils.git

commit c5ae717d8048343808330d4c0916ac2188dc3f7a
Author: Gary Gregory <[email protected]>
AuthorDate: Wed Dec 30 10:54:59 2020 -0500

    Add and use AbstractConverter toLowerCase(), toTrim(), update Javadocs.
---
 .../beanutils2/converters/AbstractConverter.java   | 257 ++++++++++++---------
 .../beanutils2/converters/ArrayConverter.java      |   2 +-
 .../beanutils2/converters/BooleanConverter.java    |   5 +-
 .../beanutils2/converters/DateTimeConverter.java   |   2 +-
 .../beanutils2/converters/NumberConverter.java     |   2 +-
 .../converters/BooleanConverterTestCase.java       |  16 +-
 6 files changed, 158 insertions(+), 126 deletions(-)

diff --git 
a/src/main/java/org/apache/commons/beanutils2/converters/AbstractConverter.java 
b/src/main/java/org/apache/commons/beanutils2/converters/AbstractConverter.java
index c2b6770..ffa32a1 100644
--- 
a/src/main/java/org/apache/commons/beanutils2/converters/AbstractConverter.java
+++ 
b/src/main/java/org/apache/commons/beanutils2/converters/AbstractConverter.java
@@ -18,6 +18,8 @@ package org.apache.commons.beanutils2.converters;
 
 import java.lang.reflect.Array;
 import java.util.Collection;
+import java.util.Locale;
+import java.util.Objects;
 
 import org.apache.commons.beanutils2.BeanUtils;
 import org.apache.commons.beanutils2.ConversionException;
@@ -62,10 +64,40 @@ public abstract class AbstractConverter implements 
Converter {
 
     /** Current package name */
     //    getPackage() below returns null on some platforms/jvm versions 
during the unit tests.
-//    private static final String PACKAGE = 
AbstractConverter.class.getPackage().getName() + ".";
+    // private static final String PACKAGE = 
AbstractConverter.class.getPackage().getName() + ".";
     private static final String PACKAGE = 
"org.apache.commons.beanutils2.converters.";
 
     /**
+     * Converts the given object to a lower-case string.
+     *
+     * @param value the input string.
+     * @return the given string trimmed and converter to lower-case.
+     */
+    protected static String toLowerCase(final Object value) {
+        return toString(value).toLowerCase(Locale.ROOT);
+    }
+
+    /**
+     * Converts the given object to a lower-case string.
+     *
+     * @param value the input string.
+     * @return the given string trimmed and converter to lower-case.
+     */
+    protected static String toString(final Object value) {
+        return Objects.requireNonNull(value, "value").toString();
+    }
+
+    /**
+     * Converts the given object to a lower-case string.
+     *
+     * @param value the input string.
+     * @return the given string trimmed and converter to lower-case.
+     */
+    protected static String toTrim(final Object value) {
+        return toString(value).trim();
+    }
+
+    /**
      * Logging for this instance.
      */
     private transient Log log;
@@ -80,17 +112,15 @@ public abstract class AbstractConverter implements 
Converter {
      */
     private Object defaultValue = null;
 
-
-
     /**
-     * Construct a <i>Converter</i> that throws a
+     * Constructs a <i>Converter</i> that throws a
      * {@code ConversionException} if an error occurs.
      */
     public AbstractConverter() {
     }
 
     /**
-     * Construct a <i>Converter</i> that returns a default
+     * Constructs a <i>Converter</i> that returns a default
      * value if an error occurs.
      *
      * @param defaultValue The default value to be returned
@@ -101,22 +131,22 @@ public abstract class AbstractConverter implements 
Converter {
         setDefaultValue(defaultValue);
     }
 
-
-
     /**
-     * Indicates whether a default value will be returned or exception
-     * thrown in the event of a conversion error.
+     * Creates a standard conversion exception with a message indicating that
+     * the passed in value cannot be converted to the desired target type.
      *
-     * @return {@code true} if a default value will be returned for
-     * conversion errors or {@code false} if a {@link ConversionException}
-     * will be thrown.
+     * @param type the target type
+     * @param value the value to be converted
+     * @return a {@code ConversionException} with a standard message
+     * @since 1.9
      */
-    public boolean isUseDefault() {
-        return useDefault;
+    protected ConversionException conversionException(final Class<?> type, 
final Object value) {
+        return new ConversionException("Can't convert value '" + value
+                + "' to type " + type);
     }
 
     /**
-     * Convert the input object into an output object of the
+     * Converts the input object into an output object of the
      * specified type.
      *
      * @param <T> the target type of the conversion
@@ -180,7 +210,55 @@ public abstract class AbstractConverter implements 
Converter {
     }
 
     /**
-     * Convert the input object into a String.
+     * Returns the first element from an Array (or Collection)
+     * or the value unchanged if not an Array (or Collection).
+     *
+     * N.B. This needs to be overridden for array/Collection converters.
+     *
+     * @param value The value to convert
+     * @return The first element in an Array (or Collection)
+     * or the value unchanged if not an Array (or Collection)
+     */
+    protected Object convertArray(final Object value) {
+        if (value == null) {
+            return null;
+        }
+        if (value.getClass().isArray()) {
+            if (Array.getLength(value) > 0) {
+                return Array.get(value, 0);
+            }
+            return null;
+        }
+        if (value instanceof Collection) {
+            final Collection<?> collection = (Collection<?>)value;
+            if (collection.size() > 0) {
+                return collection.iterator().next();
+            }
+            return null;
+        }
+        return value;
+    }
+
+    /**
+     * Converts to the default type. This method is called if we do
+     * not have a target class. In this case, the T parameter is not set.
+     * Therefore, we can cast to it (which is required to fulfill the contract
+     * of the method signature).
+     *
+     * @param <T> the type of the result object
+     * @param targetClass the target class of the conversion
+     * @param value the value to be converted
+     * @return the converted value
+     */
+    private <T> T convertToDefaultType(final Class<T> targetClass, final 
Object value) {
+        @SuppressWarnings("unchecked")
+        final
+        T result = (T) convert(getDefaultType(), value);
+        return result;
+    }
+
+    /**
+     * Converts the input object into a String.
      * <p>
      * <b>N.B.</b>This implementation simply uses the value's
      * {@code toString()} method and should be overridden if a
@@ -196,7 +274,7 @@ public abstract class AbstractConverter implements 
Converter {
     }
 
     /**
-     * Convert the input object into an output object of the
+     * Converts the input object into an output object of the
      * specified type.
      * <p>
      * Typical implementations will provide a minimum of
@@ -211,40 +289,31 @@ public abstract class AbstractConverter implements 
Converter {
     protected abstract <T> T convertToType(Class<T> type, Object value) throws 
Throwable;
 
     /**
-     * Return the first element from an Array (or Collection)
-     * or the value unchanged if not an Array (or Collection).
-     *
-     * N.B. This needs to be overridden for array/Collection converters.
-     *
-     * @param value The value to convert
-     * @return The first element in an Array (or Collection)
-     * or the value unchanged if not an Array (or Collection)
+     * Return the default value for conversions to the specified
+     * type.
+     * @param type Data type to which this value should be converted.
+     * @return The default value for the specified type.
      */
-    protected Object convertArray(final Object value) {
-        if (value == null) {
-            return null;
-        }
-        if (value.getClass().isArray()) {
-            if (Array.getLength(value) > 0) {
-                return Array.get(value, 0);
-            }
-            return null;
-        }
-        if (value instanceof Collection) {
-            final Collection<?> collection = (Collection<?>)value;
-            if (collection.size() > 0) {
-                return collection.iterator().next();
-            }
+    protected Object getDefault(final Class<?> type) {
+        if (type.equals(String.class)) {
             return null;
         }
-        return value;
+        return defaultValue;
     }
 
     /**
-     * Handle Conversion Errors.
+     * Gets the default type this {@code Converter} handles.
+     *
+     * @return The default type this {@code Converter} handles.
+     */
+    protected abstract Class<?> getDefaultType();
+
+    /**
+     * Handles Conversion Errors.
      * <p>
      * If a default value has been specified then it is returned
      * otherwise a ConversionException is thrown.
+     * </p>
      *
      * @param <T> Target type of the conversion.
      * @param type Data type to which this value should be converted.
@@ -333,11 +402,42 @@ public abstract class AbstractConverter implements 
Converter {
     }
 
     /**
+     * Indicates whether a default value will be returned or exception
+     * thrown in the event of a conversion error.
+     *
+     * @return {@code true} if a default value will be returned for
+     * conversion errors or {@code false} if a {@link ConversionException}
+     * will be thrown.
+     */
+    public boolean isUseDefault() {
+        return useDefault;
+    }
+
+
+    /**
+     * Accessor method for Log instance.
+     * <p>
+     * The Log instance variable is transient and
+     * accessing it through this method ensures it
+     * is re-initialized when this instance is
+     * de-serialized.
+     *
+     * @return The Log instance.
+     */
+    Log log() {
+        if (log == null) {
+            log = LogFactory.getLog(getClass());
+        }
+        return log;
+    }
+
+    /**
      * Set the default value, converting as required.
      * <p>
      * If the default value is different from the type the
      * {@code Converter} handles, it will be converted
      * to the handled type.
+     * </p>
      *
      * @param defaultValue The default value to be returned
      * if the value to be converted is missing or an error
@@ -359,27 +459,7 @@ public abstract class AbstractConverter implements 
Converter {
     }
 
     /**
-     * Return the default type this {@code Converter} handles.
-     *
-     * @return The default type this {@code Converter} handles.
-     */
-    protected abstract Class<?> getDefaultType();
-
-    /**
-     * Return the default value for conversions to the specified
-     * type.
-     * @param type Data type to which this value should be converted.
-     * @return The default value for the specified type.
-     */
-    protected Object getDefault(final Class<?> type) {
-        if (type.equals(String.class)) {
-            return null;
-        }
-        return defaultValue;
-    }
-
-    /**
-     * Provide a String representation of this converter.
+     * Converts this instance to a String.
      *
      * @return A String representation of this converter
      */
@@ -388,27 +468,9 @@ public abstract class AbstractConverter implements 
Converter {
         return toString(getClass()) + "[UseDefault=" + useDefault + "]";
     }
 
-
-
     /**
-     * Accessor method for Log instance.
-     * <p>
-     * The Log instance variable is transient and
-     * accessing it through this method ensures it
-     * is re-initialized when this instance is
-     * de-serialized.
+     * Converts a {@code java.lang.Class} to a String.
      *
-     * @return The Log instance.
-     */
-    Log log() {
-        if (log == null) {
-            log = LogFactory.getLog(getClass());
-        }
-        return log;
-    }
-
-    /**
-     * Provide a String representation of a {@code java.lang.Class}.
      * @param type The {@code java.lang.Class}.
      * @return The String representation.
      */
@@ -441,35 +503,4 @@ public abstract class AbstractConverter implements 
Converter {
         return typeName;
     }
 
-    /**
-     * Performs a conversion to the default type. This method is called if we 
do
-     * not have a target class. In this case, the T parameter is not set.
-     * Therefore, we can cast to it (which is required to fulfill the contract
-     * of the method signature).
-     *
-     * @param <T> the type of the result object
-     * @param targetClass the target class of the conversion
-     * @param value the value to be converted
-     * @return the converted value
-     */
-    private <T> T convertToDefaultType(final Class<T> targetClass, final 
Object value) {
-        @SuppressWarnings("unchecked")
-        final
-        T result = (T) convert(getDefaultType(), value);
-        return result;
-    }
-
-    /**
-     * Generates a standard conversion exception with a message indicating that
-     * the passed in value cannot be converted to the desired target type.
-     *
-     * @param type the target type
-     * @param value the value to be converted
-     * @return a {@code ConversionException} with a standard message
-     * @since 1.9
-     */
-    protected ConversionException conversionException(final Class<?> type, 
final Object value) {
-        return new ConversionException("Can't convert value '" + value
-                + "' to type " + type);
-    }
 }
diff --git 
a/src/main/java/org/apache/commons/beanutils2/converters/ArrayConverter.java 
b/src/main/java/org/apache/commons/beanutils2/converters/ArrayConverter.java
index e291fc9..426e66d 100644
--- a/src/main/java/org/apache/commons/beanutils2/converters/ArrayConverter.java
+++ b/src/main/java/org/apache/commons/beanutils2/converters/ArrayConverter.java
@@ -438,7 +438,7 @@ public class ArrayConverter extends AbstractConverter {
         }
 
         // Trim any matching '{' and '}' delimiters
-        value = value.trim();
+        value = toTrim(value);
         if (value.startsWith("{") && value.endsWith("}")) {
             value = value.substring(1, value.length() - 1);
         }
diff --git 
a/src/main/java/org/apache/commons/beanutils2/converters/BooleanConverter.java 
b/src/main/java/org/apache/commons/beanutils2/converters/BooleanConverter.java
index 01d1b69..1067666 100644
--- 
a/src/main/java/org/apache/commons/beanutils2/converters/BooleanConverter.java
+++ 
b/src/main/java/org/apache/commons/beanutils2/converters/BooleanConverter.java
@@ -173,7 +173,7 @@ public final class BooleanConverter extends 
AbstractConverter {
             // guaranteed to be lower-case. By converting the input value
             // to lowercase too, we can use the efficient String.equals method
             // instead of the less-efficient String.equalsIgnoreCase method.
-            final String stringValue = value.toString().toLowerCase();
+            final String stringValue = toLowerCase(value);
 
             for (final String trueString : trueStrings) {
                 if (trueString.equals(stringValue)) {
@@ -191,6 +191,7 @@ public final class BooleanConverter extends 
AbstractConverter {
         throw conversionException(type, value);
     }
 
+
     /**
      * Copies the provided array, and ensures that
      * all the strings in the newly created array contain only lower-case
@@ -203,7 +204,7 @@ public final class BooleanConverter extends 
AbstractConverter {
     private static String[] copyStrings(final String[] src) {
         final String[] dst = new String[src.length];
         for(int i=0; i<src.length; ++i) {
-            dst[i] = src[i].toLowerCase();
+            dst[i] = toLowerCase(src[i]);
         }
         return dst;
     }
diff --git 
a/src/main/java/org/apache/commons/beanutils2/converters/DateTimeConverter.java 
b/src/main/java/org/apache/commons/beanutils2/converters/DateTimeConverter.java
index a838849..252192f 100644
--- 
a/src/main/java/org/apache/commons/beanutils2/converters/DateTimeConverter.java
+++ 
b/src/main/java/org/apache/commons/beanutils2/converters/DateTimeConverter.java
@@ -361,7 +361,7 @@ public abstract class DateTimeConverter extends 
AbstractConverter {
         }
 
         // Convert all other types to String & handle
-        final String stringValue = value.toString().trim();
+        final String stringValue = toTrim(value);
         if (stringValue.length() == 0) {
             return handleMissing(targetType);
         }
diff --git 
a/src/main/java/org/apache/commons/beanutils2/converters/NumberConverter.java 
b/src/main/java/org/apache/commons/beanutils2/converters/NumberConverter.java
index 3f745f7..f4e5f13 100644
--- 
a/src/main/java/org/apache/commons/beanutils2/converters/NumberConverter.java
+++ 
b/src/main/java/org/apache/commons/beanutils2/converters/NumberConverter.java
@@ -255,7 +255,7 @@ public abstract class NumberConverter extends 
AbstractConverter {
         }
 
         // Convert all other types to String & handle
-        final String stringValue = value.toString().trim();
+        final String stringValue = toTrim(value);
         if (stringValue.length() == 0) {
             return handleMissing(targetType);
         }
diff --git 
a/src/test/java/org/apache/commons/beanutils2/converters/BooleanConverterTestCase.java
 
b/src/test/java/org/apache/commons/beanutils2/converters/BooleanConverterTestCase.java
index 463888e..c936ed1 100644
--- 
a/src/test/java/org/apache/commons/beanutils2/converters/BooleanConverterTestCase.java
+++ 
b/src/test/java/org/apache/commons/beanutils2/converters/BooleanConverterTestCase.java
@@ -40,7 +40,7 @@ public class BooleanConverterTestCase extends TestCase {
     public void testAdditionalStrings() {
         final String[] trueStrings = {"sure"};
         final String[] falseStrings = {"nope"};
-        final BooleanConverter converter = new BooleanConverter(
+        final AbstractConverter converter = new BooleanConverter(
             trueStrings, falseStrings);
         testConversionValues(
             converter,
@@ -62,7 +62,7 @@ public class BooleanConverterTestCase extends TestCase {
     }
 
     public void testCaseInsensitivity() {
-        final BooleanConverter converter = new BooleanConverter();
+        final AbstractConverter converter = new BooleanConverter();
         testConversionValues(
             converter,
             new String[] {"Yes", "TRUE"},
@@ -73,7 +73,7 @@ public class BooleanConverterTestCase extends TestCase {
      * Tests a conversion to another target type. This should not be possible.
      */
     public void testConversionToOtherType() {
-        final BooleanConverter converter = new BooleanConverter();
+        final AbstractConverter converter = new BooleanConverter();
         try {
             converter.convert(Integer.class, STANDARD_TRUES[0]);
             fail("Could convert to unsupported type!");
@@ -82,7 +82,7 @@ public class BooleanConverterTestCase extends TestCase {
         }
     }
 
-    protected void testConversionValues(final BooleanConverter converter,
+    protected void testConversionValues(final AbstractConverter converter,
             final String[] trueValues, final String[] falseValues) {
 
         for (final String trueValue : trueValues) {
@@ -95,14 +95,14 @@ public class BooleanConverterTestCase extends TestCase {
 
     public void testDefaultValue() {
         final Object defaultValue = Boolean.TRUE;
-        final BooleanConverter converter = new BooleanConverter(defaultValue);
+        final AbstractConverter converter = new BooleanConverter(defaultValue);
 
         assertSame(defaultValue, converter.convert(Boolean.class, "bogus"));
         testConversionValues(converter, STANDARD_TRUES, STANDARD_FALSES);
     }
 
     public void testInvalidString() {
-        final BooleanConverter converter = new BooleanConverter();
+        final AbstractConverter converter = new BooleanConverter();
         try {
             converter.convert(Boolean.class, "bogus");
             fail("Converting invalid string should have generated an 
exception");
@@ -115,12 +115,12 @@ public class BooleanConverterTestCase extends TestCase {
      * Tests whether a conversion to a primitive boolean is possible.
      */
     public void testPrimitiveTargetClass() {
-        final BooleanConverter converter = new BooleanConverter();
+        final AbstractConverter converter = new BooleanConverter();
         assertTrue("Wrong result", converter.convert(Boolean.TYPE, 
STANDARD_TRUES[0]));
     }
 
     public void testStandardValues() {
-        final BooleanConverter converter = new BooleanConverter();
+        final AbstractConverter converter = new BooleanConverter();
         testConversionValues(converter, STANDARD_TRUES, STANDARD_FALSES);
     }
 

Reply via email to