You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2021/01/11 09:22:47 UTC

[GitHub] [commons-lang] bonii-xx opened a new pull request #696: LANG-1639 Add getEnumByPredicate

bonii-xx opened a new pull request #696:
URL: https://github.com/apache/commons-lang/pull/696


    Adds a method to obtain a matching enum value based on a predicate. Quite useful when mapping values to enums.
   
   I used Optional as the return type since it allows the removal of a default value without removing any functionality, the caller has to handle the returned value if it's not found.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-lang] coveralls commented on pull request #696: LANG-1639 Add getEnumByPredicate

Posted by GitBox <gi...@apache.org>.
coveralls commented on pull request #696:
URL: https://github.com/apache/commons-lang/pull/696#issuecomment-757899294


   
   [![Coverage Status](https://coveralls.io/builds/36227912/badge)](https://coveralls.io/builds/36227912)
   
   Coverage decreased (-0.01%) to 94.97% when pulling **e049c91d69c3f1c9731f378278428b24537ccdbb on bonii-xx:LANG-1639-find-enum-by-predicate** into **ddae5508483084badc5c9328efae0f2e50ef09b5 on apache:master**.
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-lang] garydgregory commented on a change in pull request #696: LANG-1639 Add getEnumByPredicate

Posted by GitBox <gi...@apache.org>.
garydgregory commented on a change in pull request #696:
URL: https://github.com/apache/commons-lang/pull/696#discussion_r555244274



##########
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:
       This test does not cover checking the class' enum aspect because the predicate is null.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-lang] bonii-xx commented on a change in pull request #696: LANG-1639 Add getEnumByPredicate

Posted by GitBox <gi...@apache.org>.
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:
users@infra.apache.org



[GitHub] [commons-lang] coveralls edited a comment on pull request #696: LANG-1639 Add getEnumByPredicate

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #696:
URL: https://github.com/apache/commons-lang/pull/696#issuecomment-757899294


   
   [![Coverage Status](https://coveralls.io/builds/36268119/badge)](https://coveralls.io/builds/36268119)
   
   Coverage increased (+0.002%) to 94.983% when pulling **f6d4420c345ff3cb1790af988882aad6c792884d on bonii-xx:LANG-1639-find-enum-by-predicate** into **c7c74e936db04afeef3af431a597b16f923f6f03 on apache:master**.
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-lang] bonii-xx commented on a change in pull request #696: LANG-1639 Add getEnumByPredicate

Posted by GitBox <gi...@apache.org>.
bonii-xx commented on a change in pull request #696:
URL: https://github.com/apache/commons-lang/pull/696#discussion_r555729693



##########
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:
       Thanks, had it named differently before, but decided to align the naming with the other enum tests 🤦 




----------------------------------------------------------------
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:
users@infra.apache.org