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 | |
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.0 |
32 | */ |
33 | public class StepExecutionAggregator { |
34 | |
35 | /** |
36 | * Take the inputs and aggregate certain fields, putting the aggregates into |
37 | * the result. The aggregated fields are |
38 | * <ul> |
39 | * <li>status - choosing the highest value using {@link BatchStatus#max(BatchStatus, BatchStatus)}</li> |
40 | * <li>exitStatus - using {@link ExitStatus#and(ExitStatus)}</li> |
41 | * <li>commitCount, rollbackCount, etc. - by arithmetic sum</li> |
42 | * </ul> |
43 | * |
44 | * @param result the result to overwrite |
45 | * @param executions the inputs |
46 | */ |
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 || executions.isEmpty()) { |
50 | throw new IllegalArgumentException("Cannot aggregate empty or null executions: " + executions); |
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 | } |