Skip to content

Commit 04105a7

Browse files
author
haibo.yang
committed
添加状态机
1 parent bdc85fc commit 04105a7

File tree

5 files changed

+137
-8
lines changed

5 files changed

+137
-8
lines changed

commons-base/pom.xml

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,43 @@
66
<parent>
77
<groupId>com.bob.commons</groupId>
88
<artifactId>commons-parent</artifactId>
9-
<version>${bob.project.version}</version>
9+
<version>1.0.0</version>
1010
<relativePath>../commons-parent/pom.xml</relativePath>
1111
</parent>
1212
<artifactId>commons-base</artifactId>
1313
<version>${bob.project.version}</version>
1414
<packaging>jar</packaging>
1515

1616

17+
<dependencies>
18+
<dependency>
19+
<groupId>com.google.guava</groupId>
20+
<artifactId>guava</artifactId>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>org.apache.commons</groupId>
25+
<artifactId>commons-lang3</artifactId>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.apache.commons</groupId>
30+
<artifactId>commons-collections4</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>commons-collections</groupId>
34+
<artifactId>commons-collections</artifactId>
35+
<version>3.2.2</version>
36+
<scope>compile</scope>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>com.alibaba</groupId>
41+
<artifactId>fastjson</artifactId>
42+
<version>1.1.37</version>
43+
<scope>compile</scope>
44+
</dependency>
45+
</dependencies>
46+
1747

1848
</project>

commons-base/src/main/java/com/bob/commons/response/BaseRes.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class BaseRes<T> {
1212
/**
1313
* 响应码
1414
*/
15-
private int code;
15+
private String code;
1616
/**
1717
* 响应信息
1818
*/
@@ -37,11 +37,11 @@ public BaseRes(BaseException ex) {
3737
this.level = ex.getLevel();
3838
}
3939

40-
public int getCode() {
40+
public String getCode() {
4141
return code;
4242
}
4343

44-
public void setCode(int code) {
44+
public void setCode(String code) {
4545
this.code = code;
4646
}
4747

commons-base/src/main/java/com/bob/commons/response/web/JsonWebRes.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.bob.commons.response.web;
22

3+
import com.bob.commons.exception.BaseErrorEnum;
34
import com.bob.commons.exception.BaseException;
4-
import com.bob.commons.exception.ErrorCode;
55
import com.bob.commons.response.BaseRes;
66

77
/**
@@ -22,9 +22,9 @@ public JsonWebRes(Exception exception) {
2222
super.setMessage(ex.getMessage());
2323
super.setLevel(ex.getLevel());
2424
} else {
25-
super.setCode(ErrorCode.SYSTEM_ERROR.getCode());
26-
super.setMessage(ErrorCode.SYSTEM_ERROR.getMessage());
27-
super.setLevel(ErrorCode.SYSTEM_ERROR.getLevel());
25+
super.setCode(BaseErrorEnum.SYSTEM_ERROR.getCode());
26+
super.setMessage(BaseErrorEnum.SYSTEM_ERROR.getMessage());
27+
super.setLevel(BaseErrorEnum.SYSTEM_ERROR.getLevel());
2828
}
2929
}
3030

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.bob.commons.statemachine;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import com.google.common.collect.Maps;
5+
import org.apache.commons.collections4.CollectionUtils;
6+
import org.apache.commons.collections4.MapUtils;
7+
import org.apache.commons.lang3.StringUtils;
8+
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
/**
13+
* 状态机
14+
*
15+
* @author haibo.yang
16+
* @since 12 六月 2018
17+
*/
18+
public class StateMachines {
19+
20+
private Map<String, Map<String, List<String>>> statemachines = Maps.newHashMap();
21+
22+
public StateMachines() {
23+
}
24+
25+
public Map<String, Map<String, List<String>>> getStatemachines() {
26+
return this.statemachines;
27+
}
28+
29+
public void setStatemachines(Map<String, Map<String, List<String>>> statemachines) {
30+
this.statemachines = statemachines;
31+
}
32+
33+
/**
34+
* 判断状态是否允许改变
35+
*
36+
* @param machine 状态机
37+
* @param from 当前状态
38+
* @param to 目标状态
39+
* @return
40+
*/
41+
public boolean permit2Change(String machine, String from, String to) {
42+
if (StringUtils.isEmpty(machine) || StringUtils.isEmpty(from) || StringUtils.isEmpty(to)) {
43+
return false;
44+
}
45+
if (MapUtils.isEmpty(this.statemachines) || !this.statemachines.containsKey(machine)) {
46+
return false;
47+
} else {
48+
Map<String, List<String>> instance = this.statemachines.get(machine);
49+
if (MapUtils.isEmpty(instance) || !instance.containsKey(from)) {
50+
return false;
51+
} else {
52+
List<String> candidates = instance.get(from);
53+
return CollectionUtils.isNotEmpty(candidates) && candidates.contains(to);
54+
}
55+
}
56+
}
57+
58+
59+
public static void main(String[] args) {
60+
String json = "{\"subscribe\": {\n" +
61+
" \"Start\": [\n" +
62+
" \"Submitted\",\n" +
63+
" \"Cancelled\"\n" +
64+
" ],\n" +
65+
" \"Submitted\": [\n" +
66+
" \"Confirmed\",\n" +
67+
" \"Cancelled\"\n" +
68+
" ],\n" +
69+
" \"Confirmed\": [\n" +
70+
" \"SignIned\",\n" +
71+
" \"Cancelled\"\n" +
72+
" ],\n" +
73+
" \"SignIned\": [\n" +
74+
" \"Finish\",\n" +
75+
" \"Cancelled\"\n" +
76+
" ]\n" +
77+
" }}";
78+
79+
Map map = JSON.parseObject(json, Map.class);
80+
StateMachines machines = new StateMachines();
81+
machines.setStatemachines(map);
82+
boolean b = machines.permit2Change("subscribe", "Submitted", "Cancelled");
83+
System.out.println(b);
84+
85+
}
86+
}

commons-parent/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<!--<servlet-api.version>2.5</servlet-api.version>-->
3535
<joda-time.version>2.5</joda-time.version>
3636
<commons-lang3.version>3.4</commons-lang3.version>
37+
<commons-collections4.version>4.1</commons-collections4.version>
3738
<commons-io.version>1.3.2</commons-io.version>
3839
<commons-net.version>3.3</commons-net.version>
3940
<commons-compress.version>1.12</commons-compress.version>
@@ -48,6 +49,7 @@
4849
<activemq.version>5.11.2</activemq.version>
4950
<freemarker.version>2.3.23</freemarker.version>
5051
<quartz.version>2.2.2</quartz.version>
52+
<guava.version>20.0</guava.version>
5153

5254

5355
</properties>
@@ -74,6 +76,11 @@
7476
<artifactId>commons-lang3</artifactId>
7577
<version>${commons-lang3.version}</version>
7678
</dependency>
79+
<dependency>
80+
<groupId>org.apache.commons</groupId>
81+
<artifactId>commons-collections4</artifactId>
82+
<version>${commons-collections4.version}</version>
83+
</dependency>
7784
<dependency>
7885
<groupId>org.apache.commons</groupId>
7986
<artifactId>commons-io</artifactId>
@@ -263,6 +270,12 @@
263270
<version>2.4</version>
264271
<classifier>jdk15</classifier>
265272
</dependency>
273+
274+
<dependency>
275+
<groupId>com.google.guava</groupId>
276+
<artifactId>guava</artifactId>
277+
<version>${guava.version}</version>
278+
</dependency>
266279
</dependencies>
267280
</dependencyManagement>
268281

0 commit comments

Comments
 (0)