Guava Source Reading-base-enums

Source: Internet
Author: User

Package com.google.common.base;


Guava source of the method of this class introduction only one sentence:
Utility methods for working with {@link Enum} instances.

This means the tool method that is provided to the enum instance.

What is an enumeration?
Enumerations are simply a type of data, except that the data type contains only custom specific data, which is a set of data that has a common attribute.
For example, a color can also be defined as an enumeration type, which can contain any color you define and, when needed, only needs to be called by an enumeration, plus
For example, the seasons (summer and winter), the week (Monday to Sunday) and so on. These data with common projection characteristics can define enumerations.

What is the enumeration like?

 Public enum   genderenum {    MALE (1, "male"), FEMALE (2, "female");     Private int code;     Private String value;    Genderenum (int  code,string value) {        this. Code = Code;          this. Value = value;    }}

The above is a gender enumeration, containing both male and female, when used to directly call on the line.

Let's take a look at the guava support for enumerations.

1. GetField () method

 Public Static Field GetField (enum<?> enumvalue) {    Class<?> clazz = enumvalue.getdeclaringclass ();     try  {      return  Clazz.getdeclaredfield (Enumvalue.name ());     Catch (Nosuchfieldexception impossible) {      thrownew  Assertionerror (impossible );    }  }

field that returns the value of the variable named Enumvalue variable

2. Getifpresent () method

 Public Static extends Enum<t>> optional<t> getifpresent (class<t> enumclass, String value) {    Checknotnull ( Enumclass);    Checknotnull (value);     return platform.getenumifpresent (Enumclass, value);  }

A optional<enum> Constant according to the given type

3. Stringconverter () method

 Public Static extends Enum<t>> converter<string, t> stringconverter (final class<t> enumclass) {     returnnew stringconverter<t>(enumclass);  }

Returns a converter object converted to sring and a value of the given enum type, using Enum.valueof (Class, String) and Enum.name ().

Here's a look at the example:

Enumeration object:

 Public enum   genderenum {    MALE (1, "male"), FEMALE (2, "female");     Private int code;     Private String value;    Genderenum (int  code,string value) {        this. Code = Code;          this. Value = value;    }}

Here are the test methods:

  Genderenum male = Genderenum.male; 
System.out.println ( "enum 1:" +enums.getfield (male));
System.out.println ( "Enumeration 2:" +enums.getifpresent (Genderenum., "Life" System.out.println ( "Enumeration 3:" +enums.getifpresent (Genderenum., String.valueof (genderenum.valueof ("MALE" System.out.println ( "Enumeration 4:" +enums.stringconverter (Genderenum. Converter <String,GenderEnum> stringgenderenumconverter = Enums.stringconverter (Genderenum.class );
System.out.println ("Enum 5:" +stringgenderenumconverter.convert (GenderEnum.MALE.name ()));

Output Result:

Enumeration 1:publicstaticfinal  javatest.model.GenderEnum Javatest.model.GenderEnum.MALE
Enumeration 2:optional.absent ()
Enumeration 3:optional.of (MALE)
Enumerates 4:enums.stringconverter (Javatest.model.GenderEnum. class )
Enumeration 5:male

Guava Source Reading-base-enums

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.