| 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; |
| 17 | |
| 18 | import java.io.Serializable; |
| 19 | |
| 20 | /** |
| 21 | * Represents a contribution to a {@link StepExecution}, buffering changes until |
| 22 | * they can be applied at a chunk boundary. |
| 23 | * |
| 24 | * @author Dave Syer |
| 25 | * |
| 26 | */ |
| 27 | @SuppressWarnings("serial") |
| 28 | public class StepContribution implements Serializable { |
| 29 | |
| 30 | private volatile int readCount = 0; |
| 31 | |
| 32 | private volatile int writeCount = 0; |
| 33 | |
| 34 | private volatile int filterCount = 0; |
| 35 | |
| 36 | private final int parentSkipCount; |
| 37 | |
| 38 | private volatile int readSkipCount; |
| 39 | |
| 40 | private volatile int writeSkipCount; |
| 41 | |
| 42 | private volatile int processSkipCount; |
| 43 | |
| 44 | private ExitStatus exitStatus = ExitStatus.EXECUTING; |
| 45 | |
| 46 | /** |
| 47 | * @param execution |
| 48 | */ |
| 49 | public StepContribution(StepExecution execution) { |
| 50 | this.parentSkipCount = execution.getSkipCount(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Set the {@link ExitStatus} for this contribution. |
| 55 | * |
| 56 | * @param status |
| 57 | */ |
| 58 | public void setExitStatus(ExitStatus status) { |
| 59 | this.exitStatus = status; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Public getter for the status. |
| 64 | * |
| 65 | * @return the {@link ExitStatus} for this contribution |
| 66 | */ |
| 67 | public ExitStatus getExitStatus() { |
| 68 | return exitStatus; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Increment the counter for the number of items processed. |
| 73 | */ |
| 74 | public void incrementFilterCount(int count) { |
| 75 | filterCount += count; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Increment the counter for the number of items read. |
| 80 | */ |
| 81 | public void incrementReadCount() { |
| 82 | readCount++; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Increment the counter for the number of items written. |
| 87 | */ |
| 88 | public void incrementWriteCount(int count) { |
| 89 | writeCount += count; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Public access to the read counter. |
| 94 | * |
| 95 | * @return the item counter. |
| 96 | */ |
| 97 | public int getReadCount() { |
| 98 | return readCount; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Public access to the write counter. |
| 103 | * |
| 104 | * @return the item counter. |
| 105 | */ |
| 106 | public int getWriteCount() { |
| 107 | return writeCount; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Public getter for the filter counter. |
| 112 | * @return the filter counter |
| 113 | */ |
| 114 | public int getFilterCount() { |
| 115 | return filterCount; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @return the sum of skips accumulated in the parent {@link StepExecution} |
| 120 | * and this <code>StepContribution</code>. |
| 121 | */ |
| 122 | public int getStepSkipCount() { |
| 123 | return readSkipCount + writeSkipCount + processSkipCount + parentSkipCount; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @return the number of skips collected in this |
| 128 | * <code>StepContribution</code> (not including skips accumulated in the |
| 129 | * parent {@link StepExecution}). |
| 130 | */ |
| 131 | public int getSkipCount() { |
| 132 | return readSkipCount + writeSkipCount + processSkipCount; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Increment the read skip count for this contribution |
| 137 | */ |
| 138 | public void incrementReadSkipCount() { |
| 139 | readSkipCount++; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Increment the read skip count for this contribution |
| 144 | */ |
| 145 | public void incrementReadSkipCount(int count) { |
| 146 | readSkipCount += count; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Increment the write skip count for this contribution |
| 151 | */ |
| 152 | public void incrementWriteSkipCount() { |
| 153 | writeSkipCount++; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * |
| 158 | */ |
| 159 | public void incrementProcessSkipCount() { |
| 160 | processSkipCount++; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * @return the read skip count |
| 165 | */ |
| 166 | public int getReadSkipCount() { |
| 167 | return readSkipCount; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * @return the write skip count |
| 172 | */ |
| 173 | public int getWriteSkipCount() { |
| 174 | return writeSkipCount; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Public getter for the process skip count. |
| 179 | * @return the process skip count |
| 180 | */ |
| 181 | public int getProcessSkipCount() { |
| 182 | return processSkipCount; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * (non-Javadoc) |
| 187 | * |
| 188 | * @see java.lang.Object#toString() |
| 189 | */ |
| 190 | @Override |
| 191 | public String toString() { |
| 192 | return "[StepContribution: read=" + readCount + ", written=" + writeCount + ", filtered=" + filterCount |
| 193 | + ", readSkips=" + readSkipCount + ", writeSkips=" + writeSkipCount + ", processSkips=" |
| 194 | + processSkipCount + ", exitStatus=" + exitStatus.getExitCode() + "]"; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * @see java.lang.Object#equals(java.lang.Object) |
| 199 | */ |
| 200 | @Override |
| 201 | public boolean equals(Object obj) { |
| 202 | if (!(obj instanceof StepContribution)) { |
| 203 | return false; |
| 204 | } |
| 205 | StepContribution other = (StepContribution) obj; |
| 206 | return toString().equals(other.toString()); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * @see java.lang.Object#hashCode() |
| 211 | */ |
| 212 | @Override |
| 213 | public int hashCode() { |
| 214 | return 11 + toString().hashCode() * 43; |
| 215 | } |
| 216 | |
| 217 | } |