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.policy;
18  
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.List;
22  
23  import org.springframework.batch.repeat.CompletionPolicy;
24  import org.springframework.batch.repeat.RepeatContext;
25  import org.springframework.batch.repeat.RepeatStatus;
26  import 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   */
35  public 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 }