[
https://issues.apache.org/jira/browse/LANG-1639?focusedWorklogId=534809&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-534809
]
ASF GitHub Bot logged work on LANG-1639:
----------------------------------------
Author: ASF GitHub Bot
Created on: 12/Jan/21 12:20
Start Date: 12/Jan/21 12:20
Worklog Time Spent: 10m
Work Description: bonii-xx commented on a change in pull request #696:
URL: https://github.com/apache/commons-lang/pull/696#discussion_r555730226
##########
File path: src/test/java/org/apache/commons/lang3/EnumUtilsTest.java
##########
@@ -163,6 +165,26 @@ public void test_getEnumIgnoreCase_defaultEnum() {
assertNull(EnumUtils.getEnumIgnoreCase(Traffic.class, "PURPLE", null));
}
+ @Test
+ public void test_getEnumByPredicate() {
+ assertEquals(WithExtraData.A,
EnumUtils.getEnumByPredicate(WithExtraData.class, WithExtraData::isA).get());
+ assertNotEquals(WithExtraData.A,
EnumUtils.getEnumByPredicate(WithExtraData.class, WithExtraData::isNotA).get());
+ assertEquals(WithExtraData.A,
EnumUtils.getEnumByPredicate(WithExtraData.class, value -> true).get());
+ assertEquals(Optional.empty(),
EnumUtils.getEnumByPredicate(WithExtraData.class, value -> false));
+ }
+
+ @Test
+ public void test_getEnumByPredicate_nullInputs() {
+ assertFalse(EnumUtils.getEnumByPredicate(null,
WithExtraData::isA).isPresent());
+ assertFalse(EnumUtils.getEnumByPredicate(WithExtraData.class,
null).isPresent());
+ }
+
+ @Test
+ public void test_getEnumByPredicate_nonEnumClass() {
+ final Class rawType = Object.class;
+ assertFalse(EnumUtils.getEnumByPredicate(rawType, null).isPresent());
Review comment:
Ah, I misread the comment. Yes you're right, the test is faulty.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 534809)
Time Spent: 50m (was: 40m)
> Find enum using a predicate
> ---------------------------
>
> Key: LANG-1639
> URL: https://issues.apache.org/jira/browse/LANG-1639
> Project: Commons Lang
> Issue Type: New Feature
> Components: lang.enums.*
> Reporter: Bernhard Bonigl
> Priority: Major
> Fix For: 3.12
>
> Time Spent: 50m
> Remaining Estimate: 0h
>
> `EnumUtils` supports finding an enum based on its name. However in my
> experience it's often required to find an enum mapped by another value
> related to the enum. To use a very simple example: Mapping a string input to
> an enum, where the string does not match the enum name.
> I propose adding a `getEnumByPredicate` method, that returns the first found
> enum value matching a given predicate. Could also be expanded to return a
> list of all matching values.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)