EMMA Coverage Report (generated Thu May 22 12:08:10 CDT 2014)
[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    @Override
55        public boolean isComplete(RepeatContext context, RepeatStatus result) {
56                RepeatContext[] contexts = ((CompositeBatchContext) context).contexts;
57                CompletionPolicy[] policies = ((CompositeBatchContext) context).policies;
58                for (int i = 0; i < policies.length; i++) {
59                        if (policies[i].isComplete(contexts[i], result)) {
60                                return true;
61                        }
62                }
63                return false;
64        }
65 
66        /**
67         * This policy is complete if any of the composed policies is complete.
68         * 
69         * @see org.springframework.batch.repeat.CompletionPolicy#isComplete(org.springframework.batch.repeat.RepeatContext)
70         */
71    @Override
72        public boolean isComplete(RepeatContext context) {
73                RepeatContext[] contexts = ((CompositeBatchContext) context).contexts;
74                CompletionPolicy[] policies = ((CompositeBatchContext) context).policies;
75                for (int i = 0; i < policies.length; i++) {
76                        if (policies[i].isComplete(contexts[i])) {
77                                return true;
78                        }
79                }
80                return false;
81        }
82 
83        /**
84         * Create a new composite context from all the available policies.
85         * 
86         * @see org.springframework.batch.repeat.CompletionPolicy#start(RepeatContext)
87         */
88    @Override
89        public RepeatContext start(RepeatContext context) {
90                List<RepeatContext> list = new ArrayList<RepeatContext>();
91                for (int i = 0; i < policies.length; i++) {
92                        list.add(policies[i].start(context));
93                }
94                return new CompositeBatchContext(context, list);
95 
96        }
97 
98        /**
99         * Update all the composed contexts, and also increment the parent context.
100         * 
101         * @see org.springframework.batch.repeat.CompletionPolicy#update(org.springframework.batch.repeat.RepeatContext)
102         */
103    @Override
104        public void update(RepeatContext context) {
105                RepeatContext[] contexts = ((CompositeBatchContext) context).contexts;
106                CompletionPolicy[] policies = ((CompositeBatchContext) context).policies;
107                for (int i = 0; i < policies.length; i++) {
108                        policies[i].update(contexts[i]);
109                }
110                ((RepeatContextSupport) context).increment();
111        }
112 
113        /**
114         * Composite context that knows about the policies and contexts is was
115         * created with.
116         * 
117         * @author Dave Syer
118         * 
119         */
120        protected class CompositeBatchContext extends RepeatContextSupport {
121 
122                private RepeatContext[] contexts;
123 
124                // Save a reference to the policies when we were created - gives some
125                // protection against reference changes (e.g. if the number of policies
126                // change).
127                private CompletionPolicy[] policies;
128 
129                public CompositeBatchContext(RepeatContext context, List<RepeatContext> contexts) {
130                        super(context);
131                        this.contexts = contexts.toArray(new RepeatContext[contexts.size()]);
132                        this.policies = CompositeCompletionPolicy.this.policies;
133                }
134 
135        }
136 
137}

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