Java Enum Parse tryParseEnum(Class c, String string)

Here you can find the source of tryParseEnum(Class c, String string)

Description

Tries to parse a string into Enum, if unsuccessful returns null.

License

Open Source License

Parameter

Parameter Description
c a parameter
string a parameter
T a parameter

Return

Enum value or null if unsuccessful

Declaration

public static <T extends Enum<T>> T tryParseEnum(Class<T> c,
        String string) 

Method Source Code

//package com.java2s;
/**// w  ww .j a  v  a  2  s . c o m
 * Copyright (c) 2010-2018 by the respective copyright holders.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

public class Main {
    /**
     * Tries to parse a string into Enum, if unsuccessful returns null.
     * @param c
     * @param string
     * @param <T>
     * @return Enum value or null if unsuccessful
     */
    public static <T extends Enum<T>> T tryParseEnum(Class<T> c,
            String string) {
        if (string != null) {
            try {
                return Enum.valueOf(c, string);
            } catch (IllegalArgumentException ex) {
            }
        }
        return null;
    }
}

Related

  1. tryParseEnum(Object ob, Class enumType)