1 | /* |
2 | * Copyright 2006-2007 the original author or authors. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | package org.springframework.batch.core.job.flow; |
17 | |
18 | |
19 | /** |
20 | * This class is used as a holder for a BatchStatus/ExitStatus pair. |
21 | * |
22 | * @author Dan Garrette |
23 | * @author Dave Syer |
24 | * @since 2.0 |
25 | */ |
26 | public class FlowExecutionStatus implements Comparable<FlowExecutionStatus> { |
27 | |
28 | /** |
29 | * Special well-known status value. |
30 | */ |
31 | public static final FlowExecutionStatus COMPLETED = new FlowExecutionStatus(Status.COMPLETED.toString()); |
32 | |
33 | /** |
34 | * Special well-known status value. |
35 | */ |
36 | public static final FlowExecutionStatus STOPPED = new FlowExecutionStatus(Status.STOPPED.toString()); |
37 | |
38 | /** |
39 | * Special well-known status value. |
40 | */ |
41 | public static final FlowExecutionStatus FAILED = new FlowExecutionStatus(Status.FAILED.toString()); |
42 | |
43 | /** |
44 | * Special well-known status value. |
45 | */ |
46 | public static final FlowExecutionStatus UNKNOWN = new FlowExecutionStatus(Status.UNKNOWN.toString()); |
47 | |
48 | private final String name; |
49 | |
50 | private enum Status { |
51 | |
52 | COMPLETED, STOPPED, FAILED, UNKNOWN; |
53 | |
54 | static Status match(String value) { |
55 | for (int i = 0; i < values().length; i++) { |
56 | Status status = values()[i]; |
57 | if (value.startsWith(status.toString())) { |
58 | return status; |
59 | } |
60 | } |
61 | // Default match should be the lowest priority |
62 | return COMPLETED; |
63 | } |
64 | |
65 | }; |
66 | |
67 | /** |
68 | * @param status |
69 | */ |
70 | public FlowExecutionStatus(String status) { |
71 | this.name = status; |
72 | } |
73 | |
74 | /** |
75 | * @return true if the status starts with "STOPPED" |
76 | */ |
77 | public boolean isStop() { |
78 | return name.startsWith(STOPPED.getName()); |
79 | } |
80 | |
81 | /** |
82 | * @return true if the status starts with "FAILED" |
83 | */ |
84 | public boolean isFail() { |
85 | return name.startsWith(FAILED.getName()); |
86 | } |
87 | |
88 | /** |
89 | * Create an ordering on {@link FlowExecutionStatus} instances by comparing |
90 | * their statuses. |
91 | * |
92 | * @see Comparable#compareTo(Object) |
93 | * |
94 | * @param other |
95 | * @return negative, zero or positive as per the contract |
96 | */ |
97 | public int compareTo(FlowExecutionStatus other) { |
98 | Status one = Status.match(this.name); |
99 | Status two = Status.match(other.name); |
100 | int comparison = one.compareTo(two); |
101 | if (comparison == 0) { |
102 | return this.name.compareTo(other.name); |
103 | } |
104 | return comparison; |
105 | } |
106 | |
107 | /** |
108 | * Check the equality of the statuses. |
109 | * |
110 | * @see java.lang.Object#equals(java.lang.Object) |
111 | */ |
112 | @Override |
113 | public boolean equals(Object object) { |
114 | if (object == this) { |
115 | return true; |
116 | } |
117 | if (!(object instanceof FlowExecutionStatus)) { |
118 | return false; |
119 | } |
120 | FlowExecutionStatus other = (FlowExecutionStatus) object; |
121 | return name.equals(other.name); |
122 | } |
123 | |
124 | @Override |
125 | public int hashCode() { |
126 | return name.hashCode(); |
127 | } |
128 | |
129 | /** |
130 | * @see Object#toString() |
131 | */ |
132 | @Override |
133 | public String toString() { |
134 | return name; |
135 | } |
136 | |
137 | /** |
138 | * @return the name of this status |
139 | */ |
140 | public String getName() { |
141 | return name; |
142 | } |
143 | |
144 | } |