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 /** 20 * Enumeration representing the status of a an Execution. 21 * 22 * @author Lucas Ward 23 * @author Dave Syer 24 */ 25 public enum BatchStatus { 26 27 /** 28 * The order of the status values is significant because it can be used to 29 * aggregate a set of status values - the result should be the maximum 30 * value. Since COMPLETED is first in the order, only if all elements of an 31 * execution are COMPLETED will the aggregate status be COMPLETED. A running 32 * execution is expected to move from STARTING to STARTED to COMPLETED 33 * (through the order defined by {@link #upgradeTo(BatchStatus)}). Higher 34 * values than STARTED signify more serious failure. ABANDONED is used for 35 * steps that have finished processing, but were not successful, and where 36 * they should be skipped on a restart (so FAILED is the wrong status). 37 */ 38 COMPLETED, STARTING, STARTED, STOPPING, STOPPED, FAILED, ABANDONED, UNKNOWN; 39 40 public static BatchStatus max(BatchStatus status1, BatchStatus status2) { 41 return status1.isGreaterThan(status2) ? status1 : status2; 42 } 43 44 /** 45 * Convenience method to decide if a status indicates work is in progress. 46 * 47 * @return true if the status is STARTING, STARTED 48 */ 49 public boolean isRunning() { 50 return this == STARTING || this == STARTED; 51 } 52 53 /** 54 * Convenience method to decide if a status indicates execution was 55 * unsuccessful. 56 * 57 * @return true if the status is FAILED or greater 58 */ 59 public boolean isUnsuccessful() { 60 return this == FAILED || this.isGreaterThan(FAILED); 61 } 62 63 /** 64 * Method used to move status values through their logical progression, and 65 * override less severe failures with more severe ones. This value is 66 * compared with the parameter and the one that has higher priority is 67 * returned. If both are STARTED or less than the value returned is the 68 * largest in the sequence STARTING, STARTED, COMPLETED. Otherwise the value 69 * returned is the maximum of the two. 70 * 71 * @param other another status to compare to 72 * @return either this or the other status depending on their priority 73 */ 74 public BatchStatus upgradeTo(BatchStatus other) { 75 if (isGreaterThan(STARTED) || other.isGreaterThan(STARTED)) { 76 return max(this, other); 77 } 78 // Both less than or equal to STARTED 79 if (this == COMPLETED || other == COMPLETED) 80 return COMPLETED; 81 return max(this, other); 82 } 83 84 /** 85 * @param other a status value to compare 86 * @return true if this is greater than other 87 */ 88 public boolean isGreaterThan(BatchStatus other) { 89 return this.compareTo(other) > 0; 90 } 91 92 /** 93 * @param other a status value to compare 94 * @return true if this is less than other 95 */ 96 public boolean isLessThan(BatchStatus other) { 97 return this.compareTo(other) < 0; 98 } 99 100 /** 101 * @param other a status value to compare 102 * @return true if this is less than other 103 */ 104 public boolean isLessThanOrEqualTo(BatchStatus other) { 105 return this.compareTo(other) <= 0; 106 } 107 108 /** 109 * Find a BatchStatus that matches the beginning of the given value. If no 110 * match is found, return COMPLETED as the default because has is low 111 * precedence. 112 * 113 * @param value a string representing a status 114 * @return a BatchStatus 115 */ 116 public static BatchStatus match(String value) { 117 for (BatchStatus status : values()) { 118 if (value.startsWith(status.toString())) { 119 return status; 120 } 121 } 122 // Default match should be the lowest priority 123 return COMPLETED; 124 } 125 }