001/*
002 * Copyright 2006-2013 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.springframework.batch.core;
017
018import java.io.Serializable;
019
020/**
021 * Represents a contribution to a {@link StepExecution}, buffering changes until
022 * they can be applied at a chunk boundary.
023 *
024 * @author Dave Syer
025 *
026 */
027@SuppressWarnings("serial")
028public class StepContribution implements Serializable {
029
030        private volatile int readCount = 0;
031
032        private volatile int writeCount = 0;
033
034        private volatile int filterCount = 0;
035
036        private final int parentSkipCount;
037
038        private volatile int readSkipCount;
039
040        private volatile int writeSkipCount;
041
042        private volatile int processSkipCount;
043
044        private ExitStatus exitStatus = ExitStatus.EXECUTING;
045
046        /**
047         * @param execution {@link StepExecution} the stepExecution used to initialize
048         * {@code skipCount}.
049         */
050        public StepContribution(StepExecution execution) {
051                this.parentSkipCount = execution.getSkipCount();
052        }
053
054        /**
055         * Set the {@link ExitStatus} for this contribution.
056         *
057         * @param status {@link ExitStatus} instance to be used to set the exit status.
058         */
059        public void setExitStatus(ExitStatus status) {
060                this.exitStatus = status;
061        }
062
063        /**
064         * Public getter for the status.
065         *
066         * @return the {@link ExitStatus} for this contribution
067         */
068        public ExitStatus getExitStatus() {
069                return exitStatus;
070        }
071
072        /**
073         * Increment the counter for the number of items processed.
074         *
075         * @param count int amount to increment by.
076         */
077        public void incrementFilterCount(int count) {
078                filterCount += count;
079        }
080
081        /**
082         * Increment the counter for the number of items read.
083         */
084        public void incrementReadCount() {
085                readCount++;
086        }
087
088        /**
089         * Increment the counter for the number of items written.
090         *
091         * @param count int amount to increment by.
092         */
093        public void incrementWriteCount(int count) {
094                writeCount += count;
095        }
096
097        /**
098         * Public access to the read counter.
099         *
100         * @return the item counter.
101         */
102        public int getReadCount() {
103                return readCount;
104        }
105
106        /**
107         * Public access to the write counter.
108         *
109         * @return the item counter.
110         */
111        public int getWriteCount() {
112                return writeCount;
113        }
114
115        /**
116         * Public getter for the filter counter.
117         *
118         * @return the filter counter
119         */
120        public int getFilterCount() {
121                return filterCount;
122        }
123
124        /**
125         * @return the sum of skips accumulated in the parent {@link StepExecution}
126         * and this <code>StepContribution</code>.
127         */
128        public int getStepSkipCount() {
129                return readSkipCount + writeSkipCount + processSkipCount + parentSkipCount;
130        }
131
132        /**
133         * @return the number of skips collected in this
134         * <code>StepContribution</code> (not including skips accumulated in the
135         * parent {@link StepExecution}).
136         */
137        public int getSkipCount() {
138                return readSkipCount + writeSkipCount + processSkipCount;
139        }
140
141        /**
142         * Increment the read skip count for this contribution
143         */
144        public void incrementReadSkipCount() {
145                readSkipCount++;
146        }
147
148        /**
149         * Increment the read skip count for this contribution
150         *
151         * @param count int amount to increment by.
152         */
153        public void incrementReadSkipCount(int count) {
154                readSkipCount += count;
155        }
156
157        /**
158         * Increment the write skip count for this contribution
159         */
160        public void incrementWriteSkipCount() {
161                writeSkipCount++;
162        }
163
164        /**
165         *
166         */
167        public void incrementProcessSkipCount() {
168                processSkipCount++;
169        }
170
171        /**
172         * @return the read skip count
173         */
174        public int getReadSkipCount() {
175                return readSkipCount;
176        }
177
178        /**
179         * @return the write skip count
180         */
181        public int getWriteSkipCount() {
182                return writeSkipCount;
183        }
184
185        /**
186         * Public getter for the process skip count.
187         *
188         * @return the process skip count
189         */
190        public int getProcessSkipCount() {
191                return processSkipCount;
192        }
193
194        /*
195         * (non-Javadoc)
196         *
197         * @see java.lang.Object#toString()
198         */
199        @Override
200        public String toString() {
201                return "[StepContribution: read=" + readCount + ", written=" + writeCount + ", filtered=" + filterCount
202                                + ", readSkips=" + readSkipCount + ", writeSkips=" + writeSkipCount + ", processSkips="
203                                + processSkipCount + ", exitStatus=" + exitStatus.getExitCode() + "]";
204        }
205
206        /**
207         * @see java.lang.Object#equals(java.lang.Object)
208         */
209        @Override
210        public boolean equals(Object obj) {
211                if (!(obj instanceof StepContribution)) {
212                        return false;
213                }
214                StepContribution other = (StepContribution) obj;
215                return toString().equals(other.toString());
216        }
217
218        /**
219         * @see java.lang.Object#hashCode()
220         */
221        @Override
222        public int hashCode() {
223                return 11 + toString().hashCode() * 43;
224        }
225
226}