View Javadoc

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