You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/11/15 17:08:36 UTC

[commons-lang] branch master updated: Add fluent-style ArrayUtils.sort(Object[], Comparable).

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

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new 3bda8be  Add fluent-style ArrayUtils.sort(Object[], Comparable).
3bda8be is described below

commit 3bda8be03638b5248df2252832388775601692ff
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Nov 15 12:08:32 2020 -0500

    Add fluent-style ArrayUtils.sort(Object[], Comparable).
---
 src/changes/changes.xml                                  |  2 +-
 src/main/java/org/apache/commons/lang3/ArrayUtils.java   | 16 ++++++++++++++++
 .../java/org/apache/commons/lang3/ArrayUtilsTest.java    |  7 +++++++
 .../org/apache/commons/lang3/reflect/TypeUtilsTest.java  |  2 +-
 4 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 9d070a5..ef11ac6 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -59,7 +59,6 @@ The <action> type attribute can be add,update,fix,remove.
     <action issue="LANG-1541" type="fix" dev="ggregory" due-to="Arturo Bernal, Gary Gregory">ArrayUtils.contains() and indexOf() fails to handle Double.NaN #647.</action>
     <action                   type="fix" dev="ggregory" due-to="Gary Gregory">Fix potential NPE in TypeUtils.isAssignable(Type, ParameterizedType, Map, Type>).</action>
     <action issue="LANG-1420" type="fix" dev="ggregory" due-to="Gordon Fraser, Rostislav Krasny, Arturo Bernal, Gary Gregory">TypeUtils.isAssignable returns wrong result for GenericArrayType and ParameterizedType, #643.</action>
-    
     <!--  ADDS -->
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add BooleanUtils.booleanValues().</action>
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add BooleanUtils.primitiveValues().</action>
@@ -68,6 +67,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="add" dev="ggregory" due-to="Edgar Asatryan">More test coverage for CharSequenceUtils. #631.</action>
     <action issue="LANG-1596" type="update" dev="aherbert" due-to="Richard Eckart de Castilho">ArrayUtils.toPrimitive(Object) does not support boolean and other types #607.</action>
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add fluent-style ArrayUtils.sort(Object[]).</action>
+    <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add fluent-style ArrayUtils.sort(Object[], Comparable).</action>
     <!--  UPDATES -->
     <action                   type="update" dev="ggregory" due-to="Gary Gregory">Enable Dependabot #587.</action>
     <action                   type="update" dev="chtompki">Bump junit-jupiter from 5.6.2 to 5.7.0.</action>
diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java
index ccc9698..cee5f3d 100644
--- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java
@@ -7873,6 +7873,22 @@ public static int indexOf(final int[] array, final int valueToFind, int startInd
     }
 
     /**
+     * Sorts and returns the given array.
+     *
+     * @param <T> the array type.
+     * @param array the array to sort.
+     * @param comparator the comparator to determine the order of the array.  
+     *        A {@code null} value uses the elements' {@link Comparable natural ordering}.
+     * @return the given array.
+     * @see Arrays#sort(Object[])
+     * @since 3.12
+     */
+    public static <T> T[] sort(T[] array, Comparator<? super T> comparator) {
+        Arrays.sort(array, comparator);
+        return array;
+    }
+    
+    /**
      * <p>Produces a new {@code boolean} array containing the elements
      * between the start and end indices.
      *
diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
index 5b5f889..99ea44d 100644
--- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
@@ -5120,6 +5120,13 @@ public class ArrayUtilsTest {
         assertEquals(array1, ArrayUtils.sort(array2));
     }
 
+    public void testSortComparable() {
+        final String[] array1 = ArrayUtils.toArray("foo", "bar");
+        final String[] array2 = array1.clone();
+        Arrays.sort(array1);
+        assertEquals(array1, ArrayUtils.sort(array2, String::compareTo));
+    }
+
     @Test
     public void testSubarrayBoolean() {
         final boolean[] nullArray = null;
diff --git a/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java b/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java
index 8cdce0b..a3711bc 100644
--- a/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java
@@ -790,7 +790,7 @@ public class TypeUtilsTest<B> {
         assertFalse(paramType.getClass().isAssignableFrom(WildcardType.class));
 
         WildcardType testType = TypeUtils.WILDCARD_ALL;
-        // TODO This test returns true unlike the test above.
+        // TODO This test returns true unlike the test above. 
         // Is this a bug in this test or in the main code?
         assertFalse(TypeUtils.isAssignable(paramType, testType),
                 () -> String.format("TypeUtils.isAssignable(%s, %s)", paramType, testType));