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

COVERAGE SUMMARY FOR SOURCE FILE [CompositeCompletionPolicy.java]

nameclass, %method, %block, %line, %
CompositeCompletionPolicy.java100% (2/2)100% (9/9)100% (155/155)100% (32/32)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CompositeCompletionPolicy100% (1/1)100% (6/6)100% (130/130)100% (27/27)
CompositeCompletionPolicy (): void 100% (1/1)100% (7/7)100% (3/3)
isComplete (RepeatContext): boolean 100% (1/1)100% (28/28)100% (6/6)
isComplete (RepeatContext, RepeatStatus): boolean 100% (1/1)100% (29/29)100% (6/6)
setPolicies (CompletionPolicy []): void 100% (1/1)100% (10/10)100% (2/2)
start (RepeatContext): RepeatContext 100% (1/1)100% (29/29)100% (4/4)
update (RepeatContext): void 100% (1/1)100% (27/27)100% (6/6)
     
class CompositeCompletionPolicy$CompositeBatchContext100% (1/1)100% (3/3)100% (25/25)100% (6/6)
CompositeCompletionPolicy$CompositeBatchContext (CompositeCompletionPolicy, R... 100% (1/1)100% (19/19)100% (5/5)
access$000 (CompositeCompletionPolicy$CompositeBatchContext): RepeatContext [] 100% (1/1)100% (3/3)100% (1/1)
access$100 (CompositeCompletionPolicy$CompositeBatchContext): CompletionPolic... 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 java.util.ArrayList;
20import java.util.Arrays;
21import java.util.List;
22 
23import org.springframework.batch.repeat.CompletionPolicy;
24import org.springframework.batch.repeat.RepeatContext;
25import org.springframework.batch.repeat.RepeatStatus;
26import org.springframework.batch.repeat.context.RepeatContextSupport;
27 
28/**
29 * Composite policy that loops through a list of delegate policies and answers
30 * calls by a concensus.
31 * 
32 * @author Dave Syer
33 * 
34 */
35public class CompositeCompletionPolicy implements CompletionPolicy {
36 
37        CompletionPolicy[] policies = new CompletionPolicy[0];
38 
39        /**
40         * Setter for the policies.
41         * 
42         * @param policies
43         */
44        public void setPolicies(CompletionPolicy[] policies) {
45                this.policies = Arrays.asList(policies).toArray(new CompletionPolicy[policies.length]);
46        }
47 
48        /**
49         * This policy is complete if any of the composed policies is complete.
50         * 
51         * @see org.springframework.batch.repeat.CompletionPolicy#isComplete(org.springframework.batch.repeat.RepeatContext,
52         * RepeatStatus)
53         */
54        public boolean isComplete(RepeatContext context, RepeatStatus result) {
55                RepeatContext[] contexts = ((CompositeBatchContext) context).contexts;
56                CompletionPolicy[] policies = ((CompositeBatchContext) context).policies;
57                for (int i = 0; i < policies.length; i++) {
58                        if (policies[i].isComplete(contexts[i], result)) {
59                                return true;
60                        }
61                }
62                return false;
63        }
64 
65        /**
66         * This policy is complete if any of the composed policies is complete.
67         * 
68         * @see org.springframework.batch.repeat.CompletionPolicy#isComplete(org.springframework.batch.repeat.RepeatContext)
69         */
70        public boolean isComplete(RepeatContext context) {
71                RepeatContext[] contexts = ((CompositeBatchContext) context).contexts;
72                CompletionPolicy[] policies = ((CompositeBatchContext) context).policies;
73                for (int i = 0; i < policies.length; i++) {
74                        if (policies[i].isComplete(contexts[i])) {
75                                return true;
76                        }
77                }
78                return false;
79        }
80 
81        /**
82         * Create a new composite context from all the available policies.
83         * 
84         * @see org.springframework.batch.repeat.CompletionPolicy#start(RepeatContext)
85         */
86        public RepeatContext start(RepeatContext context) {
87                List<RepeatContext> list = new ArrayList<RepeatContext>();
88                for (int i = 0; i < policies.length; i++) {
89                        list.add(policies[i].start(context));
90                }
91                return new CompositeBatchContext(context, list);
92 
93        }
94 
95        /**
96         * Update all the composed contexts, and also increment the parent context.
97         * 
98         * @see org.springframework.batch.repeat.CompletionPolicy#update(org.springframework.batch.repeat.RepeatContext)
99         */
100        public void update(RepeatContext context) {
101                RepeatContext[] contexts = ((CompositeBatchContext) context).contexts;
102                CompletionPolicy[] policies = ((CompositeBatchContext) context).policies;
103                for (int i = 0; i < policies.length; i++) {
104                        policies[i].update(contexts[i]);
105                }
106                ((RepeatContextSupport) context).increment();
107        }
108 
109        /**
110         * Composite context that knows about the policies and contexts is was
111         * created with.
112         * 
113         * @author Dave Syer
114         * 
115         */
116        protected class CompositeBatchContext extends RepeatContextSupport {
117 
118                private RepeatContext[] contexts;
119 
120                // Save a reference to the policies when we were created - gives some
121                // protection against reference changes (e.g. if the number of policies
122                // change).
123                private CompletionPolicy[] policies;
124 
125                public CompositeBatchContext(RepeatContext context, List<RepeatContext> contexts) {
126                        super(context);
127                        this.contexts = contexts.toArray(new RepeatContext[contexts.size()]);
128                        this.policies = CompositeCompletionPolicy.this.policies;
129                }
130 
131        }
132 
133}

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