Skip to content

Commit a4ba12e

Browse files
committed
org.apache.commons.lang3.EnumUtils.getFirstEnumIgnoreCase(Class<E>,
String, Function<E, String>, E) now returns the given default enum on null enumClass input - org.apache.commons.lang3.EnumUtils.getEnumIgnoreCase(Class<E>, String, E) now returns the given default enum on null enumClass input - org.apache.commons.lang3.EnumUtils.getEnumIgnoreCase(Class<E>, String) now returns the given default enum on null enumClass input
1 parent 76ec155 commit a4ba12e

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ The <action> type attribute can be add,update,fix,remove.
100100
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix edge-case NullPointerException in org.apache.commons.lang3.SystemUtils.isJavaVersionAtMost(JavaVersion).</action>
101101
<action type="fix" dev="ggregory" due-to="Gary Gregory">Return the default enum if a SecurityException is caught in getEnumSystemProperty().</action>
102102
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix edge-case NullPointerException in org.apache.commons.lang3.EnumUtils.getEnum(Class, String, E).</action>
103+
<action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.lang3.EnumUtils.getFirstEnumIgnoreCase(Class, String, Function, E) now returns the given default enum on null enumClass input.</action>
104+
<action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.lang3.EnumUtils.getEnumIgnoreCase(Class, String, E) now returns the given default enum on null enumClass input.</action>
105+
<action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.lang3.EnumUtils.getEnumIgnoreCase(Class, String) now returns the given default enum on null enumClass input.</action>
103106
<!-- ADD -->
104107
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Strings and refactor StringUtils.</action>
105108
<action issue="LANG-1747" type="add" dev="ggregory" due-to="Oliver B. Fischer, Gary Gregory">Add StopWatch.run([Failable]Runnable) and get([Failable]Supplier).</action>

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,10 @@ public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final Stri
231231
* <p>This method differs from {@link Enum#valueOf} in that it does not throw an exception
232232
* for an invalid enum name and performs case insensitive matching of the name.</p>
233233
*
234-
* @param <E> the type of the enumeration
235-
* @param enumClass the class of the enum to query, not null
236-
* @param enumName the enum name, null returns null
237-
* @return the enum, null if not found
234+
* @param <E> the type of the enumeration.
235+
* @param enumClass the class of the enum to query, may be null.
236+
* @param enumName the enum name, null returns null.
237+
* @return the enum, null if not found.
238238
* @since 3.8
239239
*/
240240
public static <E extends Enum<E>> E getEnumIgnoreCase(final Class<E> enumClass, final String enumName) {
@@ -247,11 +247,11 @@ public static <E extends Enum<E>> E getEnumIgnoreCase(final Class<E> enumClass,
247247
* <p>This method differs from {@link Enum#valueOf} in that it does not throw an exception
248248
* for an invalid enum name and performs case insensitive matching of the name.</p>
249249
*
250-
* @param <E> the type of the enumeration
251-
* @param enumClass the class of the enum to query, not null
252-
* @param enumName the enum name, null returns default enum
253-
* @param defaultEnum the default enum
254-
* @return the enum, default enum if not found
250+
* @param <E> the type of the enumeration.
251+
* @param enumClass the class of the enum to query, null returns default enum.
252+
* @param enumName the enum name, null returns default enum.
253+
* @param defaultEnum the default enum.
254+
* @return the enum, default enum if not found.
255255
* @since 3.10
256256
*/
257257
public static <E extends Enum<E>> E getEnumIgnoreCase(final Class<E> enumClass, final String enumName,
@@ -353,17 +353,17 @@ public static <E extends Enum<E>> E getFirstEnum(final Class<E> enumClass, final
353353
* <p>This method differs from {@link Enum#valueOf} in that it does not throw an exception
354354
* for an invalid enum name and performs case insensitive matching of the name.</p>
355355
*
356-
* @param <E> the type of the enumeration
357-
* @param enumClass the class of the enum to query, not null
358-
* @param enumName the enum name, null returns default enum
356+
* @param <E> the type of the enumeration.
357+
* @param enumClass the class of the enum to query, null returns default enum.
358+
* @param enumName the enum name, null returns default enum.
359359
* @param stringFunction the function that gets the string for an enum for comparison to {@code enumName}.
360-
* @param defaultEnum the default enum
361-
* @return an enum, default enum if not found
360+
* @param defaultEnum the default enum.
361+
* @return an enum, default enum if not found.
362362
* @since 3.13.0
363363
*/
364364
public static <E extends Enum<E>> E getFirstEnumIgnoreCase(final Class<E> enumClass, final String enumName, final Function<E, String> stringFunction,
365365
final E defaultEnum) {
366-
if (enumName == null || !enumClass.isEnum()) {
366+
if (enumName == null) {
367367
return defaultEnum;
368368
}
369369
return stream(enumClass).filter(e -> enumName.equalsIgnoreCase(stringFunction.apply(e))).findFirst().orElse(defaultEnum);
@@ -454,13 +454,13 @@ public static <E extends Enum<E>> EnumSet<E> processBitVectors(final Class<E> en
454454
* Returns a sequential ordered stream whose elements are the given class' enum values.
455455
*
456456
* @param <T> the type of stream elements.
457-
* @param clazz the class containing the enum values.
458-
* @return the new stream.
457+
* @param clazz the class containing the enum values, may be null.
458+
* @return the new stream, empty of {@code clazz} is null.
459459
* @since 3.18.0
460460
* @see Class#getEnumConstants()
461461
*/
462462
public static <T> Stream<T> stream(final Class<T> clazz) {
463-
return Streams.of(clazz.getEnumConstants());
463+
return clazz != null ? Streams.of(clazz.getEnumConstants()) : Stream.empty();
464464
}
465465

466466
/**

src/test/java/org/apache/commons/lang3/EnumUtilsTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ void testGetEnumIgnoreCase_defaultEnum() {
321321
assertEquals(Traffic.GREEN, EnumUtils.getEnumIgnoreCase(Traffic.class, null, Traffic.GREEN));
322322
assertEquals(Traffic.RED, EnumUtils.getEnumIgnoreCase(Traffic.class, null, Traffic.RED));
323323
assertNull(EnumUtils.getEnumIgnoreCase(Traffic.class, "PURPLE", null));
324+
assertNull(EnumUtils.getEnumIgnoreCase(null, "PURPLE", null));
324325
}
325326

326327
/**
@@ -336,7 +337,7 @@ void testGetEnumIgnoreCase_nonEnumClass() {
336337

337338
@Test
338339
void testGetEnumIgnoreCase_nullClass() {
339-
assertNullPointerException(() -> EnumUtils.getEnumIgnoreCase((Class<Traffic>) null, "PURPLE"));
340+
assertNull(EnumUtils.getEnumIgnoreCase((Class<Traffic>) null, "PURPLE"));
340341
}
341342

342343
@Test
@@ -440,6 +441,7 @@ void testGetFirstEnumIgnoreCase_defaultEnum() {
440441
assertEquals(Traffic2.GREEN, EnumUtils.getFirstEnumIgnoreCase(Traffic2.class, null, f, Traffic2.GREEN));
441442
assertEquals(Traffic2.RED, EnumUtils.getFirstEnumIgnoreCase(Traffic2.class, null, f, Traffic2.RED));
442443
assertNull(EnumUtils.getFirstEnumIgnoreCase(Traffic2.class, "PURPLE", f, null));
444+
assertNull(EnumUtils.getFirstEnumIgnoreCase(null, "PURPLE", f, null));
443445
}
444446

445447
@Test
@@ -482,7 +484,7 @@ void testIsValidEnumIgnoreCase() {
482484

483485
@Test
484486
void testIsValidEnumIgnoreCase_nullClass() {
485-
assertNullPointerException(() -> EnumUtils.isValidEnumIgnoreCase(null, "PURPLE"));
487+
assertFalse(EnumUtils.isValidEnumIgnoreCase(null, "PURPLE"));
486488
}
487489

488490
@Test
@@ -613,6 +615,7 @@ void testStream() {
613615
assertEquals(7, EnumUtils.stream(TimeUnit.class).count());
614616
Assertions.assertArrayEquals(TimeUnit.values(), EnumUtils.stream(TimeUnit.class).toArray(TimeUnit[]::new));
615617
assertEquals(0, EnumUtils.stream(Object.class).count());
618+
assertEquals(0, EnumUtils.stream(null).count());
616619
}
617620

618621
}

0 commit comments

Comments
 (0)