Skip to content

Commit e68a413

Browse files
author
lucasward
committed
BATCH-671: Added a new class, JobParameter, that represents an individual parameter. Also converted BatchStatus and ParameterType typesafe enumerations to java 5 enums.
1 parent 170d9d5 commit e68a413

19 files changed

+416
-482
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/BatchStatus.java

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -16,73 +16,14 @@
1616

1717
package org.springframework.batch.core;
1818

19-
import java.io.ObjectStreamException;
20-
import java.io.Serializable;
2119

2220
/**
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-
* This class is immutable and therefore thread-safe.
21+
* Enumeration representing the status of a an Execution.
3022
*
3123
* @author Lucas Ward
32-
* @author Greg Kick
3324
*/
3425

35-
public class BatchStatus implements Serializable {
36-
37-
private static final long serialVersionUID = 1634960297477743037L;
38-
39-
private final String name;
40-
41-
private BatchStatus(String name) {
42-
this.name = name;
43-
}
44-
45-
private Object readResolve() throws ObjectStreamException {
46-
return getStatus(name);
47-
}
48-
49-
public String toString() {
50-
return name;
51-
}
52-
53-
public static final BatchStatus COMPLETED = new BatchStatus("COMPLETED");
54-
55-
public static final BatchStatus STARTED = new BatchStatus("STARTED");
56-
57-
public static final BatchStatus STARTING = new BatchStatus("STARTING");
58-
59-
public static final BatchStatus FAILED = new BatchStatus("FAILED");
60-
61-
public static final BatchStatus STOPPING = new BatchStatus("STOPPING");
62-
63-
public static final BatchStatus STOPPED = new BatchStatus("STOPPED");
64-
65-
public static final BatchStatus UNKNOWN = new BatchStatus("UNKNOWN");
66-
67-
private static final BatchStatus[] VALUES = { STARTING, STARTED, COMPLETED, FAILED, STOPPING, STOPPED, UNKNOWN };
26+
public enum BatchStatus {
6827

69-
/**
70-
* Given a string representation of a status, return the appropriate BatchStatus.
71-
*
72-
* @param statusAsString string representation of a status
73-
* @return a valid BatchStatus or null if the input is null
74-
* @throws IllegalArgumentException if no status matches provided string.
75-
*/
76-
public static BatchStatus getStatus(String statusAsString) {
77-
if (statusAsString == null) {
78-
return null;
79-
}
80-
final String upperCaseStatusAsString = statusAsString.toUpperCase();
81-
for (int i = 0; i < VALUES.length; i++) {
82-
if (VALUES[i].toString().equals(upperCaseStatusAsString)) {
83-
return VALUES[i];
84-
}
85-
}
86-
throw new IllegalArgumentException("The string did not match a valid status.");
87-
}
28+
COMPLETED, STARTED, STARTING, FAILED, STOPPING, STOPPED, UNKNOWN;
8829
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
*
3+
*/
4+
package org.springframework.batch.core;
5+
6+
import java.io.Serializable;
7+
import java.util.Date;
8+
9+
import org.apache.commons.lang.builder.HashCodeBuilder;
10+
11+
/**
12+
* Domain representation of a parameter to a batch job. Only the following types can be
13+
* parameters: String, Long, Date, and Double.
14+
*
15+
* @author Lucas Ward
16+
* @since 2.0
17+
*
18+
*/
19+
public class JobParameter implements Serializable{
20+
21+
private final Object parameter;
22+
private final ParameterType parameterType;
23+
24+
/**
25+
* Construct a new JobParameter as a String.
26+
*/
27+
public JobParameter(String parameter){
28+
this.parameter = parameter;
29+
parameterType = ParameterType.STRING;
30+
}
31+
32+
/**
33+
* Construct a new JobParameter as a Long.
34+
*
35+
* @param parameter
36+
*/
37+
public JobParameter(Long parameter){
38+
this.parameter = parameter;
39+
parameterType = ParameterType.LONG;
40+
}
41+
42+
/**
43+
* Construct a new JobParameter as a Date.
44+
*
45+
* @param parameter
46+
*/
47+
public JobParameter(Date parameter) {
48+
this.parameter = new Date(parameter.getTime());
49+
parameterType = ParameterType.DATE;
50+
}
51+
52+
/**
53+
* Construct a new JobParameter as a Double.
54+
*
55+
* @param parameter
56+
*/
57+
public JobParameter(Double parameter){
58+
this.parameter = parameter;
59+
parameterType = ParameterType.DOUBLE;
60+
}
61+
62+
/**
63+
* @return the value contained within this JobParameter.
64+
*/
65+
public Object getValue(){
66+
67+
if(parameter.getClass().isInstance(Date.class)){
68+
return new Date(((Date)parameter).getTime());
69+
}
70+
else{
71+
return parameter;
72+
}
73+
}
74+
75+
/**
76+
* @return a ParameterType representing the type of this parameter.
77+
*/
78+
public ParameterType getType(){
79+
return parameterType;
80+
}
81+
82+
@Override
83+
public boolean equals(Object obj) {
84+
if(obj instanceof JobParameter == false){
85+
return false;
86+
}
87+
88+
if(this == obj){
89+
return true;
90+
}
91+
92+
JobParameter rhs = (JobParameter)obj;
93+
return this.parameter.equals(rhs.parameter);
94+
}
95+
96+
@Override
97+
public String toString() {
98+
return parameter.toString();
99+
}
100+
101+
public int hashCode() {
102+
return new HashCodeBuilder(7, 21).append(parameter).toHashCode();
103+
}
104+
105+
/**
106+
* Enumeration representing the type of a JobParameter.
107+
*/
108+
public enum ParameterType{
109+
110+
STRING, DATE, LONG, DOUBLE;
111+
}
112+
}

0 commit comments

Comments
 (0)