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 ba8b34a2b7b028c9514bee3ce5abe99af310ddc5
Author: Gary D. Gregory <garydgreg...@gmail.com>
AuthorDate: Thu Jun 12 11:20:47 2025 -0400

    Refactor internals for some removeElement() methods
---
 .../java/org/apache/commons/lang3/ArrayUtils.java  | 77 ++++------------------
 .../java/org/apache/commons/lang3/ObjectUtils.java |  7 +-
 2 files changed, 15 insertions(+), 69 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java 
b/src/main/java/org/apache/commons/lang3/ArrayUtils.java
index 84fea2c87..c90f32ae0 100644
--- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java
@@ -1870,6 +1870,10 @@ public static int hashCode(final Object array) {
         return new HashCodeBuilder().append(array).toHashCode();
     }
 
+    static <K> void increment(final Map<K, MutableInt> occurrences, final K 
boxed) {
+        occurrences.computeIfAbsent(boxed, k -> new MutableInt()).increment();
+    }
+
     /**
      * Finds the indices of the given value in the array.
      * <p>
@@ -6086,13 +6090,7 @@ public static boolean[] removeElements(final boolean[] 
array, final boolean... v
         }
         final HashMap<Boolean, MutableInt> occurrences = new HashMap<>(2); // 
only two possible values here
         for (final boolean v : values) {
-            final Boolean boxed = Boolean.valueOf(v);
-            final MutableInt count = occurrences.get(boxed);
-            if (count == null) {
-                occurrences.put(boxed, new MutableInt(1));
-            } else {
-                count.increment();
-            }
+            increment(occurrences, Boolean.valueOf(v));
         }
         final BitSet toRemove = new BitSet();
         for (int i = 0; i < array.length; i++) {
@@ -6139,15 +6137,9 @@ public static byte[] removeElements(final byte[] array, 
final byte... values) {
         if (isEmpty(array) || isEmpty(values)) {
             return clone(array);
         }
-        final Map<Byte, MutableInt> occurrences = new HashMap<>(values.length);
+        final HashMap<Byte, MutableInt> occurrences = new 
HashMap<>(values.length);
         for (final byte v : values) {
-            final Byte boxed = Byte.valueOf(v);
-            final MutableInt count = occurrences.get(boxed);
-            if (count == null) {
-                occurrences.put(boxed, new MutableInt(1));
-            } else {
-                count.increment();
-            }
+            increment(occurrences, Byte.valueOf(v));
         }
         final BitSet toRemove = new BitSet();
         for (int i = 0; i < array.length; i++) {
@@ -6196,13 +6188,7 @@ public static char[] removeElements(final char[] array, 
final char... values) {
         }
         final HashMap<Character, MutableInt> occurrences = new 
HashMap<>(values.length);
         for (final char v : values) {
-            final Character boxed = Character.valueOf(v);
-            final MutableInt count = occurrences.get(boxed);
-            if (count == null) {
-                occurrences.put(boxed, new MutableInt(1));
-            } else {
-                count.increment();
-            }
+            increment(occurrences, Character.valueOf(v));
         }
         final BitSet toRemove = new BitSet();
         for (int i = 0; i < array.length; i++) {
@@ -6251,13 +6237,7 @@ public static double[] removeElements(final double[] 
array, final double... valu
         }
         final HashMap<Double, MutableInt> occurrences = new 
HashMap<>(values.length);
         for (final double v : values) {
-            final Double boxed = Double.valueOf(v);
-            final MutableInt count = occurrences.get(boxed);
-            if (count == null) {
-                occurrences.put(boxed, new MutableInt(1));
-            } else {
-                count.increment();
-            }
+            increment(occurrences, Double.valueOf(v));
         }
         final BitSet toRemove = new BitSet();
         for (int i = 0; i < array.length; i++) {
@@ -6306,13 +6286,7 @@ public static float[] removeElements(final float[] 
array, final float... values)
         }
         final HashMap<Float, MutableInt> occurrences = new 
HashMap<>(values.length);
         for (final float v : values) {
-            final Float boxed = Float.valueOf(v);
-            final MutableInt count = occurrences.get(boxed);
-            if (count == null) {
-                occurrences.put(boxed, new MutableInt(1));
-            } else {
-                count.increment();
-            }
+            increment(occurrences, Float.valueOf(v));
         }
         final BitSet toRemove = new BitSet();
         for (int i = 0; i < array.length; i++) {
@@ -6361,13 +6335,7 @@ public static int[] removeElements(final int[] array, 
final int... values) {
         }
         final HashMap<Integer, MutableInt> occurrences = new 
HashMap<>(values.length);
         for (final int v : values) {
-            final Integer boxed = Integer.valueOf(v);
-            final MutableInt count = occurrences.get(boxed);
-            if (count == null) {
-                occurrences.put(boxed, new MutableInt(1));
-            } else {
-                count.increment();
-            }
+            increment(occurrences, Integer.valueOf(v));
         }
         final BitSet toRemove = new BitSet();
         for (int i = 0; i < array.length; i++) {
@@ -6416,13 +6384,7 @@ public static long[] removeElements(final long[] array, 
final long... values) {
         }
         final HashMap<Long, MutableInt> occurrences = new 
HashMap<>(values.length);
         for (final long v : values) {
-            final Long boxed = Long.valueOf(v);
-            final MutableInt count = occurrences.get(boxed);
-            if (count == null) {
-                occurrences.put(boxed, new MutableInt(1));
-            } else {
-                count.increment();
-            }
+            increment(occurrences, Long.valueOf(v));
         }
         final BitSet toRemove = new BitSet();
         for (int i = 0; i < array.length; i++) {
@@ -6471,13 +6433,7 @@ public static short[] removeElements(final short[] 
array, final short... values)
         }
         final HashMap<Short, MutableInt> occurrences = new 
HashMap<>(values.length);
         for (final short v : values) {
-            final Short boxed = Short.valueOf(v);
-            final MutableInt count = occurrences.get(boxed);
-            if (count == null) {
-                occurrences.put(boxed, new MutableInt(1));
-            } else {
-                count.increment();
-            }
+            increment(occurrences, Short.valueOf(v));
         }
         final BitSet toRemove = new BitSet();
         for (int i = 0; i < array.length; i++) {
@@ -6528,12 +6484,7 @@ public static <T> T[] removeElements(final T[] array, 
final T... values) {
         }
         final HashMap<T, MutableInt> occurrences = new 
HashMap<>(values.length);
         for (final T v : values) {
-            final MutableInt count = occurrences.get(v);
-            if (count == null) {
-                occurrences.put(v, new MutableInt(1));
-            } else {
-                count.increment();
-            }
+            increment(occurrences, v);
         }
         final BitSet toRemove = new BitSet();
         for (int i = 0; i < array.length; i++) {
diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java 
b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
index bdd84ef5c..00b239ab5 100644
--- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
@@ -1185,12 +1185,7 @@ public static <T> T mode(final T... items) {
         if (ArrayUtils.isNotEmpty(items)) {
             final HashMap<T, MutableInt> occurrences = new 
HashMap<>(items.length);
             for (final T t : items) {
-                final MutableInt count = occurrences.get(t);
-                if (count == null) {
-                    occurrences.put(t, new MutableInt(1));
-                } else {
-                    count.increment();
-                }
+                ArrayUtils.increment(occurrences, t);
             }
             T result = null;
             int max = 0;

Reply via email to