biya-bi commented on code in PR #696:
URL: https://github.com/apache/commons-lang/pull/696#discussion_r1419770544
##########
src/main/java/org/apache/commons/lang3/EnumUtils.java:
##########
@@ -264,6 +266,35 @@ public static <E extends Enum<E>> E
getEnumIgnoreCase(final Class<E> enumClass,
return defaultEnum;
}
+ /**
+ * <p>Tries to find the enum value for the class matching the given
predicate. The first hit will be returned.</p>
+ *
+ * <p>This method allows the usage of arbitrary predicates instead of only
enum names to get the enum value.
+ * It returns an empty optional if no value matches, or any input is
null.</p>
+ *
+ * <p>If multiple values match the predicate only the first one
encountered is returned.</p>
+ *
+ * <p>A typical usage example would be finding an enum based on additional
information stored inside the enum,
+ * or context information regarding the enum values.</p>
+ *
+ * @param <E> the type of the enumeration
+ * @param enumClass the class of the enum to query, not null
+ * @param predicate the predicate used to test for the value, not null
+ * @return an Optional containing the enum or an empty optional if none
was found, or any input is invalid
+ * @since 3.12
+ */
+ public static <E extends Enum<E>> Optional<E> getEnumByPredicate(final
Class<E> enumClass, final Predicate<E> predicate) {
+ if (predicate == null || enumClass == null || !enumClass.isEnum()) {
+ return Optional.empty();
+ }
Review Comment:
I however agree that the bad parameters should be reported with an exception.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]