velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cbris...@apache.org
Subject svn commit: r1827576 - in /velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools: ConversionUtils.java generic/ConversionTool.java
Date Fri, 23 Mar 2018 15:13:53 GMT
Author: cbrisson
Date: Fri Mar 23 15:13:52 2018
New Revision: 1827576

URL: http://svn.apache.org/viewvc?rev=1827576&view=rev
Log:
[tools] add new $conv.toList(obj) conversion method

Modified:
    velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ConversionUtils.java
    velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/ConversionTool.java

Modified: velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ConversionUtils.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ConversionUtils.java?rev=1827576&r1=1827575&r2=1827576&view=diff
==============================================================================
--- velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ConversionUtils.java
(original)
+++ velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ConversionUtils.java
Fri Mar 23 15:13:52 2018
@@ -31,10 +31,13 @@ import java.text.FieldPosition;
 import java.text.NumberFormat;
 import java.text.ParsePosition;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Date;
 import java.util.Calendar;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.TimeZone;
@@ -841,4 +844,16 @@ public class ConversionUtils
         catch (Exception e) {}
         return null;
     }
+
+    public static List asList(Object value)
+    {
+        if (value instanceof List) return (List)value;
+        else if (value.getClass().isArray()) return Arrays.asList(value);
+        else
+        {
+            List ret = new ArrayList();
+            ret.add(value);
+            return ret;
+        }
+    }
 }

Modified: velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/ConversionTool.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/ConversionTool.java?rev=1827576&r1=1827575&r2=1827576&view=diff
==============================================================================
--- velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/ConversionTool.java
(original)
+++ velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/ConversionTool.java
Fri Mar 23 15:13:52 2018
@@ -25,6 +25,7 @@ import java.util.Collection;
 import java.util.Date;
 import java.util.Calendar;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Locale;
 import java.util.TimeZone;
 import org.apache.velocity.tools.ConversionUtils;
@@ -657,6 +658,19 @@ public class ConversionTool extends Loca
         return calendars;
     }
 
+    /**
+     * Convert a singleton or an array as List  
+     * @param value Object to be converted
+     * @return either the object itself if it's already a list,
+     *         the object converted to a list if it's an array,
+     *         or a new singleton list.
+     */
+    public List toList(Object value)
+    {
+        if (value == null) return null;
+        else return ConversionUtils.asList(value);
+    }
+    
 
     // --------------------- basic string parsing methods --------------
 



Mime
View raw message