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/03/14 21:35:18 UTC

[GitHub] [commons-lang] MaxwellCody opened a new pull request #730: EnumUtils: Add more-generic getEnumMap() method.

MaxwellCody opened a new pull request #730:
URL: https://github.com/apache/commons-lang/pull/730


   The method accepts a Function object used to supply keys for the map.
   
   The behavior of the single-parameter getEnumMap() can be emulated with this
   method by using a reference to the Enum::name method. For example,
   
      EnumUtils.getEnumMap(Traffic.class);
   
   would become
   
      EnumUtils.getEnumMap(Traffic.class, Traffic::name);
   
   and the resulting map would be identical.


----------------------------------------------------------------
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 pull request #730: EnumUtils: Add more-generic getEnumMap() method.

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


   I pushed a different implementation to 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.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [commons-lang] aherbert commented on a change in pull request #730: EnumUtils: Add more-generic getEnumMap() method.

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



##########
File path: src/main/java/org/apache/commons/lang3/EnumUtils.java
##########
@@ -294,6 +295,25 @@
         return map;
     }
 
+    /**
+     * <p>Gets the {@code Map} of enums.</p>
+     *
+     * <p>This method is useful when you need a map of enums.</p>
+     *
+     * @param <E> the type of enumeration
+     * @param <K> the type of the map key
+     * @param enumClass the class of the enum to query, not null
+     * @param keyFunction the function to query for the key, not null
+     * @return the modifiable map of enums, never null
+     */
+    public static <E extends Enum<E>, K> Map<K, E> getEnumMap(final Class<E> enumClass, final Function<E, K> keyFunction) {
+        final Map<K, E> map = new LinkedHashMap<>();

Review comment:
       Perhaps optimise the size of the hashmap:
   ```java
   final E[] elements = enumClass.getEnumConstants();
   final Map<K, E> map = new LinkedHashMap<>(elements.length);
   for (final E e : elements) {
   ```




-- 
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 #730: EnumUtils: Add more-generic getEnumMap() method.

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


   
   [![Coverage Status](https://coveralls.io/builds/37919260/badge)](https://coveralls.io/builds/37919260)
   
   Coverage increased (+0.008%) to 94.937% when pulling **40ba96b782d41165d93ab130a9b2903eda9022fe on MaxwellCody:generic-getenummap** into **6b9964ff4b89205d63eac3db005757edf19e9dda 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 #730: EnumUtils: Add more-generic getEnumMap() method.

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



##########
File path: src/main/java/org/apache/commons/lang3/EnumUtils.java
##########
@@ -294,6 +295,25 @@
         return map;
     }
 
+    /**
+     * <p>Gets the {@code Map} of enums.</p>
+     *
+     * <p>This method is useful when you need a map of enums.</p>
+     *
+     * @param <E> the type of enumeration
+     * @param <K> the type of the map key
+     * @param enumClass the class of the enum to query, not null
+     * @param keyFunction the function to query for the key, not null
+     * @return the modifiable map of enums, never null
+     */

Review comment:
       Hi @MaxwellCody and thank you for your PR. Some comments:
   
   - Add `@since 3.13.0`
   - "Eat own dog food" and reimplement `EnumUtils.getEnumMap(Class<E>)` to use this method.

##########
File path: src/main/java/org/apache/commons/lang3/EnumUtils.java
##########
@@ -294,6 +295,25 @@
         return map;
     }
 
+    /**
+     * <p>Gets the {@code Map} of enums.</p>
+     *
+     * <p>This method is useful when you need a map of enums.</p>
+     *
+     * @param <E> the type of enumeration
+     * @param <K> the type of the map key
+     * @param enumClass the class of the enum to query, not null
+     * @param keyFunction the function to query for the key, not null
+     * @return the modifiable map of enums, never null
+     */
+    public static <E extends Enum<E>, K> Map<K, E> getEnumMap(final Class<E> enumClass, final Function<E, K> keyFunction) {
+        final Map<K, E> map = new LinkedHashMap<>();

Review comment:
       Add `Objects.requireNonNull(enumClass, "enumClass")` as the 1st line of this method.




-- 
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 pull request #730: EnumUtils: Add more-generic getEnumMap() method.

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


   Hi @MaxwellCody 
   May you please rebase on master and address the comments?
   TY!
   


-- 
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 closed pull request #730: EnumUtils: Add more-generic getEnumMap() method.

Posted by GitBox <gi...@apache.org>.
garydgregory closed pull request #730:
URL: https://github.com/apache/commons-lang/pull/730


   


-- 
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: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org