commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gary D. Gregory (Jira)" <j...@apache.org>
Subject [jira] [Resolved] (LANG-1495) Add EnumUtils getEnum() methods with default values
Date Mon, 28 Oct 2019 18:30:00 GMT

     [ https://issues.apache.org/jira/browse/LANG-1495?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Gary D. Gregory resolved LANG-1495.
-----------------------------------
    Fix Version/s: 3.10
       Resolution: Fixed

In git master, please review and close.

> Add EnumUtils getEnum() methods with default values
> ---------------------------------------------------
>
>                 Key: LANG-1495
>                 URL: https://issues.apache.org/jira/browse/LANG-1495
>             Project: Commons Lang
>          Issue Type: Improvement
>            Reporter: Cheong Voon Leong
>            Priority: Trivial
>             Fix For: 3.10
>
>          Time Spent: 1h 20m
>  Remaining Estimate: 0h
>
> I would like to add 2 overloading methods to EnumUtils, getEnum and getEnumIgnoreCase.
> Instead of returning null, if not found. In my opinion I think allowing user to set a
default result is a better approach or return an Optional.
>   
> {noformat}
> public static <E extends Enum<E>> E getEnum(final Class<E> enumClass,
final String enumName) {
>  return getEnum(enumClass, enumName, null);
>  }
> public static <E extends Enum<E>> E getEnum(final Class<E> enumClass,
final String enumName, E defaultEnum) {
>  if (enumName == null) {
>  return defaultEnum;
>  }
>  try {
>  return Enum.valueOf(enumClass, enumName);
>  } catch (final IllegalArgumentException ex) {
>  return defaultEnum;
>  }
>  }
> {noformat}
>   
>  
> {code:java}
> public static <E extends Enum<E>> E getEnumIgnoreCase(final Class<E>
enumClass, final String enumName) {
>  return getEnumIgnoreCase(enumClass, enumName, null);
>  }
> public static <E extends Enum<E>> E getEnumIgnoreCase(final Class<E>
enumClass, final String enumName, E defaultEnum) {
>  if (enumName == null || !enumClass.isEnum()) {
>  return defaultEnum;
>  }
>  for (final E each : enumClass.getEnumConstants()) {
>  if (each.name().equalsIgnoreCase(enumName)) {
>  return each;
>  }
>  }
>  return defaultEnum;
>  }
> {code}
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Mime
View raw message