Here you can find the source of fromString(Class
Parameter | Description |
---|---|
T | the enum type |
enumClass | the class object for the enum type |
s | the string that specifies a constant |
defaultValue | the default value to use if none of the constants matches or if s is null |
public static final <T extends Enum<T>> T fromString(Class<T> enumClass, String s, T defaultValue)
//package com.java2s; /**/*from ww w. j av a 2 s. com*/ * Copyright (c) 2010 Martin Geisse * * This file is distributed under the terms of the MIT license. */ public class Main { /** * Returns the enum constant that matches the specified string, or the default value if * none of the constants matches or if the string is null. * @param <T> the enum type * @param enumClass the class object for the enum type * @param s the string that specifies a constant * @param defaultValue the default value to use if none of the constants matches or if s is null * @return the matching constant or the default value */ public static final <T extends Enum<T>> T fromString(Class<T> enumClass, String s, T defaultValue) { try { return Enum.valueOf(enumClass, s); } catch (IllegalArgumentException e) { return defaultValue; } catch (NullPointerException e) { return defaultValue; } } }