Preskočiť na hlavný obsah

Get Enum constant of an Enum by String

Method cast(Class, String) builds the enum value constant of the specified Enum. The name must match exactly (case) an identifier used to declare an enum constant in the given Enum.

@SuppressWarnings({ "rawtypes", "unchecked" })
public static <E extends Enum<E>> E cast(String name, Class&lt;E> clazz) {

  if (clazz == null || name == null || name.isEmpty()) {
    return null;
  }

  try {
   return (E) Enum.valueOf((Class<Enum>) clazz, name);
  } catch (Throwable e) {
    throw new RuntimeException("Enum cast error: '" + name
 + "' in not valid value of '" + clazz.getName() + "'");
  }
}

Obľúbené príspevky z tohto blogu

mysql 5.0 upgrade to 5.1

The 5.1 series of MySQLwas unmasked for the gentoo portage. When upgrading from an older major version (including 5.0), you will be required to rebuild everything linked to the libmysqlclient.so.15 and libmysqlclient_r.so.15. You can do this by installing app-portage/gentoolkit and running: # revdep-rebuild --library libmysqlclient.so.15 # revdep-rebuild --library libmysqlclient_r.so.15 If you use the Portage 2.2 series, you may also use: # emerge @preserved-rebuild The official upgrade documentation is available here: http://dev.mysql.com/doc/refman/5.1/en/upgrading.html Note that existing databases may need converting as well, again including those upgrading from 5.0 to 5.1.