EMMA Coverage Report (generated Thu Jan 24 13:37:04 CST 2013)
[all classes][org.springframework.batch.repeat.context]

COVERAGE SUMMARY FOR SOURCE FILE [RepeatContextSupport.java]

nameclass, %method, %block, %line, %
RepeatContextSupport.java100% (1/1)90%  (9/10)91%  (130/143)96%  (35.4/37)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class RepeatContextSupport100% (1/1)90%  (9/10)91%  (130/143)96%  (35.4/37)
isTerminateOnly (): boolean 0%   (0/1)0%   (0/3)0%   (0/1)
registerDestructionCallback (String, Runnable): void 100% (1/1)86%  (31/36)95%  (7.6/8)
close (): void 100% (1/1)93%  (62/67)98%  (13.8/14)
RepeatContextSupport (RepeatContext): void 100% (1/1)100% (11/11)100% (4/4)
getParent (): RepeatContext 100% (1/1)100% (3/3)100% (1/1)
getStartedCount (): int 100% (1/1)100% (3/3)100% (1/1)
increment (): void 100% (1/1)100% (7/7)100% (2/2)
isCompleteOnly (): boolean 100% (1/1)100% (3/3)100% (1/1)
setCompleteOnly (): void 100% (1/1)100% (4/4)100% (2/2)
setTerminateOnly (): void 100% (1/1)100% (6/6)100% (3/3)

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.context;
18 
19import java.util.ArrayList;
20import java.util.HashMap;
21import java.util.HashSet;
22import java.util.List;
23import java.util.Map;
24import java.util.Set;
25 
26import org.springframework.batch.repeat.RepeatContext;
27 
28public class RepeatContextSupport extends SynchronizedAttributeAccessor implements RepeatContext {
29 
30        private RepeatContext parent;
31 
32        private int count;
33 
34        private volatile boolean completeOnly;
35 
36        private volatile boolean terminateOnly;
37 
38        private Map<String, Set<Runnable>> callbacks = new HashMap<String, Set<Runnable>>();
39 
40        /**
41         * Constructor for {@link RepeatContextSupport}. The parent can be null, but
42         * should be set to the enclosing repeat context if there is one, e.g. if
43         * this context is an inner loop.
44         * @param parent
45         */
46        public RepeatContextSupport(RepeatContext parent) {
47                super();
48                this.parent = parent;
49        }
50 
51        /*
52         * (non-Javadoc)
53         * 
54         * @see org.springframework.batch.repeat.RepeatContext#isCompleteOnly()
55         */
56        public boolean isCompleteOnly() {
57                return completeOnly;
58        }
59 
60        /*
61         * (non-Javadoc)
62         * 
63         * @see org.springframework.batch.repeat.RepeatContext#setCompleteOnly()
64         */
65        public void setCompleteOnly() {
66                completeOnly = true;
67        }
68 
69        /*
70         * (non-Javadoc)
71         * 
72         * @see org.springframework.batch.repeat.RepeatContext#isTerminateOnly()
73         */
74        public boolean isTerminateOnly() {
75                return terminateOnly;
76        }
77 
78        /*
79         * (non-Javadoc)
80         * 
81         * @see org.springframework.batch.repeat.RepeatContext#setTerminateOnly()
82         */
83        public void setTerminateOnly() {
84                terminateOnly = true;
85                setCompleteOnly();
86        }
87 
88        /*
89         * (non-Javadoc)
90         * 
91         * @see org.springframework.batch.repeat.RepeatContext#getParent()
92         */
93        public RepeatContext getParent() {
94                return parent;
95        }
96 
97        /**
98         * Used by clients to increment the started count.
99         */
100        public synchronized void increment() {
101                count++;
102        }
103 
104        /*
105         * (non-Javadoc)
106         * 
107         * @see org.springframework.batch.repeat.RepeatContext#getStartedCount()
108         */
109        public synchronized int getStartedCount() {
110                return count;
111        }
112 
113        /*
114         * (non-Javadoc)
115         * 
116         * @see
117         * org.springframework.batch.repeat.RepeatContext#registerDestructionCallback
118         * (java.lang.String, java.lang.Runnable)
119         */
120        public void registerDestructionCallback(String name, Runnable callback) {
121                synchronized (callbacks) {
122                        Set<Runnable> set = callbacks.get(name);
123                        if (set == null) {
124                                set = new HashSet<Runnable>();
125                                callbacks.put(name, set);
126                        }
127                        set.add(callback);
128                }
129        }
130 
131        /*
132         * (non-Javadoc)
133         * 
134         * @see org.springframework.batch.repeat.RepeatContext#close()
135         */
136        public void close() {
137 
138                List<RuntimeException> errors = new ArrayList<RuntimeException>();
139 
140                Set<Map.Entry<String, Set<Runnable>>> copy;
141 
142                synchronized (callbacks) {
143                        copy = new HashSet<Map.Entry<String, Set<Runnable>>>(callbacks.entrySet());
144                }
145 
146                for (Map.Entry<String, Set<Runnable>> entry : copy) {
147 
148                        for (Runnable callback : entry.getValue()) {
149                                /*
150                                 * Potentially we could check here if there is an attribute with
151                                 * the given name - if it has been removed, maybe the callback
152                                 * is invalid. On the other hand it is less surprising for the
153                                 * callback register if it is always executed.
154                                 */
155                                if (callback != null) {
156                                        /*
157                                         * The documentation of the interface says that these
158                                         * callbacks must not throw exceptions, but we don't trust
159                                         * them necessarily...
160                                         */
161                                        try {
162                                                callback.run();
163                                        }
164                                        catch (RuntimeException t) {
165                                                errors.add(t);
166                                        }
167                                }
168                        }
169                }
170 
171                if (errors.isEmpty()) {
172                        return;
173                }
174 
175                throw (RuntimeException) errors.get(0);
176        }
177 
178}

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