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

commit 8e1df6b9bf6afc0e3f3a99d4d99bb9d78d148c81
Author: Gary Gregory <[email protected]>
AuthorDate: Mon Oct 25 09:34:31 2021 -0400

    Add ComparableUtils.max(A, A) and ComparableUtils.min(A, A).
---
 src/changes/changes.xml                            |  3 +-
 .../java/org/apache/commons/lang3/ObjectUtils.java |  4 +++
 .../commons/lang3/compare/ComparableUtils.java     | 36 ++++++++++++++++++++++
 .../commons/lang3/compare/ComparableUtilsTest.java | 34 ++++++++++++++++++++
 4 files changed, 76 insertions(+), 1 deletion(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 31d424a..50cd851 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -90,7 +90,8 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add ObjectUtils.identityHashCodeHex(Object).</action>
     <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add ObjectUtils.hashCodeHex(Object).</action>
     <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add StringUtils.removeStart(String, char).</action>
-    <action issue="LANG-1659" type="fix" dev="ggregory" due-to="Arturo Bernal, 
Gary Gregory">Add null-safe ObjectUtils.isArray() #754.</action>
+    <action issue="LANG-1659" type="add" dev="ggregory" due-to="Arturo Bernal, 
Gary Gregory">Add null-safe ObjectUtils.isArray() #754.</action>
+    <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add ComparableUtils.max(A, A) and ComparableUtils.min(A, A).</action>
     <!-- UPDATE -->
     <action                   type="update" dev="ggregory" due-to="Dependabot, 
Gary Gregory">Bump spotbugs-maven-plugin from 4.2.0 to 4.4.1 #735, 
#808.</action>
     <action                   type="update" dev="ggregory" due-to="Dependabot, 
XenoAmess">Bump Bump actions/cache from v2.1.4 to v2.1.6 #742, #752, 
#764.</action>
diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java 
b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
index b240cf6..feb124a 100644
--- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
@@ -299,6 +299,7 @@ public class ObjectUtils {
     /**
      * <p>Null safe comparison of Comparables.
      * {@code null} is assumed to be less than a non-{@code null} value.</p>
+     * <p>TODO Move to ComparableUtils.</p>
      *
      * @param <T> type of the values processed by this method
      * @param c1  the first comparable, may be null
@@ -312,6 +313,7 @@ public class ObjectUtils {
 
     /**
      * <p>Null safe comparison of Comparables.</p>
+     * <p>TODO Move to ComparableUtils.</p>
      *
      * @param <T> type of the values processed by this method
      * @param c1  the first comparable, may be null
@@ -1081,6 +1083,7 @@ public class ObjectUtils {
 
     /**
      * <p>Null safe comparison of Comparables.</p>
+     * <p>TODO Move to ComparableUtils.</p>
      *
      * @param <T> type of the values processed by this method
      * @param values the set of comparable values, may be null
@@ -1151,6 +1154,7 @@ public class ObjectUtils {
 
     /**
      * <p>Null safe comparison of Comparables.</p>
+     * <p>TODO Move to ComparableUtils.</p>
      *
      * @param <T> type of the values processed by this method
      * @param values the set of comparable values, may be null
diff --git 
a/src/main/java/org/apache/commons/lang3/compare/ComparableUtils.java 
b/src/main/java/org/apache/commons/lang3/compare/ComparableUtils.java
index c757a52..eb7038e 100644
--- a/src/main/java/org/apache/commons/lang3/compare/ComparableUtils.java
+++ b/src/main/java/org/apache/commons/lang3/compare/ComparableUtils.java
@@ -18,6 +18,8 @@ package org.apache.commons.lang3.compare;
 
 import java.util.function.Predicate;
 
+import org.apache.commons.lang3.ObjectUtils;
+
 /**
  * <p>Utility library to provide helper methods for translating {@link 
Comparable#compareTo} result into a boolean.</p>
  *
@@ -204,5 +206,39 @@ public class ComparableUtils {
         return a -> is(a).lessThan(b);
     }
 
+    /**
+     * Returns the greater of two {@code Comparable} values, ignoring null.
+     * <p>
+     * For three or more values, use {@link ObjectUtils#max(Comparable...)}.
+     * </p>
+     *
+     * @param <A> Type of what we are comparing.
+     * @param comparable1 an argument.
+     * @param comparable2 another argument.
+     * @return the larger of {@code c1} and {@code c2}.
+     * @see ObjectUtils#max(Comparable...)
+     * @since 3.13.0
+     */
+    public static <A extends Comparable<A>> A max(A comparable1, A 
comparable2) {
+        return ObjectUtils.compare(comparable1, comparable2, false) > 0 ? 
comparable1 : comparable2;
+    }
+
+    /**
+     * Returns the lesser of two {@code Comparable} values, ignoring null.
+     * <p>
+     * For three or more values, use {@link ObjectUtils#min(Comparable...)}.
+     * </p>
+     *
+     * @param <A> Type of what we are comparing.
+     * @param comparable1 an argument.
+     * @param comparable2 another argument.
+     * @return the larger of {@code c1} and {@code c2}.
+     * @see ObjectUtils#min(Comparable...)
+     * @since 3.13.0
+     */
+    public static <A extends Comparable<A>> A min(A comparable1, A 
comparable2) {
+        return ObjectUtils.compare(comparable1, comparable2, true) < 0 ? 
comparable1 : comparable2;
+    }
+
     private ComparableUtils() {}
 }
diff --git 
a/src/test/java/org/apache/commons/lang3/compare/ComparableUtilsTest.java 
b/src/test/java/org/apache/commons/lang3/compare/ComparableUtilsTest.java
index 2b391b6..a55df2d 100644
--- a/src/test/java/org/apache/commons/lang3/compare/ComparableUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/compare/ComparableUtilsTest.java
@@ -17,10 +17,12 @@
 package org.apache.commons.lang3.compare;
 
 import static org.apache.commons.lang3.compare.ComparableUtils.is;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.math.BigDecimal;
+import java.time.Instant;
 
 import org.junit.jupiter.api.DisplayName;
 import org.junit.jupiter.api.DisplayNameGeneration;
@@ -285,4 +287,36 @@ public class ComparableUtilsTest {
 
         BigDecimal a = BigDecimal.ONE;
     }
+
+    @Test
+    public void testMax() {
+        assertEquals(Instant.MAX, ComparableUtils.max(Instant.MAX, 
Instant.MAX));
+        assertEquals(Instant.MIN, ComparableUtils.max(Instant.MIN, 
Instant.MIN));
+        assertEquals(Instant.MAX, ComparableUtils.max(Instant.MIN, 
Instant.MAX));
+        assertEquals(Instant.MAX, ComparableUtils.max(Instant.MAX, 
Instant.MIN));
+        //
+        assertEquals(Integer.MIN_VALUE, 
ComparableUtils.max(Integer.valueOf(Integer.MIN_VALUE), 
Integer.valueOf(Integer.MIN_VALUE)));
+        assertEquals(Integer.MAX_VALUE, 
ComparableUtils.max(Integer.valueOf(Integer.MAX_VALUE), 
Integer.valueOf(Integer.MAX_VALUE)));
+        assertEquals(Integer.MAX_VALUE, 
ComparableUtils.max(Integer.valueOf(Integer.MIN_VALUE), 
Integer.valueOf(Integer.MAX_VALUE)));
+        assertEquals(Integer.MAX_VALUE, 
ComparableUtils.max(Integer.valueOf(Integer.MAX_VALUE), 
Integer.valueOf(Integer.MIN_VALUE)));
+        //
+        assertEquals(Instant.MAX, ComparableUtils.max(null, Instant.MAX));
+        assertEquals(Instant.MAX, ComparableUtils.max(Instant.MAX, null));
+    }
+
+    @Test
+    public void testMin() {
+        assertEquals(Instant.MAX, ComparableUtils.min(Instant.MAX, 
Instant.MAX));
+        assertEquals(Instant.MIN, ComparableUtils.min(Instant.MIN, 
Instant.MIN));
+        assertEquals(Instant.MIN, ComparableUtils.min(Instant.MIN, 
Instant.MAX));
+        assertEquals(Instant.MIN, ComparableUtils.min(Instant.MAX, 
Instant.MIN));
+        //
+        assertEquals(Integer.MIN_VALUE, 
ComparableUtils.min(Integer.valueOf(Integer.MIN_VALUE), 
Integer.valueOf(Integer.MIN_VALUE)));
+        assertEquals(Integer.MAX_VALUE, 
ComparableUtils.min(Integer.valueOf(Integer.MAX_VALUE), 
Integer.valueOf(Integer.MAX_VALUE)));
+        assertEquals(Integer.MIN_VALUE, 
ComparableUtils.min(Integer.valueOf(Integer.MIN_VALUE), 
Integer.valueOf(Integer.MAX_VALUE)));
+        assertEquals(Integer.MIN_VALUE, 
ComparableUtils.min(Integer.valueOf(Integer.MAX_VALUE), 
Integer.valueOf(Integer.MIN_VALUE)));
+        //
+        assertEquals(Instant.MAX, ComparableUtils.min(null, Instant.MAX));
+        assertEquals(Instant.MAX, ComparableUtils.min(Instant.MAX, null));
+    }
 }

Reply via email to