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 in the Hadoop#yarn part of the source code, read 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 like this, quickly translated down, rose posture.

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

Review

Enmu in Java is more powerful than any other language, which produces a lot of surprising usage.

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

Enmu of Singleton and tool class usage

You can use a ENMU to build a single case or tool class very simply.

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

Implement an interface with ENMU

You can also implement an interface in a ENMU.

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 ENMU instance, a different subclass

You can overload a method for an ENMU instance. This will efficiently give an example of a ENMU 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);}

Implement a state machine using a ENMU

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

In this small example, a parser's state machine processes a raw XML from a bytebuffer. Each state has its own processing method, and if there is not enough data available, the state machine can return to get more data. Each transformation between states is defined, and the code for all States is in a enmu.

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 milliseconds. In most cases, it is as efficient as you need.

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.