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.item.support;
18
19 import java.util.List;
20
21 import org.springframework.batch.item.ItemProcessor;
22 import org.springframework.beans.factory.InitializingBean;
23 import org.springframework.util.Assert;
24
25 /**
26 * Composite {@link ItemProcessor} that passes the item through a sequence of
27 * injected <code>ItemTransformer</code>s (return value of previous
28 * transformation is the entry value of the next).<br/>
29 * <br/>
30 *
31 * Note the user is responsible for injecting a chain of {@link ItemProcessor} s
32 * that conforms to declared input and output types.
33 *
34 * @author Robert Kasanicky
35 */
36 public class CompositeItemProcessor<I, O> implements ItemProcessor<I, O>, InitializingBean {
37
38 private List<ItemProcessor<Object, Object>> delegates;
39
40 @Override
41 @SuppressWarnings("unchecked")
42 public O process(I item) throws Exception {
43 Object result = item;
44
45 for (ItemProcessor<Object, Object> delegate : delegates) {
46 if (result == null) {
47 return null;
48 }
49 result = delegate.process(result);
50 }
51 return (O) result;
52 }
53
54 @Override
55 public void afterPropertiesSet() throws Exception {
56 Assert.notNull(delegates, "The 'delgates' may not be null");
57 Assert.notEmpty(delegates, "The 'delgates' may not be empty");
58 }
59
60 public void setDelegates(List<ItemProcessor<Object, Object>> delegates) {
61 this.delegates = delegates;
62 }
63
64 }