How to convert an integer to an enumeration type for easy use in the switch

Source: Internet
Author: User

In Java, Enum is different from int, which is equivalent to two different types and cannot be converted to each other. You need to add additional methods for conversion. If you want to customize the initial values and compare the values in the switch, you need to do some special processing.
The enum prototype is as follows:

 
Public abstract class Enum <E extends Enum <E> implements serializable, comparable <E> {private final string name; private final int ordinal ;}

Here, name is the name of the enumeration you define, such as estudent and eteacher. Ordinal is arranged sequentially according to the sequence you defined. The values are 0, 1, and 2 ....

It should be noted that the enum type in Android cannot be inherited, and it is also very difficult to rewrite, such as basiclrucache. java, emptyarray. java, class. java and many other files, while class. java references many packages, such:

Import sun. Misc. unsafe;
Import sun. Reflect. constantpool;
Import sun. Reflect. reflection;
Import sun. Reflect. reflectionfactory;
Import sun. Reflect. signatureiterator;
Import sun. Reflect. generics. Factory. corereflectionfactory;
Import sun. Reflect. generics. Factory. genericsfactory;
Import sun. Reflect. generics. repository. classrepository;
Import sun. Reflect. generics. repository. methodrepository;
Import sun. Reflect. generics. repository. constructorrepository;
Import sun. Reflect. generics. Scope. classs.pdf;
Import sun. Security. util. securityconstants;
Import sun. Reflect. annotation .*;
......

Therefore, it is difficult to replace Enum with myenum:
Public abstract class myenum <E extends myenum <E> implements serializable, comparable <E> {
}

However, to achieve the above purpose, we can see the following example.
This is the user's expectation:

Int val = 2; myenum type = enumutils. getmyenum (myenum. class, Val); val = 10; myenum defaulttype = myenum. eunknown; type = enumutils. getmyenum (myenum. class, Val, defaulttype); Switch (type) {Case eunknown: break; Case estudent: break; Case steacher: break ;}

In actual projects, the enumerated values of myenum may be as follows:

 
Public Enum myenum {eunknown (0), estudent (2), steacher (3 );}

Even like this:

 
Enewstext (0x1001), enewswithslide (0x1002), enewswithvideo (0x1003), enewscomment (0x1004), ematch (0x2000), // match, news ematchlivevideopresent (0x2001), ematchlivetextpresent (0x2002), ematchover (0x2003), ematchlivevideodns (0x2004), ematchlivetextdns (0x2005 ),

Therefore, you must first assign an initial value (private ):

 
Int val; myenum (INT Val) {This. Val = val ;}

Second, convert int to enum.
The conversion of Enum to int is very simple. You only need to note the difference between Val and ordinal:

 
Public int getval () {return val ;}

Finally, to convert int to Enum, the powerful Java certainly won't be a fool. You need to return different enum values through switch (VAL). You can use the following method:

Public static <t extends Enum <t> T getmyenum (class <t> enumtype, int Val) {return getmyenum (enumtype, Val, null );} public static <t extends Enum <t> T getmyenum (class <t> enumtype, int Val, t defaultt) {T ret = defaultt; try {T [] myenums = enumtype. getenumconstants (); field FL = enumtype. getdeclaredfield ("Val"); If (myenums! = NULL & myenums. length> 0) {for (T: myenums) {try {If (val = FL. getint (t) {ret = T; break;} catch (illegalargumentexception e) {// E. printstacktrace ();} catch (illegalaccessexception e) {// E. printstacktrace () ;}}} catch (nosuchfieldexception e) {// E. printstacktrace ();} catch (nullpointerexception e) {// E. printstacktrace ();} return ret ;}

Come on, cainiao! Pai_^

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.