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 | |
17 | package org.springframework.batch.core; |
18 | |
19 | import java.io.ObjectStreamException; |
20 | import java.io.Serializable; |
21 | |
22 | /** |
23 | * Typesafe enumeration representing the status of an artifact within the batch environment. See Effective Java |
24 | * Programming by Joshua Bloch for more details on the pattern used. |
25 | * |
26 | * A BatchStatus can be safely serialized, however, it should be noted that the pattern can break down if different |
27 | * class loaders load the enumeration. |
28 | * |
29 | * @author Lucas Ward |
30 | * @author Greg Kick |
31 | */ |
32 | |
33 | public class BatchStatus implements Serializable { |
34 | |
35 | private static final long serialVersionUID = 1634960297477743037L; |
36 | |
37 | private final String name; |
38 | |
39 | private BatchStatus(String name) { |
40 | this.name = name; |
41 | } |
42 | |
43 | private Object readResolve() throws ObjectStreamException { |
44 | return getStatus(name); |
45 | } |
46 | |
47 | public String toString() { |
48 | return name; |
49 | } |
50 | |
51 | public static final BatchStatus COMPLETED = new BatchStatus("COMPLETED"); |
52 | |
53 | public static final BatchStatus STARTED = new BatchStatus("STARTED"); |
54 | |
55 | public static final BatchStatus STARTING = new BatchStatus("STARTING"); |
56 | |
57 | public static final BatchStatus FAILED = new BatchStatus("FAILED"); |
58 | |
59 | public static final BatchStatus STOPPING = new BatchStatus("STOPPING"); |
60 | |
61 | public static final BatchStatus STOPPED = new BatchStatus("STOPPED"); |
62 | |
63 | public static final BatchStatus UNKNOWN = new BatchStatus("UNKNOWN"); |
64 | |
65 | private static final BatchStatus[] VALUES = { STARTING, STARTED, COMPLETED, FAILED, STOPPING, STOPPED, UNKNOWN }; |
66 | |
67 | /** |
68 | * Given a string representation of a status, return the appropriate BatchStatus. |
69 | * |
70 | * @param statusAsString string representation of a status |
71 | * @return a valid BatchStatus or null if the input is null |
72 | * @throws IllegalArgumentException if no status matches provided string. |
73 | */ |
74 | public static BatchStatus getStatus(String statusAsString) { |
75 | if (statusAsString == null) { |
76 | return null; |
77 | } |
78 | final String upperCaseStatusAsString = statusAsString.toUpperCase(); |
79 | for (int i = 0; i < VALUES.length; i++) { |
80 | if (VALUES[i].toString().equals(upperCaseStatusAsString)) { |
81 | return VALUES[i]; |
82 | } |
83 | } |
84 | throw new IllegalArgumentException("The string did not match a valid status."); |
85 | } |
86 | } |