You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by cb...@apache.org on 2022/07/25 12:06:42 UTC

[velocity-tools] branch master updated: Add NumberTool.toBoolean() method - fixes VELTOOLS-195

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

cbrisson pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/velocity-tools.git


The following commit(s) were added to refs/heads/master by this push:
     new 79842528 Add NumberTool.toBoolean() method - fixes VELTOOLS-195
79842528 is described below

commit 79842528ff892efd8e580a2885a1bd6a633b24bc
Author: Claude Brisson <cl...@renegat.net>
AuthorDate: Mon Jul 25 14:06:25 2022 +0200

    Add NumberTool.toBoolean() method - fixes VELTOOLS-195
---
 .../src/main/webapp/number.vm                      |  3 +++
 .../velocity/tools/generic/ConversionTool.java     |  2 +-
 .../apache/velocity/tools/generic/NumberTool.java  | 14 ++++++++++++-
 .../tools/test/whitebox/GenericToolsTests.java     | 23 ++++++++++++----------
 4 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/velocity-tools-examples/velocity-tools-examples-showcase/src/main/webapp/number.vm b/velocity-tools-examples/velocity-tools-examples-showcase/src/main/webapp/number.vm
index 38f3c8b7..6aaa7731 100644
--- a/velocity-tools-examples/velocity-tools-examples-showcase/src/main/webapp/number.vm
+++ b/velocity-tools-examples/velocity-tools-examples-showcase/src/main/webapp/number.vm
@@ -66,5 +66,8 @@ $demo.thisPage.insert("#doclink( 'NumberTool' true )").
 #set( $desc = 'Converts an object to an instance of Number using the specified format and Localeif the object is not already an instance of Number.' )
 #demo3( 'number' 'toNumber' 4 $desc )
 
+#set( $desc = 'Converts an object to an instance of Boolean if the object is not already an instance of Boolean.' )
+#demo1( 'number' 'toBoolean' 4 $desc )
+
 #demoCustom( 'number' )
 </table>
diff --git a/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/ConversionTool.java b/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/ConversionTool.java
index f50acc53..5586a331 100644
--- a/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/ConversionTool.java
+++ b/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/ConversionTool.java
@@ -54,7 +54,7 @@ import org.apache.velocity.tools.config.SkipSetters;
  * @author Nathan Bubna
  * @version $Revision$ $Date: 2007-02-26 11:24:39 -0800 (Mon, 26 Feb 2007) $
  * @since VelocityTools 2.0
- * @deprecated use NumberTool for numbers formatting/parsing, DateTool for date/time formatting/parsing,
+ * @deprecated use NumberTool for numbers and booleans formatting/parsing, DateTool for date/time formatting/parsing,
  * or CollectionTool for toStrings().
  */
 
diff --git a/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/NumberTool.java b/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/NumberTool.java
index 5278d099..66e13150 100644
--- a/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/NumberTool.java
+++ b/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/NumberTool.java
@@ -217,7 +217,7 @@ public class NumberTool extends FormatConfig implements Serializable
 
     /**
      * Converts an object to an instance of {@link Number} using the
-     * specified format and {@link Locale}if the object is not already
+     * specified format and {@link Locale} if the object is not already
      * an instance of Number.
      *
      * @param format - the format the number is in
@@ -232,4 +232,16 @@ public class NumberTool extends FormatConfig implements Serializable
         return ConversionUtils.toNumber(obj, format, locale);
     }
 
+    /**
+     * Converts an object to an instance of {@link Boolean} if the object
+     * is not already an instance of Boolean.
+     *
+     * @param value the object to be converted
+     * @return a {@link Boolean} object for the specified value or
+     *         <code>null</code> if the value is null or the conversion failed
+     */
+    public Boolean toBoolean(Object value)
+    {
+        return ConversionUtils.toBoolean(value);
+    }
 }
diff --git a/velocity-tools-generic/src/test/java/org/apache/velocity/tools/test/whitebox/GenericToolsTests.java b/velocity-tools-generic/src/test/java/org/apache/velocity/tools/test/whitebox/GenericToolsTests.java
index 6e07e655..1e98fdb3 100644
--- a/velocity-tools-generic/src/test/java/org/apache/velocity/tools/test/whitebox/GenericToolsTests.java
+++ b/velocity-tools-generic/src/test/java/org/apache/velocity/tools/test/whitebox/GenericToolsTests.java
@@ -82,9 +82,9 @@ public class GenericToolsTests {
         assertEquals(disp,dateTool.format(date));
         assertEquals(disp2,dateTool.format("yyyy/MM/dd",date));
         /* test parsing */
-        assertEquals(new Integer(2007),dateTool.getYear(disp));
-        assertEquals(new Integer(0),dateTool.getMonth(disp));
-        assertEquals(new Integer(2),dateTool.getDay(disp));
+        assertEquals(Integer.valueOf(2007),dateTool.getYear(disp));
+        assertEquals(Integer.valueOf(0),dateTool.getMonth(disp));
+        assertEquals(Integer.valueOf(2),dateTool.getDay(disp));
     }
 
     public @Test void testEscapeTool() {
@@ -151,30 +151,33 @@ public class GenericToolsTests {
         assertNotNull(mathTool);
         assertEquals(1,mathTool.abs(-1));
         assertEquals(2,mathTool.add(1,1));
-        assertEquals(new Integer(3),mathTool.ceil(2.5));
+        assertEquals(Integer.valueOf(3),mathTool.ceil(2.5));
         assertEquals(4,mathTool.div(8,2));
-        assertEquals(new Integer(5),mathTool.floor(5.1));
+        assertEquals(Integer.valueOf(5),mathTool.floor(5.1));
         assertEquals(6,mathTool.getAverage(new long[] {5,6,7}));
         /* getTotal() watches the type of its first argument, so assertEquals needs a long */
         assertEquals(7L,mathTool.getTotal(new long[]{2, 2, 3}));
-        assertEquals(new Integer(8), mathTool.idiv(130, 16));
+        assertEquals(Integer.valueOf(8), mathTool.idiv(130, 16));
         assertEquals(9,mathTool.max(9,-10));
         assertEquals(10, mathTool.min(10, 20));
-        assertEquals(new Integer(11),mathTool.mod(37,13));
+        assertEquals(Integer.valueOf(11),mathTool.mod(37,13));
         assertEquals(12, mathTool.mul(3, 4));
-        assertEquals(new Integer(13),mathTool.round(12.8));
+        assertEquals(Integer.valueOf(13),mathTool.round(12.8));
         assertEquals(new Double(14.2),mathTool.roundTo(1,14.18));
         assertEquals(new Double(-5.0),mathTool.roundTo(2,-4.999));
         assertEquals(15,mathTool.sub(30,15));
         assertEquals(16, mathTool.pow(4, 2));
-        assertEquals(new Integer(17),mathTool.toInteger("17"));
+        assertEquals(Integer.valueOf(17),mathTool.toInteger("17"));
         assertEquals(new Double(18.1),mathTool.toDouble("18.1"));
     }
 
     public @Test void testNumberTool() {
         NumberTool numberTool = (NumberTool)toolbox.get("number");
         assertNotNull(numberTool);
-//        assertEquals()
+        assertEquals("17", numberTool.integer("17"));
+        assertEquals(14.2, numberTool.toNumber("#0.00", "14.20", Locale.ROOT));
+        assertEquals(Boolean.valueOf(true), numberTool.toBoolean("true"));
+
     }
 
     public @Test void testResourceTool() {