| 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 | |
| 17 | package org.springframework.batch.core.partition.support; |
| 18 | |
| 19 | import java.util.Collection; |
| 20 | |
| 21 | import org.springframework.batch.core.BatchStatus; |
| 22 | import org.springframework.batch.core.ExitStatus; |
| 23 | import org.springframework.batch.core.StepExecution; |
| 24 | import org.springframework.util.Assert; |
| 25 | |
| 26 | /** |
| 27 | * Convenience class for aggregating a set of {@link StepExecution} instances |
| 28 | * into a single result. |
| 29 | * |
| 30 | * @author Dave Syer |
| 31 | * @since 2.1 |
| 32 | */ |
| 33 | public class DefaultStepExecutionAggregator implements StepExecutionAggregator { |
| 34 | |
| 35 | /** |
| 36 | * Aggregates the input executions into the result {@link StepExecution}. |
| 37 | * The aggregated fields are |
| 38 | * <ul> |
| 39 | * <li>status - choosing the highest value using |
| 40 | * {@link BatchStatus#max(BatchStatus, BatchStatus)}</li> |
| 41 | * <li>exitStatus - using {@link ExitStatus#and(ExitStatus)}</li> |
| 42 | * <li>commitCount, rollbackCount, etc. - by arithmetic sum</li> |
| 43 | * </ul> |
| 44 | * @see StepExecutionAggregator #aggregate(StepExecution, Collection) |
| 45 | */ |
| 46 | @Override |
| 47 | public void aggregate(StepExecution result, Collection<StepExecution> executions) { |
| 48 | Assert.notNull(result, "To aggregate into a result it must be non-null."); |
| 49 | if (executions == null) { |
| 50 | return; |
| 51 | } |
| 52 | for (StepExecution stepExecution : executions) { |
| 53 | BatchStatus status = stepExecution.getStatus(); |
| 54 | result.setStatus(BatchStatus.max(result.getStatus(), status)); |
| 55 | result.setExitStatus(result.getExitStatus().and(stepExecution.getExitStatus())); |
| 56 | result.setCommitCount(result.getCommitCount() + stepExecution.getCommitCount()); |
| 57 | result.setRollbackCount(result.getRollbackCount() + stepExecution.getRollbackCount()); |
| 58 | result.setReadCount(result.getReadCount() + stepExecution.getReadCount()); |
| 59 | result.setReadSkipCount(result.getReadSkipCount() + stepExecution.getReadSkipCount()); |
| 60 | result.setWriteCount(result.getWriteCount() + stepExecution.getWriteCount()); |
| 61 | result.setWriteSkipCount(result.getWriteSkipCount() + stepExecution.getWriteSkipCount()); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | } |