近期在读Hadoop#Yarn部分的源代码。读到状态机那一部分的时候,感到enmu的使用方法实在是太灵活了,在给并发编程网翻译一篇文章的时候,正好碰到一篇这种文章。就赶紧翻译下来,涨涨姿势。

原文链接:http://www.javacodegeeks.com/2011/07/java-secret-using-enum-to-build-state.html

作者:Peter Lawrey    译者:陈振阳

综述

Java中的enum比其它的语言中的都强大,这产生了非常多令人吃惊的使用方法。

本文中,我将列出Java中的enum的一些特性,然后将这些特性应用到一起构成一个状态机。

Enum的单例和工具类使用方法

你能够很easy地用一个enmu构建一个单例或者工具类。

enum Singleton {
INSTANCE;
}
enum Utility {
; // no instances
}

用enum实现一个接口

你也能够在一个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; }
}

每个enum实例,一个不同的子类

你能够重载一个enum实例的方法。这将高效的给以个enum的实例一个自己的实现。

// from http://download.oracle.com/javase/1,5.0/docs/guide/language/enums.html
public enum Operation {
PLUS { double eval(double x, double y) { return x + y; } },
MINUS { double eval(double x, double y) { return x - y; } },
TIMES { double eval(double x, double y) { return x * y; } },
DIVIDE { double eval(double x, double y) { return x / y; } }; // Do arithmetic op represented by this constant
abstract double eval(double x, double y);
}

使用一个enum实现一个状态机

用上边的技术你能够做的是创建一个基于状态的enum。

在这个小样例中,一个解析器的状态机处理一个来自一个ByteBuffer的原始的XML。每个状态都有自己的处理方法,假设没有足够的可用的数据,状态机能够返回来再次获取很多其它的数据。

状态之间的每一次变换都被定义,全部状态的代码在一个enum中。

interface Context {
ByteBuffer buffer();
State state();
void state(State 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().remaining() < 16) 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));
}

使用这中方式,能够创建一个XML解析器,解析器能够处理10微秒内的数据包。大多数情况下。它跟你须要的一样高效。

Reference: Java Secret: Using an enum to build a State machine from our JCG
partner
 Peter Lawrey at the Vanilla Java.

Related Articles:

Java Secret: Using an enum to build a State machine(Java秘术:用枚举构建一个状态机)的更多相关文章

  1. this compilation unit is not on the build path of a java project

    在eclipse中新建maven project后,会自动生成main\test目录结构,新建一个测试类,然后编辑类文件时,总是提示错误:this compilation unit is not on ...

  2. 【eclipse】eclipse报错:the resource is not on the build path of a java project

    最近在eclipse中,使用svn导入svn上的一个maven项目,但是导入后类的包并没有以源码包的方式显示,而是以普通文件包的方式显示出来,在对类进行F3等操作时就报错:“the resource ...

  3. java.lang.IllegalArgumentException: No enum constant org.apache.ws.commons.schema.XmlSchemaForm.

    一次系统断电维护之后,apache cxf 的 web service 接口调用一直报错: java.lang.IllegalArgumentException: No enum constant o ...

  4. 【问题记录系列】the resource is not on the build path of a java project

    在eclipse中新建了一个maven项目搭建Spring源码阅读环境,创建一个bean生产getter和setter方法的时候报错“the resource is not on the build ...

  5. Sublime Text Build System——编译运行Java

    今天Google如何在ST中编译运行Java的时候,无意中发现了一个更好的方法. 其实,在ST中是可以编译Java的,但是运行不了,因为没有配置运行命令.那么一般的配置方法都是如下的: http:// ...

  6. build path libraries java基础--Jar包添加到build path方式说明--01

    摘自: http://blog.csdn.net/haolongabc/article/details/7007701 java基础--Jar包添加到build path方式说明--01 前言:这段短 ...

  7. Error-the resource is not on the build path of a java project

    错误描述 eclipse中的the resource is not on the build path of a java project,在Eclipse中点击生成源码时,弹窗提示该错误 解决办法 ...

  8. sbt is a build tool for Scala, Java, and more

    http://www.scala-sbt.org/0.13/docs/index.html sbt is a build tool for Scala, Java, and more. It requ ...

  9. the resource is not on the build path of a java project错误

    在eclipse中,使用mavenimport了一个工程,但是在对某一个类进行F3/F4/ctrl+alt+H操作的时候报错:“the resource is not on the build pat ...

随机推荐

  1. PowerShell攻防进阶篇:nishang工具用法详解

    PowerShell攻防进阶篇:nishang工具用法详解 导语:nishang,PowerShell下并肩Empire,Powersploit的神器. 开始之前,先放出个下载地址! 下载地址:htt ...

  2. 【撸码caffe 三】 caffe.cpp

    caffe.cpp文件完成对网络模型以及模型配置参数的读入和提取,提供了网络模型训练的入口函数train和对模型的测试入口函数test.文件中使用了很多gflags和glog指令,gflags是goo ...

  3. [JavaEE] 20141228_Java类文章搜集

    http://www.blogjava.net/jiangshachina 博客园java频道 Maven入门--概念与实例(原) Maven入门--较复杂的实例(原) Maven插件使用收集(原) ...

  4. [Javascript] 40个轻量级JavaScript脚本库

    诸如jQuery, MooTools, Prototype, Dojo和YUI等JavaScript脚本库,大家都已经很熟悉.但这些脚本库有利也有弊--比如说JavaScript文件过大的问题.有时你 ...

  5. POJ 1416 DFS

    题目翻译: 公司现在要发明一种新的碎纸机,要求新的碎纸机能够把纸条上的数字切成最接近而不超过target值.比如,target的值是50,而纸条上的数字是12346,应该把数字切成四部分,分别是1.2 ...

  6. js两个页面之间URL传递参数中文乱码

  7. Educational Codeforces Round 33

    # Who = Penalty * A B C D E F 479 arkethos 4 247   + 00:08 + 00:19 +1 00:59 +2 01:41     479  ne-leo ...

  8. 关于React-native的介绍以及环境搭建

    React-Native介绍(后面内容的RN就是指react-native) 由facebook公司推出的,基于react,能开发原生app 原理: 1. 利用react框架写好js代码 2. 利用p ...

  9. Java学习-课堂总结

    一.字符串比较方式 1)‘==’   地址值比较      2) equals()方法   内容比较 二.String类的两种实例化方式     1)String str=“Hello”:     2 ...

  10. 【Oracle】ORA-55610: Invalid DDL statement on history-tracked table

    —删除表emp1时出现问题 SCOTT@GOOD> drop table emp1; drop table emp1 * ERROR at line 1: ORA-55610: Invalid ...