[ 
https://issues.apache.org/jira/browse/LANG-974?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14174076#comment-14174076
 ] 

Duncan Jones commented on LANG-974:
-----------------------------------

That sounds like an odd use case for this method. I would expect a key->enum 
mapping scheme to be a switch statement, where the default case would catch any 
invalid key values.

Or perhaps an enum that is aware of the legacy values:

{code:java}
public enum ExampleEnum {
  Foo("foo"),
  BAR("bar");
  
  private final String legacyString;
  
  private ExampleEnum(String legacyString) {
    this.legacyString = legacyString;
  }
  
  public static ExampleEnum fromLegacyString(String legacyString) {
    for (ExampleEnum e : values()) {
      if (e.legacyString.equals(legacyString)) {
        return e;
      }
    }    
    throw new IllegalArgumentException("Bad enum value");
  }  
}
{code}

It doesn't seem like a problem that should be solved with a 
{{defaultIfNotIn()}} method.

> New StringUtils Method defaultIfNotIn(String, String, String ...)
> -----------------------------------------------------------------
>
>                 Key: LANG-974
>                 URL: https://issues.apache.org/jira/browse/LANG-974
>             Project: Commons Lang
>          Issue Type: Wish
>          Components: lang.*
>    Affects Versions: 3.2.1
>            Reporter: Stephan Knitelius
>            Priority: Minor
>             Fix For: Review Patch
>
>
> Example use case:
> If a Method expects values that should only consist of a certain set such as 
> "0" and "1", and any Invalid String should be defaulted to "0".
> Possible Solution:
> {code}
>   public static String defaultIfNotIn(final String str, final String 
> defaultString, String... validStrings) {
>     if (str == null) {
>       return defaultString;
>     }
>     for (final String validString : validStrings) {
>       if (validString.equals(str)) {
>         return str;
>       }
>     }
>     return defaultString;
>   }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to