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 */
016
017package org.springframework.batch.core.partition.support;
018
019import org.springframework.batch.core.BatchStatus;
020import org.springframework.batch.core.ExitStatus;
021import org.springframework.batch.core.StepExecution;
022import org.springframework.util.Assert;
023
024import java.util.Collection;
025
026/**
027 * Convenience class for aggregating a set of {@link StepExecution} instances
028 * into a single result.
029 *
030 * @author Dave Syer
031 * @since 2.1
032 */
033public class DefaultStepExecutionAggregator implements StepExecutionAggregator {
034
035        /**
036         * Aggregates the input executions into the result {@link StepExecution}.
037         * The aggregated fields are
038         * <ul>
039         * <li>status - choosing the highest value using
040         * {@link BatchStatus#max(BatchStatus, BatchStatus)}</li>
041         * <li>exitStatus - using {@link ExitStatus#and(ExitStatus)}</li>
042         * <li>commitCount, rollbackCount, etc. - by arithmetic sum</li>
043         * </ul>
044         * @see StepExecutionAggregator #aggregate(StepExecution, Collection)
045         */
046        @Override
047        public void aggregate(StepExecution result, Collection<StepExecution> executions) {
048                Assert.notNull(result, "To aggregate into a result it must be non-null.");
049                if (executions == null) {
050                        return;
051                }
052                for (StepExecution stepExecution : executions) {
053                        BatchStatus status = stepExecution.getStatus();
054                        result.setStatus(BatchStatus.max(result.getStatus(), status));
055                        result.setExitStatus(result.getExitStatus().and(stepExecution.getExitStatus()));
056                        result.setFilterCount(result.getFilterCount() + stepExecution.getFilterCount());
057                        result.setProcessSkipCount(result.getProcessSkipCount() + stepExecution.getProcessSkipCount());
058                        result.setCommitCount(result.getCommitCount() + stepExecution.getCommitCount());
059                        result.setRollbackCount(result.getRollbackCount() + stepExecution.getRollbackCount());
060                        result.setReadCount(result.getReadCount() + stepExecution.getReadCount());
061                        result.setReadSkipCount(result.getReadSkipCount() + stepExecution.getReadSkipCount());
062                        result.setWriteCount(result.getWriteCount() + stepExecution.getWriteCount());
063                        result.setWriteSkipCount(result.getWriteSkipCount() + stepExecution.getWriteSkipCount());
064                }
065        }
066
067}