| 1 | /* |
| 2 | * Copyright 2006-2013 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 | * Represents the status of {@link FlowExecution}. |
| 20 | * |
| 21 | * @author Dan Garrette |
| 22 | * @author Dave Syer |
| 23 | * @since 2.0 |
| 24 | */ |
| 25 | public class FlowExecutionStatus implements Comparable<FlowExecutionStatus> { |
| 26 | |
| 27 | /** |
| 28 | * Special well-known status value. |
| 29 | */ |
| 30 | public static final FlowExecutionStatus COMPLETED = new FlowExecutionStatus(Status.COMPLETED.toString()); |
| 31 | |
| 32 | /** |
| 33 | * Special well-known status value. |
| 34 | */ |
| 35 | public static final FlowExecutionStatus STOPPED = new FlowExecutionStatus(Status.STOPPED.toString()); |
| 36 | |
| 37 | /** |
| 38 | * Special well-known status value. |
| 39 | */ |
| 40 | public static final FlowExecutionStatus FAILED = new FlowExecutionStatus(Status.FAILED.toString()); |
| 41 | |
| 42 | /** |
| 43 | * Special well-known status value. |
| 44 | */ |
| 45 | public static final FlowExecutionStatus UNKNOWN = new FlowExecutionStatus(Status.UNKNOWN.toString()); |
| 46 | |
| 47 | private final String name; |
| 48 | |
| 49 | private enum Status { |
| 50 | |
| 51 | COMPLETED, STOPPED, FAILED, UNKNOWN; |
| 52 | |
| 53 | static Status match(String value) { |
| 54 | for (int i = 0; i < values().length; i++) { |
| 55 | Status status = values()[i]; |
| 56 | if (value.startsWith(status.toString())) { |
| 57 | return status; |
| 58 | } |
| 59 | } |
| 60 | // Default match should be the lowest priority |
| 61 | return COMPLETED; |
| 62 | } |
| 63 | |
| 64 | }; |
| 65 | |
| 66 | /** |
| 67 | * @param status |
| 68 | */ |
| 69 | public FlowExecutionStatus(String status) { |
| 70 | this.name = status; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @return true if the status starts with "STOPPED" |
| 75 | */ |
| 76 | public boolean isStop() { |
| 77 | return name.startsWith(STOPPED.getName()); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @return true if the status starts with "FAILED" |
| 82 | */ |
| 83 | public boolean isFail() { |
| 84 | return name.startsWith(FAILED.getName()); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | /** |
| 89 | * @return true if this status represents the end of a flow |
| 90 | */ |
| 91 | public boolean isEnd() { |
| 92 | return isStop() || isFail() || isComplete(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @return true if the status starts with "COMPLETED" |
| 97 | */ |
| 98 | private boolean isComplete() { |
| 99 | return name.startsWith(COMPLETED.getName()); |
| 100 | } |
| 101 | /** |
| 102 | * Create an ordering on {@link FlowExecutionStatus} instances by comparing |
| 103 | * their statuses. |
| 104 | * |
| 105 | * @see Comparable#compareTo(Object) |
| 106 | * |
| 107 | * @param other |
| 108 | * @return negative, zero or positive as per the contract |
| 109 | */ |
| 110 | @Override |
| 111 | public int compareTo(FlowExecutionStatus other) { |
| 112 | Status one = Status.match(this.name); |
| 113 | Status two = Status.match(other.name); |
| 114 | int comparison = one.compareTo(two); |
| 115 | if (comparison == 0) { |
| 116 | return this.name.compareTo(other.name); |
| 117 | } |
| 118 | return comparison; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Check the equality of the statuses. |
| 123 | * |
| 124 | * @see java.lang.Object#equals(java.lang.Object) |
| 125 | */ |
| 126 | @Override |
| 127 | public boolean equals(Object object) { |
| 128 | if (object == this) { |
| 129 | return true; |
| 130 | } |
| 131 | if (!(object instanceof FlowExecutionStatus)) { |
| 132 | return false; |
| 133 | } |
| 134 | FlowExecutionStatus other = (FlowExecutionStatus) object; |
| 135 | return name.equals(other.name); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public int hashCode() { |
| 140 | return name.hashCode(); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @see Object#toString() |
| 145 | */ |
| 146 | @Override |
| 147 | public String toString() { |
| 148 | return name; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @return the name of this status |
| 153 | */ |
| 154 | public String getName() { |
| 155 | return name; |
| 156 | } |
| 157 | |
| 158 | } |