On Mon, Apr 27, 2020 at 9:11 AM Gary Gregory <garydgreg...@gmail.com> wrote:

> On Mon, Apr 27, 2020 at 6:39 AM Rob Spoor <apa...@icemanx.nl> wrote:
>
>> Why use a String as default and not a T?
>>
>
> Yes of course you are correct.
>

Like this:

    default <T extends Enum<T>> T getEnum(String key, Class<T> enumType) {
        return Enum.valueOf(enumType, getString(key));
    }

    default <T extends Enum<T>> T getEnum(String key, Class<T> enumType, T
defaultEnum) {
        final String strValue = getString(key);
        return strValue == null ? defaultEnum : Enum.valueOf(enumType,
strValue);
    }

Gary


>
> Gary
>
>
>>
>> default <T extends Enum<T>> T getEnum(String key, Class<T> enumType, T
>> defaultEnum) {
>>      String value = getString(key, null);
>>      return value != null ? Enum.valueOf(enumType, value) : defaultEnum);
>> }
>>
>>
>> On 27/04/2020 06:28, Xeno Amess wrote:
>> > I really think we shall have a common template for Enums
>> >
>> > XenoAmess
>> >
>> > ________________________________
>> > From: Gary Gregory <garydgreg...@gmail.com>
>> > Sent: Monday, April 27, 2020 8:50:22 AM
>> > To: Commons Developers List <dev@commons.apache.org>
>> > Subject: [configuration]
>> org.apache.commons.configuration2.ImmutableConfiguration.getEnum()?
>> >
>> > Hi All:
>> >
>> > I'd like to have a way to get an enum instance out of
>> > a org.apache.commons.configuration2.ImmutableConfiguration.
>> >
>> > I'm surprised there is nothing like a getEnum(...). Any thoughts about
>> > adding:
>> >
>> > default <T extends Enum<T>> T getEnum(String key, Class<T> enumType) {
>> >          return Enum.valueOf(enumType, getString(key));
>> >      }
>> >
>> > default <T extends Enum<T>> T getEnum(String key, Class<T> enumType,
>> String
>> > defaultEnum) {
>> >          return Enum.valueOf(enumType, getString(key, defaultEnum));
>> >      }
>> >
>> > ?
>> > Gary
>> >
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
>> For additional commands, e-mail: dev-h...@commons.apache.org
>>
>>

Reply via email to