EMMA Coverage Report (generated Fri Jan 30 13:20:29 EST 2009)
[all classes][org.springframework.batch.repeat.policy]

COVERAGE SUMMARY FOR SOURCE FILE [CountingCompletionPolicy.java]

nameclass, %method, %block, %line, %
CountingCompletionPolicy.java100% (2/2)100% (10/10)99%  (87/88)100% (21.9/22)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CountingCompletionPolicy100% (1/1)100% (8/8)99%  (68/69)100% (16.9/17)
<static initializer> 100% (1/1)94%  (17/18)94%  (0.9/1)
CountingCompletionPolicy (): void 100% (1/1)100% (9/9)100% (4/4)
doUpdate (RepeatContext): int 100% (1/1)100% (2/2)100% (1/1)
isComplete (RepeatContext): boolean 100% (1/1)100% (13/13)100% (2/2)
setMaxCount (int): void 100% (1/1)100% (4/4)100% (2/2)
setUseParent (boolean): void 100% (1/1)100% (4/4)100% (2/2)
start (RepeatContext): RepeatContext 100% (1/1)100% (6/6)100% (1/1)
update (RepeatContext): void 100% (1/1)100% (13/13)100% (4/4)
     
class CountingCompletionPolicy$CountingBatchContext100% (1/1)100% (2/2)100% (19/19)100% (5/5)
CountingCompletionPolicy$CountingBatchContext (CountingCompletionPolicy, Repe... 100% (1/1)100% (16/16)100% (4/4)
getCounter (): RepeatContextCounter 100% (1/1)100% (3/3)100% (1/1)

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 
17package org.springframework.batch.repeat.policy;
18 
19import org.springframework.batch.repeat.RepeatContext;
20import org.springframework.batch.repeat.context.RepeatContextCounter;
21import org.springframework.batch.repeat.context.RepeatContextSupport;
22 
23/**
24 * Abstract base class for policies that need to count the number of occurrences
25 * of some event (e.g. an exception type in the context), and terminate based on
26 * a limit for the counter. The value of the counter can be stored between
27 * batches in a nested context, so that the termination decision is based on the
28 * aggregate of a number of sibling batches.
29 * 
30 * @author Dave Syer
31 * 
32 */
33public abstract class CountingCompletionPolicy extends DefaultResultCompletionPolicy {
34 
35        /**
36         * Session key for global counter.
37         */
38        public static final String COUNT = CountingCompletionPolicy.class.getName() + ".COUNT";
39 
40        private boolean useParent = false;
41 
42        private int maxCount = 0;
43 
44        /**
45         * Flag to indicate whether the count is at the level of the parent context,
46         * or just local to the context. If true then the count is aggregated among
47         * siblings in a nested batch.
48         * 
49         * @param useParent whether to use the parent context to cache the total
50         * count. Default value is false.
51         */
52        public void setUseParent(boolean useParent) {
53                this.useParent = useParent;
54        }
55 
56        /**
57         * Setter for maximum value of count before termination.
58         * 
59         * @param maxCount the maximum number of counts before termination. Default
60         * 0 so termination is immediate.
61         */
62        public void setMaxCount(int maxCount) {
63                this.maxCount = maxCount;
64        }
65 
66        /**
67         * Extension point for subclasses. Obtain the value of the count in the
68         * current context. Subclasses can count the number of attempts or
69         * violations and store the result in their context. This policy base class
70         * will take care of the termination contract and aggregating at the level
71         * of the session if required.
72         * 
73         * @param context the current context, specific to the subclass.
74         * @return the value of the counter in the context.
75         */
76        protected abstract int getCount(RepeatContext context);
77 
78        /**
79         * Extension point for subclasses. Inspect the context and update the state
80         * of a counter in whatever way is appropriate. This will be added to the
81         * session-level counter if {@link #setUseParent(boolean)} is true.
82         * 
83         * @param context the current context.
84         * 
85         * @return the change in the value of the counter (default 0).
86         */
87        protected int doUpdate(RepeatContext context) {
88                return 0;
89        }
90 
91        /*
92         * (non-Javadoc)
93         * @see org.springframework.batch.repeat.policy.CompletionPolicySupport#isComplete(org.springframework.batch.repeat.BatchContext)
94         */
95        final public boolean isComplete(RepeatContext context) {
96                int count = ((CountingBatchContext) context).getCounter().getCount();
97                return count >= maxCount;
98        }
99 
100        /*
101         * (non-Javadoc)
102         * @see org.springframework.batch.repeat.policy.CompletionPolicySupport#start(org.springframework.batch.repeat.BatchContext)
103         */
104        public RepeatContext start(RepeatContext parent) {
105                return new CountingBatchContext(parent);
106        }
107 
108        /*
109         * (non-Javadoc)
110         * @see org.springframework.batch.repeat.policy.CompletionPolicySupport#update(org.springframework.batch.repeat.BatchContext)
111         */
112        final public void update(RepeatContext context) {
113                super.update(context);
114                int delta = doUpdate(context);
115                ((CountingBatchContext) context).getCounter().increment(delta);
116        }
117 
118        protected class CountingBatchContext extends RepeatContextSupport {
119 
120                RepeatContextCounter counter;
121 
122                public CountingBatchContext(RepeatContext parent) {
123                        super(parent);
124                        counter = new RepeatContextCounter(this, COUNT, useParent);
125                }
126 
127                public RepeatContextCounter getCounter() {
128                        return counter;
129                }
130 
131        }
132}

[all classes][org.springframework.batch.repeat.policy]
EMMA 2.0.5312 (C) Vladimir Roubtsov