You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2013/10/23 22:13:11 UTC

svn commit: r1535145 - in /commons/proper/beanutils/branches/java5/src: main/java/org/apache/commons/beanutils/converters/BooleanConverter.java test/java/org/apache/commons/beanutils/converters/BooleanConverterTestCase.java

Author: oheger
Date: Wed Oct 23 20:13:11 2013
New Revision: 1535145

URL: http://svn.apache.org/r1535145
Log:
Generified BooleanConverter.

The converter now only supports conversions to the target types String and
Boolean.

Modified:
    commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/BooleanConverter.java
    commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/BooleanConverterTestCase.java

Modified: commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/BooleanConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/BooleanConverter.java?rev=1535145&r1=1535144&r2=1535145&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/BooleanConverter.java (original)
+++ commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/BooleanConverter.java Wed Oct 23 20:13:11 2013
@@ -51,7 +51,7 @@ import org.apache.commons.beanutils.Conv
  * @version $Id$
  * @since 1.3
  */
-public final class BooleanConverter extends AbstractConverter {
+public final class BooleanConverter extends AbstractConverter<Boolean> {
 
 
     // ----------------------------------------------------------- Constructors
@@ -178,7 +178,7 @@ public final class BooleanConverter exte
      * @since 1.8.0
      */
     @Override
-    protected Class getDefaultType() {
+    protected Class<Boolean> getDefaultType() {
         return Boolean.class;
     }
 
@@ -202,27 +202,29 @@ public final class BooleanConverter exte
      * @since 1.8.0
      */
     @Override
-    protected Object convertToType(Class type, Object value) throws Throwable {
+    protected <T> T convertToType(Class<T> type, Object value) throws Throwable {
 
-        // All the values in the trueStrings and falseStrings arrays are
-        // 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.
-        String stringValue = value.toString().toLowerCase();
-
-        for(int i=0; i<trueStrings.length; ++i) {
-            if (trueStrings[i].equals(stringValue)) {
-                return Boolean.TRUE;
+        if (Boolean.class.equals(type) || Boolean.TYPE.equals(type)) {
+            // All the values in the trueStrings and falseStrings arrays are
+            // 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.
+            String stringValue = value.toString().toLowerCase();
+
+            for (int i = 0; i < trueStrings.length; ++i) {
+                if (trueStrings[i].equals(stringValue)) {
+                    return type.cast(Boolean.TRUE);
+                }
             }
-        }
 
-        for(int i=0; i<falseStrings.length; ++i) {
-            if (falseStrings[i].equals(stringValue)) {
-                return Boolean.FALSE;
+            for (int i = 0; i < falseStrings.length; ++i) {
+                if (falseStrings[i].equals(stringValue)) {
+                    return type.cast(Boolean.FALSE);
+                }
             }
         }
 
-        throw new ConversionException("Can't convert value '" + value + "' to a Boolean");
+        throw new ConversionException("Can't convert value '" + value + "' to type " + type);
     }
 
     /**

Modified: commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/BooleanConverterTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/BooleanConverterTestCase.java?rev=1535145&r1=1535144&r2=1535145&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/BooleanConverterTestCase.java (original)
+++ commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/BooleanConverterTestCase.java Wed Oct 23 20:13:11 2013
@@ -17,10 +17,10 @@
 
 package org.apache.commons.beanutils.converters;
 
-import org.apache.commons.beanutils.ConversionException;
-
 import junit.framework.TestCase;
 
+import org.apache.commons.beanutils.ConversionException;
+
 /**
  * @version $Id$
  */
@@ -95,6 +95,26 @@ public class BooleanConverterTestCase ex
         }
     }
 
+    /**
+     * Tests a conversion to another target type. This should not be possible.
+     */
+    public void testConversionToOtherType() {
+        BooleanConverter converter = new BooleanConverter();
+        try {
+            converter.convert(Integer.class, STANDARD_TRUES[0]);
+            fail("Could convert to unsupported type!");
+        } catch (ConversionException cex) {
+            // Expected result
+        }
+    }
+
+    /**
+     * Tests whether a conversion to a primitive boolean is possible.
+     */
+    public void testPrimitiveTargetClass() {
+        BooleanConverter converter = new BooleanConverter();
+        assertTrue("Wrong result", converter.convert(Boolean.TYPE, STANDARD_TRUES[0]));
+    }
 
     protected void testConversionValues(BooleanConverter converter,
             String[] trueValues, String[] falseValues) {