From commits-return-3300-archive-asf-public=cust-asf.ponee.io@velocity.apache.org Tue Jun 26 01:58:02 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id 0FE56180627 for ; Tue, 26 Jun 2018 01:58:01 +0200 (CEST) Received: (qmail 41673 invoked by uid 500); 25 Jun 2018 23:58:01 -0000 Mailing-List: contact commits-help@velocity.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@velocity.apache.org Delivered-To: mailing list commits@velocity.apache.org Received: (qmail 41664 invoked by uid 99); 25 Jun 2018 23:58:01 -0000 Received: from Unknown (HELO svn01-us-west.apache.org) (209.188.14.144) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 25 Jun 2018 23:58:01 +0000 Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id A98253A0220 for ; Mon, 25 Jun 2018 23:58:00 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1834385 - in /velocity/tools/trunk/velocity-tools-generic/src: main/java/org/apache/velocity/tools/generic/CollectionTool.java test/java/org/apache/velocity/tools/generic/CollectionToolTests.java Date: Mon, 25 Jun 2018 23:58:00 -0000 To: commits@velocity.apache.org From: cbrisson@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20180625235800.A98253A0220@svn01-us-west.apache.org> 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 Collection sort(final Collection c, final Comparator comparator) { - final ArrayList list = new ArrayList<>(c); + ArrayList 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[] sort(final T[] a, final Comparator 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)); + } +}