Java secret:using an enum-to-Build a state machine (Java secret: building an enumeration with enumerations)

Source: Internet
Author: User
Tags xml parser

Recently read the source code of the Hadoop#yarn section. Read to the state machine that part of the time, feel the use of ENMU is too flexible, in the concurrent programming network to translate an article, just met an article. Quickly translate down, rise posture.

Original link: http://www.javacodegeeks.com/2011/07/java-secret-using-enum-to-build-state.html
Peter Lawrey Translator: Chen Zhenyang

Review

The enum in Java is more powerful than any other language, which creates a surprising number of ways to use it.

In this article, I'll list some of the features of the enum in Java, and then apply these attributes together to form a state machine.

Examples of enums and methods of using tool classes

You can easily build a single case or tool class with a ENMU.

Enum Singleton {    INSTANCE;} Enum Utility {    ;//No instances}

Implementing an interface with enum

You can also implement an interface in an enum.

Interface Named {public    String name ();    public int order ();} Enum Planets implements Named {    Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune;    Name () is implemented automagically.    public int order () {return ordinal () +1;}}

Each enum instance, a different subclass

You can overload the method of an enum instance. This will efficiently give an example of an enum to its own implementation.

From Http://download.oracle.com/javase/1,5.0/docs/guide/language/enums.htmlpublic enum operation {  PLUS   {Double eval (double x, double y) {return x + y;}},  minus  {double eval (double x, double y) {return xy;}} ,  times  {Double eval (double x, double y) {return x * y;}},  DIVIDE {double eval (double x, double y) {Retu RN x/y; } };  Do arithmetic op represented by this constant  abstract double eval (double x, double y);}

Using an enum to implement a state machine

What you can do with the above technique is to create a state-based enum.

In this sample, a parser's state machine processes a raw XML from a bytebuffer. Each state has its own processing method, assuming that there is not enough data available, the state machine can return to fetch many other data again.

Each transformation between states is defined, and the code for all States is in an enum.

Interface Context {bytebuffer buffer ();    State State (); void State;     Interface State {/** * @return true to keep processing, false to read more data. */Boolean process (context context);} Enum states implements state {XML {public Boolean process (context context) {if (Context.buffer (). R)            Emaining () <) return false;            Read header if (headercomplete) context.state (states.root);        return true;  }}, ROOT {public Boolean process (context context) {if (Context.buffer (). Remaining () < 8) return            False            Read root tag if (rootcomplete) context.state (states.in_root);        return true;    }}}public void Process (context context) {Socket.read (Context.buffer ()); while (Context.state (). Process (context));}

Using this method, you can create an XML parser that can handle packets within 10 microseconds. Most of the cases. It's as efficient as you need to be.

Reference: Java secret:using an enum-to-build a state machine from our JCG partner Peter Lawrey at the Vanilla java.

Related articles:

    • Low GC in Java:use primitives instead of wrappers
    • Java Lambda Syntax Alternatives
    • How does JVM handle locks
    • Erlang vs Java Memory Architecture
    • Java Fork/join for Parallel programming


Java secret:using an enum-to-Build a state machine (Java secret: building an enumeration with enumerations)

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.