Skip to content

Commit

Permalink
added methods for getting enum property values
Browse files Browse the repository at this point in the history
  • Loading branch information
nur-sgaertner authored and darkv committed Sep 9, 2016
1 parent 1b2faf1 commit ad24429
Showing 1 changed file with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,46 @@ public static NSArray<String> componentsSeparatedByStringWithDefault(String key,
}
return array;
}


/**
* Returns an enum value for a given enum class and system property. If the property is not
* set or matches no enum constant, <code>null</code> will be returned. The search for the
* enum value is case insensitive, i.e. a property value "foo" will match the enum constant
* <code>FOO</code>.
*
* @param enumClass the enum class
* @param key the property key
* @return the enum value
*/
public static <T extends Enum> T enumValueForKey(Class<T> enumClass, String key) {
return enumValueForKeyWithDefault(enumClass, key, null);
}

/**
* Returns an enum value for a given enum class and system property. If the property is not
* set or matches no enum constant, the specified default value will be returned. The
* search for the enum value is case insensitive, i.e. a property value "foo" will match
* the enum constant <code>FOO</code>.
*
* @param enumClass the enum class
* @param key the property key
* @param defaultValue the default value
* @return the enum value
*/
public static <T extends Enum> T enumValueForKeyWithDefault(Class<T> enumClass, String key, T defaultValue) {
T result = defaultValue;
String stringValue = stringForKey(key);
if (stringValue != null) {
for (T enumValue : enumClass.getEnumConstants()) {
if (enumValue.name().equalsIgnoreCase(stringValue)) {
result = enumValue;
break;
}
}
}
return result;
}

/**
* <div class="en">
* Sets an array in the System properties for
Expand Down

0 comments on commit ad24429

Please sign in to comment.