Java Secret: Using an enum to build a State machine(Java秘术:用枚举构建一个状态机)
近期在读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秘术:用枚举构建一个状态机)的更多相关文章
- this compilation unit is not on the build path of a java project
在eclipse中新建maven project后,会自动生成main\test目录结构,新建一个测试类,然后编辑类文件时,总是提示错误:this compilation unit is not on ...
- 【eclipse】eclipse报错:the resource is not on the build path of a java project
最近在eclipse中,使用svn导入svn上的一个maven项目,但是导入后类的包并没有以源码包的方式显示,而是以普通文件包的方式显示出来,在对类进行F3等操作时就报错:“the resource ...
- java.lang.IllegalArgumentException: No enum constant org.apache.ws.commons.schema.XmlSchemaForm.
一次系统断电维护之后,apache cxf 的 web service 接口调用一直报错: java.lang.IllegalArgumentException: No enum constant o ...
- 【问题记录系列】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 ...
- Sublime Text Build System——编译运行Java
今天Google如何在ST中编译运行Java的时候,无意中发现了一个更好的方法. 其实,在ST中是可以编译Java的,但是运行不了,因为没有配置运行命令.那么一般的配置方法都是如下的: http:// ...
- build path libraries java基础--Jar包添加到build path方式说明--01
摘自: http://blog.csdn.net/haolongabc/article/details/7007701 java基础--Jar包添加到build path方式说明--01 前言:这段短 ...
- 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中点击生成源码时,弹窗提示该错误 解决办法 ...
- 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 ...
- 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 ...
随机推荐
- php错误抑制符
php错误抑制符 简介 PHP 支持一个错误控制运算符:@.当将其放置在一个 PHP 表达式之前,该表达式可能产生的任何错误信息都被忽略掉. @这个符号在Java里面是注解符号. 实例 <?ph ...
- 关于ssh加密方式的理解
最近公司服务器被挖矿,所以更换了ssh的连接方式,从之前的密码登陆更换为密钥登陆方式,且禁止了密码登陆.所以在配置这个密钥的过程中,顺带了解了些ssh的原理和相关知识.通用的开源 1.ssh是什么,为 ...
- python 微信红包
def redbags(money, num=10): import random choice = random.sample(range(1, money * 100), num - 1) cho ...
- weborm aspx开发基础
ASP.NET - .net开发网站应用程序的技术总称,来源于 ASP 两种方法技术—WebForm: MVC:java用 十二个表单元素: 文本框<input type="text& ...
- T对象序列化后T对象中属性字段不见了?
序列化:JsonConvert.SerializeObject(T) 直接在类的上面添加[Table("表名")] 在类上添加属性[DataContract] 在属性上添加属性[D ...
- JavaScript实现鼠标效果
<html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...
- 06--C语言数学函数
在使用C语言数学函数时候,应该在该源文件中使用以下命令行: #include <math.h> 或 #include "math.h",这里的<>跟&quo ...
- CorelDRAW 2019新品发布,行业大咖就差你了
近日,由苏州思杰马克丁软件公司独家代理的CorelDRAW 2019将在苏州开启一场设计上的饕餮盛宴,您报名了么? 不管您是专业的设计师还是热爱设计的狂热粉丝,都将有机会参与到我们的活动中,为了这场盛 ...
- springboot-helloworld实现
springboot快速入门 首先,建立一个空的项目 第二步: 建立一个springboot项目 第三步:添加依赖: <?xml version="1.0" encoding ...
- day27-2 pandas模块
目录 pandas Series(了解) DataFrame 内置方法 处理缺失值 合并数据 取值 把表格传入excel文件中 把表格从excel中取出来 高级(了解) pandas 处理表格等文件/ ...