velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From schu...@apache.org
Subject svn commit: r1432349 - /velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/SortTool.java
Date Sat, 12 Jan 2013 01:13:20 GMT
Author: schultz
Date: Sat Jan 12 01:13:19 2013
New Revision: 1432349

URL: http://svn.apache.org/viewvc?rev=1432349&view=rev
Log:
Forward-ported r1432348:

Fix https://issues.apache.org/jira/browse/VELTOOLS-156

Added sort(Collection, Comparator) method.
Added sort(Object[], Comparator) method.
Added sort(Map, Comparator) method.
Added sort(Object, Comparator) method.

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

Modified: velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/SortTool.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/SortTool.java?rev=1432349&r1=1432348&r2=1432349&view=diff
==============================================================================
--- velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/SortTool.java
(original)
+++ velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/SortTool.java
Sat Jan 12 01:13:19 2013
@@ -115,6 +115,94 @@ import org.apache.velocity.tools.config.
 @DefaultKey("sorter")
 public class SortTool
 {
+    /**
+     * Sorts a Collection using a Comparator. A defensive copy is made
+     * of the Collection beforehand, so the original Collection is left
+     * untouched.
+     *
+     * @param c The Collection to sort.
+     * @param comparator The comparator to use for sorting.
+     * @return A copy of the original Collection,
+     *         sorted using the supplied Comparator.
+     * @since VelocityTools 2.0.1
+     */
+    public <T> Collection<T> sort(final Collection<T> c,
+                                  final Comparator<T> comparator)
+    {
+        final ArrayList<T> list = new ArrayList<T>(c);
+        Collections.sort(list, comparator);
+        return list;
+    }
+
+    /**
+     * Sorts an array using a Comparator. A defensive copy is made
+     * of the array beforehand, so the original array is left
+     * untouched.
+     *
+     * @param a The array to sort.
+     * @param comparator The comparator to use for sorting.
+     * @return A copy of the original array,
+     *         sorted using the supplied Comparator.
+     * @since VelocityTools 2.0.1
+     */
+    public <T> T[] sort(final T[] a, final Comparator<T> comparator)
+    {
+        final T[] copy = a.clone();
+        Arrays.sort(copy, comparator);
+        return copy;
+    }
+
+    /**
+     * Sorts a Map's values using a Comparator. A defensive copy is made
+     * of the values beforehand, so the original Map is left
+     * untouched.
+     *
+     * @param map The Map whose values should be sorted.
+     * @param comparator The comparator to use for sorting.
+     * @return A copy of the original Map's values,
+     *         sorted using the supplied Comparator.
+     * @since VelocityTools 2.0.1
+     */
+    public <T> Collection<T> sort(final Map<?,T> map,
+                                  final Comparator<T> comparator)
+    {
+        return sort(map.values(), comparator);
+    }
+
+    /**
+     * Sorts a Collection (or array, or Map's values)
+     * using a Comparator. A defensive copy is made
+     * of the original beforehand, so the original is left
+     * untouched. Unsupported collection objects result in
+     * a <code>null</code> return value.
+     *
+     * @param o The Collection to sort.
+     * @param comparator The comparator to use for sorting.
+     * @return A copy of the original Collection,
+     *         sorted using the supplied Comparator.
+     * @since VelocityTools 2.0.1
+     */
+    public Collection<?> sort(final Object o,
+                              final Comparator<?> comparator)
+    {
+        if (o instanceof Collection)
+        {
+            return sort((Collection<?>)o, comparator);
+        }
+        else if (o instanceof Object[])
+        {
+            return sort((Object[])o, comparator);
+        }
+        else if (o instanceof Map)
+        {
+            return sort((Map<?,?>)o, comparator);
+        }
+        else
+        {
+            // the object type is not supported
+            return null;
+        }
+    }
 
     public Collection sort(Collection collection)
     {



Mime
View raw message