Author: cbrisson
Date: Mon Jun 25 23:58:00 2018
New Revision: 1834385

URL: http://svn.apache.org/viewvc?rev=1834385&view=rev
Log:
[tools] Have CollectionTool.sort() methods filter out null elements

Added:
    
velocity/tools/trunk/velocity-tools-generic/src/test/java/org/apache/velocity/tools/generic/CollectionToolTests.java
Modified:
    
velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/CollectionTool.java

Modified: 
velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/CollectionTool.java
URL: 
http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/CollectionTool.java?rev=1834385&r1=1834384&r2=1834385&view=diff
==============================================================================
--- 
velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/CollectionTool.java
 (original)
+++ 
velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/CollectionTool.java
 Mon Jun 25 23:58:00 2018
@@ -216,7 +216,7 @@ public class CollectionTool extends Safe
     /**
      * Sorts a Collection using a Comparator. A defensive copy is made
      * of the Collection beforehand, so the original Collection is left
-     * untouched.
+     * untouched and null elements filtered out.
      *
      * @param c The Collection to sort.
      * @param comparator The comparator to use for sorting.
@@ -227,7 +227,18 @@ public class CollectionTool extends Safe
     public <T> Collection<T> sort(final Collection<T> c,
                                   final Comparator<T> comparator)
     {
-        final ArrayList<T> list = new ArrayList<>(c);
+        ArrayList<T> list = new ArrayList<>();
+        for (T elem : c)
+        {
+            if (elem != null)
+            {
+                list.add(elem);
+            }
+        }
+        if (list.size() < c.size())
+        {
+            getLog().warn("[collection] sort: null items have been filtered");
+        }
         Collections.sort(list, comparator);
         return list;
     }
@@ -235,7 +246,7 @@ public class CollectionTool extends Safe
     /**
      * Sorts an array using a Comparator. A defensive copy is made
      * of the array beforehand, so the original array is left
-     * untouched.
+     * untouched and null elements filtered out.
      *
      * @param a The array to sort.
      * @param comparator The comparator to use for sorting.
@@ -245,7 +256,20 @@ public class CollectionTool extends Safe
      */
     public <T> T[] sort(final T[] a, final Comparator<T> comparator)
     {
-        final T[] copy = a.clone();
+        int nulls = 0;
+        for (T t : a)
+        {
+            if (t == null)
+            {
+                ++nulls;
+            }
+        }
+        if (nulls > 0)
+        {
+            getLog().warn("[collection] sort: null items have been filtered 
out");
+        }
+        final T[] copy = Arrays.copyOf(a,a.length - nulls);
+        for (int from = 0, to = 0; from < a.length; ++from) if (a[from] != 
null) copy[to++] = a[from];
         Arrays.sort(copy, comparator);
         return copy;
     }
@@ -366,7 +390,18 @@ public class CollectionTool extends Safe
 
     public Collection sort(Collection collection, List properties)
     {
-        List<?> list = new ArrayList<>(collection);
+        List list = new ArrayList<>();
+        for (Object o : collection)
+        {
+            if (o != null)
+            {
+                list.add(o);
+            }
+        }
+        if (list.size() < collection.size())
+        {
+            getLog().warn("[collection] sort: null items have been filtered 
out");
+        }
         return internalSort(list, properties);
     }
 
@@ -377,7 +412,7 @@ public class CollectionTool extends Safe
 
     public Collection sort(Object[] array, List properties)
     {
-        return internalSort(Arrays.asList(array), properties);
+        return sort(Arrays.asList(array), properties);
     }
 
     protected Collection internalSort(List list, List properties)
@@ -387,7 +422,9 @@ public class CollectionTool extends Safe
             if (properties == null)
             {
                 Collections.sort(list);
-            } else {
+            }
+            else
+            {
                 Collections.sort(list, new PropertiesComparator(properties));
             }
             return list;

Added: 
velocity/tools/trunk/velocity-tools-generic/src/test/java/org/apache/velocity/tools/generic/CollectionToolTests.java
URL: 
http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-generic/src/test/java/org/apache/velocity/tools/generic/CollectionToolTests.java?rev=1834385&view=auto
==============================================================================
--- 
velocity/tools/trunk/velocity-tools-generic/src/test/java/org/apache/velocity/tools/generic/CollectionToolTests.java
 (added)
+++ 
velocity/tools/trunk/velocity-tools-generic/src/test/java/org/apache/velocity/tools/generic/CollectionToolTests.java
 Mon Jun 25 23:58:00 2018
@@ -0,0 +1,28 @@
+package org.apache.velocity.tools.generic;
+
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+
+import java.util.Arrays;
+import java.util.List;
+
+public class CollectionToolTests
+{
+  public @Test
+  void testNullElements()
+  {
+    CollectionTool tool = new CollectionTool();
+
+    List lst = Arrays.asList("b", null, "a");
+    List sorted = (List)tool.sort(lst);
+    assertEquals(2, sorted.size());
+    assertEquals("a", sorted.get(0));
+    assertEquals("b", sorted.get(1));
+
+    String arr[] = new String[] { "foo", null, "bar"};
+    sorted = (List)tool.sort(arr);
+    assertEquals(2, sorted.size());
+    assertEquals("bar", sorted.get(0));
+    assertEquals("foo", sorted.get(1));
+  }
+}


Reply via email to