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.repeat.context; |
18 | |
19 | import org.springframework.batch.repeat.RepeatContext; |
20 | import org.springframework.util.Assert; |
21 | |
22 | /** |
23 | * Helper class for policies that need to count the number of occurrences of |
24 | * some event (e.g. an exception type in the context) in the scope of a batch. |
25 | * The value of the counter can be stored between batches in a nested context, |
26 | * so that the termination decision is based on the aggregate of a number of |
27 | * sibling batches. |
28 | * |
29 | * @author Dave Syer |
30 | * |
31 | */ |
32 | public class RepeatContextCounter { |
33 | |
34 | private String countKey; |
35 | |
36 | /** |
37 | * Flag to indicate whether the count is stored at the level of the parent |
38 | * context, or just local to the current context. Default value is false. |
39 | */ |
40 | private boolean useParent = false; |
41 | |
42 | private RepeatContext context; |
43 | |
44 | /** |
45 | * Increment the counter. |
46 | * |
47 | * @param delta the amount by which to increment the counter. |
48 | */ |
49 | final public void increment(int delta) { |
50 | AtomicCounter count = getCounter(); |
51 | count.addAndGet(delta); |
52 | } |
53 | |
54 | /** |
55 | * Increment by 1. |
56 | */ |
57 | final public void increment() { |
58 | increment(1); |
59 | } |
60 | |
61 | /** |
62 | * Convenience constructor with useParent=false. |
63 | * @param context the current context. |
64 | * @param countKey the key to use to store the counter in the context. |
65 | */ |
66 | public RepeatContextCounter(RepeatContext context, String countKey) { |
67 | this(context, countKey, false); |
68 | } |
69 | |
70 | /** |
71 | * Construct a new {@link RepeatContextCounter}. |
72 | * |
73 | * @param context the current context. |
74 | * @param countKey the key to use to store the counter in the context. |
75 | * @param useParent true if the counter is to be shared between siblings. |
76 | * The state will be stored in the parent of the context (if it exists) |
77 | * instead of the context itself. |
78 | */ |
79 | public RepeatContextCounter(RepeatContext context, String countKey, boolean useParent) { |
80 | |
81 | super(); |
82 | |
83 | Assert.notNull(context, "The context must be provided"); |
84 | |
85 | this.countKey = countKey; |
86 | this.useParent = useParent; |
87 | |
88 | RepeatContext parent = context.getParent(); |
89 | |
90 | if (this.useParent && parent != null) { |
91 | this.context = parent; |
92 | } |
93 | else { |
94 | this.context = context; |
95 | } |
96 | if (!this.context.hasAttribute(countKey)) { |
97 | AtomicCounterFactory factory = new AtomicCounterFactory(); |
98 | this.context.setAttribute(countKey, factory.getAtomicCounter()); |
99 | } |
100 | |
101 | } |
102 | |
103 | /** |
104 | * @return the current value of the counter |
105 | */ |
106 | public int getCount() { |
107 | return getCounter().intValue(); |
108 | } |
109 | |
110 | private AtomicCounter getCounter() { |
111 | return ((AtomicCounter) context.getAttribute(countKey)); |
112 | } |
113 | |
114 | } |