近期在读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. ES 断路器——本质上保护OOM提前抛出异常而已

    监控fielddata使用了多少内存以及是否有数据被驱逐是非常重要的.大量的数据被驱逐会导致严重的资源问题以及不好的性能. Fielddata使用可以通过下面的方式来监控: 对于单个索引使用 {ref ...

  2. Connect the Cities--hdoj

    Connect the Cities Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...

  3. 院校-美国:麻省理工学院(MIT)

    ylbtech-院校-美国:麻省理工学院(MIT) 麻省理工学院(Massachusetts Institute of Technology),简称麻省理工(MIT),坐落于美国马萨诸塞州波士顿都市区 ...

  4. ettercap + driftnet 实现同网段下流量欺骗

    前言: 由于在局域网中,网关会不断地发送 ARP 数据包询问当前是否有新的客户端上线,如果我们可以欺骗当前局域网网段下的主机, 把我们当成网关地址,并且我们把欺骗的流量转发到真正的网关地址,这样我们就 ...

  5. MySQL架构与SQL执行流程

    MySQL架构设计 下面是一张MySQL的架构图: 上方各个组件的含义如下: Connectors 指的是不同语言中与SQL的交互 Management Serveices & Utiliti ...

  6. Solr快速入门(一)

    概述 本文档介绍了如何获取和运行Solr,将各种数据源收集到多个集合中,以及了解Solr管理和搜索界面. 首先解压缩Solr版本并将工作目录更改为安装Solr的子目录.请注意,基本目录名称可能随Sol ...

  7. Android ToolBar自定义图标,关联DrawerLayout

    Android5.0出现了一个可以代替ActionBar的控件ToolBar,使用更加灵活,一般我们使用ToolBar来和DrawerLayout配合使用,官方提供了一个开关类ActionBarDra ...

  8. Appium 环境搭建 - macOS

    本文没有安装 Appium Desktop,Appium Server 直接在命令行中进行即可. Homebrew,macOS 包管理器: ruby -e "$(curl -fsSL htt ...

  9. UWP Tiles

    1.我们建议安装通知库 NuGet 程序包 详细内容 2.我们建议安装NotificationsVisualizerLibrary 这是 The official NotificationsVisua ...

  10. Cython入门.VS.C++

    原文链接:http://blog.csdn.net/gzlaiyonghao/article/details/4561611 作者:perrygeo 译者:赖勇浩(http://laiyonghao. ...