Skip to content

Commit ba8b34a

Browse files
committed
Refactor internals for some removeElement() methods
1 parent 377fb06 commit ba8b34a

File tree

2 files changed

+15
-69
lines changed

2 files changed

+15
-69
lines changed

src/main/java/org/apache/commons/lang3/ArrayUtils.java

Lines changed: 14 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,6 +1870,10 @@ public static int hashCode(final Object array) {
18701870
return new HashCodeBuilder().append(array).toHashCode();
18711871
}
18721872

1873+
static <K> void increment(final Map<K, MutableInt> occurrences, final K boxed) {
1874+
occurrences.computeIfAbsent(boxed, k -> new MutableInt()).increment();
1875+
}
1876+
18731877
/**
18741878
* Finds the indices of the given value in the array.
18751879
* <p>
@@ -6086,13 +6090,7 @@ public static boolean[] removeElements(final boolean[] array, final boolean... v
60866090
}
60876091
final HashMap<Boolean, MutableInt> occurrences = new HashMap<>(2); // only two possible values here
60886092
for (final boolean v : values) {
6089-
final Boolean boxed = Boolean.valueOf(v);
6090-
final MutableInt count = occurrences.get(boxed);
6091-
if (count == null) {
6092-
occurrences.put(boxed, new MutableInt(1));
6093-
} else {
6094-
count.increment();
6095-
}
6093+
increment(occurrences, Boolean.valueOf(v));
60966094
}
60976095
final BitSet toRemove = new BitSet();
60986096
for (int i = 0; i < array.length; i++) {
@@ -6139,15 +6137,9 @@ public static byte[] removeElements(final byte[] array, final byte... values) {
61396137
if (isEmpty(array) || isEmpty(values)) {
61406138
return clone(array);
61416139
}
6142-
final Map<Byte, MutableInt> occurrences = new HashMap<>(values.length);
6140+
final HashMap<Byte, MutableInt> occurrences = new HashMap<>(values.length);
61436141
for (final byte v : values) {
6144-
final Byte boxed = Byte.valueOf(v);
6145-
final MutableInt count = occurrences.get(boxed);
6146-
if (count == null) {
6147-
occurrences.put(boxed, new MutableInt(1));
6148-
} else {
6149-
count.increment();
6150-
}
6142+
increment(occurrences, Byte.valueOf(v));
61516143
}
61526144
final BitSet toRemove = new BitSet();
61536145
for (int i = 0; i < array.length; i++) {
@@ -6196,13 +6188,7 @@ public static char[] removeElements(final char[] array, final char... values) {
61966188
}
61976189
final HashMap<Character, MutableInt> occurrences = new HashMap<>(values.length);
61986190
for (final char v : values) {
6199-
final Character boxed = Character.valueOf(v);
6200-
final MutableInt count = occurrences.get(boxed);
6201-
if (count == null) {
6202-
occurrences.put(boxed, new MutableInt(1));
6203-
} else {
6204-
count.increment();
6205-
}
6191+
increment(occurrences, Character.valueOf(v));
62066192
}
62076193
final BitSet toRemove = new BitSet();
62086194
for (int i = 0; i < array.length; i++) {
@@ -6251,13 +6237,7 @@ public static double[] removeElements(final double[] array, final double... valu
62516237
}
62526238
final HashMap<Double, MutableInt> occurrences = new HashMap<>(values.length);
62536239
for (final double v : values) {
6254-
final Double boxed = Double.valueOf(v);
6255-
final MutableInt count = occurrences.get(boxed);
6256-
if (count == null) {
6257-
occurrences.put(boxed, new MutableInt(1));
6258-
} else {
6259-
count.increment();
6260-
}
6240+
increment(occurrences, Double.valueOf(v));
62616241
}
62626242
final BitSet toRemove = new BitSet();
62636243
for (int i = 0; i < array.length; i++) {
@@ -6306,13 +6286,7 @@ public static float[] removeElements(final float[] array, final float... values)
63066286
}
63076287
final HashMap<Float, MutableInt> occurrences = new HashMap<>(values.length);
63086288
for (final float v : values) {
6309-
final Float boxed = Float.valueOf(v);
6310-
final MutableInt count = occurrences.get(boxed);
6311-
if (count == null) {
6312-
occurrences.put(boxed, new MutableInt(1));
6313-
} else {
6314-
count.increment();
6315-
}
6289+
increment(occurrences, Float.valueOf(v));
63166290
}
63176291
final BitSet toRemove = new BitSet();
63186292
for (int i = 0; i < array.length; i++) {
@@ -6361,13 +6335,7 @@ public static int[] removeElements(final int[] array, final int... values) {
63616335
}
63626336
final HashMap<Integer, MutableInt> occurrences = new HashMap<>(values.length);
63636337
for (final int v : values) {
6364-
final Integer boxed = Integer.valueOf(v);
6365-
final MutableInt count = occurrences.get(boxed);
6366-
if (count == null) {
6367-
occurrences.put(boxed, new MutableInt(1));
6368-
} else {
6369-
count.increment();
6370-
}
6338+
increment(occurrences, Integer.valueOf(v));
63716339
}
63726340
final BitSet toRemove = new BitSet();
63736341
for (int i = 0; i < array.length; i++) {
@@ -6416,13 +6384,7 @@ public static long[] removeElements(final long[] array, final long... values) {
64166384
}
64176385
final HashMap<Long, MutableInt> occurrences = new HashMap<>(values.length);
64186386
for (final long v : values) {
6419-
final Long boxed = Long.valueOf(v);
6420-
final MutableInt count = occurrences.get(boxed);
6421-
if (count == null) {
6422-
occurrences.put(boxed, new MutableInt(1));
6423-
} else {
6424-
count.increment();
6425-
}
6387+
increment(occurrences, Long.valueOf(v));
64266388
}
64276389
final BitSet toRemove = new BitSet();
64286390
for (int i = 0; i < array.length; i++) {
@@ -6471,13 +6433,7 @@ public static short[] removeElements(final short[] array, final short... values)
64716433
}
64726434
final HashMap<Short, MutableInt> occurrences = new HashMap<>(values.length);
64736435
for (final short v : values) {
6474-
final Short boxed = Short.valueOf(v);
6475-
final MutableInt count = occurrences.get(boxed);
6476-
if (count == null) {
6477-
occurrences.put(boxed, new MutableInt(1));
6478-
} else {
6479-
count.increment();
6480-
}
6436+
increment(occurrences, Short.valueOf(v));
64816437
}
64826438
final BitSet toRemove = new BitSet();
64836439
for (int i = 0; i < array.length; i++) {
@@ -6528,12 +6484,7 @@ public static <T> T[] removeElements(final T[] array, final T... values) {
65286484
}
65296485
final HashMap<T, MutableInt> occurrences = new HashMap<>(values.length);
65306486
for (final T v : values) {
6531-
final MutableInt count = occurrences.get(v);
6532-
if (count == null) {
6533-
occurrences.put(v, new MutableInt(1));
6534-
} else {
6535-
count.increment();
6536-
}
6487+
increment(occurrences, v);
65376488
}
65386489
final BitSet toRemove = new BitSet();
65396490
for (int i = 0; i < array.length; i++) {

src/main/java/org/apache/commons/lang3/ObjectUtils.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,12 +1185,7 @@ public static <T> T mode(final T... items) {
11851185
if (ArrayUtils.isNotEmpty(items)) {
11861186
final HashMap<T, MutableInt> occurrences = new HashMap<>(items.length);
11871187
for (final T t : items) {
1188-
final MutableInt count = occurrences.get(t);
1189-
if (count == null) {
1190-
occurrences.put(t, new MutableInt(1));
1191-
} else {
1192-
count.increment();
1193-
}
1188+
ArrayUtils.increment(occurrences, t);
11941189
}
11951190
T result = null;
11961191
int max = 0;

0 commit comments

Comments
 (0)