Skip to main content
added the MyEnum definition and added comments to make the happening behind the scene more explicitt
Source Link

One of my favorite lib: Apache Commons.

The EnumUtils can do that easily.

Following an example to validate an Enum with that library:

public enum MyEnum { DIV("div"), DEPT("dept"), CLASS("class"); private final String val; MyEnum(String val) { this.val = val; } public String getVal() { return val; } } MyEnum strTypeEnum = null; // test if String str is compatible with the enum // e.g. if you pass str = "div", it will return false. If you pass "DIV", it will return true. if( EnumUtils.isValidEnum(MyEnum.class, str) ){ strTypeEnum = MyEnum.valueOf(str); } 

One of my favorite lib: Apache Commons.

The EnumUtils can do that easily.

Following an example to validate an Enum with that library:

MyEnum strTypeEnum = null; // test if String str is compatible with the enum if( EnumUtils.isValidEnum(MyEnum.class, str) ){ strTypeEnum = MyEnum.valueOf(str); } 

One of my favorite lib: Apache Commons.

The EnumUtils can do that easily.

Following an example to validate an Enum with that library:

public enum MyEnum { DIV("div"), DEPT("dept"), CLASS("class"); private final String val; MyEnum(String val) { this.val = val; } public String getVal() { return val; } } MyEnum strTypeEnum = null; // test if String str is compatible with the enum // e.g. if you pass str = "div", it will return false. If you pass "DIV", it will return true. if( EnumUtils.isValidEnum(MyEnum.class, str) ){ strTypeEnum = MyEnum.valueOf(str); } 
minor grammar improvement
Source Link
рüффп
  • 5.5k
  • 34
  • 74
  • 125

One of my favorite lib: Apache Commons.

The EnumUtils can do that easily.

Here is a sample codeFollowing an example to validate an Enum with that library:

MyEnum strTypeEnum = null; // test if String str is compatible with the enum if( EnumUtils.isValidEnum(MyEnum.class, str) ){ strTypeEnum = MyEnum.valueOf(str); } 

One of my favorite lib: Apache Commons.

The EnumUtils can do that easily.

Here is a sample code to validate an Enum:

MyEnum strTypeEnum = null; // test if String str is compatible with the enum if( EnumUtils.isValidEnum(MyEnum.class, str) ){ strTypeEnum = MyEnum.valueOf(str); } 

One of my favorite lib: Apache Commons.

The EnumUtils can do that easily.

Following an example to validate an Enum with that library:

MyEnum strTypeEnum = null; // test if String str is compatible with the enum if( EnumUtils.isValidEnum(MyEnum.class, str) ){ strTypeEnum = MyEnum.valueOf(str); } 
Added sample code
Source Link
рüффп
  • 5.5k
  • 34
  • 74
  • 125

One of my favorite lib: Apache Commons.

The EnumUtils can do that easily.

Here is a sample code to validate an Enum:

MyEnum strTypeEnum = null; // test if String str is compatible with the enum if( EnumUtils.isValidEnum(MyEnum.class, str) ){ strTypeEnum = MyEnum.valueOf(str); } 

One of my favorite lib: Apache Commons.

The EnumUtils can do that easily.

One of my favorite lib: Apache Commons.

The EnumUtils can do that easily.

Here is a sample code to validate an Enum:

MyEnum strTypeEnum = null; // test if String str is compatible with the enum if( EnumUtils.isValidEnum(MyEnum.class, str) ){ strTypeEnum = MyEnum.valueOf(str); } 
Source Link
рüффп
  • 5.5k
  • 34
  • 74
  • 125
Loading