View Javadoc

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