| 1 | package org.springframework.batch.item.transform; |
| 2 | |
| 3 | import java.util.Iterator; |
| 4 | import java.util.List; |
| 5 | |
| 6 | import org.springframework.beans.factory.InitializingBean; |
| 7 | import org.springframework.util.Assert; |
| 8 | |
| 9 | /** |
| 10 | * Composite {@link ItemTransformer} that passes the item through a sequence |
| 11 | * of injected <code>ItemTransformer</code>s (return value of previous transformation |
| 12 | * is the entry value of the next). |
| 13 | * |
| 14 | * @author Robert Kasanicky |
| 15 | */ |
| 16 | public class CompositeItemTransformer implements ItemTransformer, InitializingBean { |
| 17 | |
| 18 | private List itemTransformers; |
| 19 | |
| 20 | public Object transform(Object item) throws Exception { |
| 21 | Object result = item; |
| 22 | for (Iterator iterator = itemTransformers.listIterator(); iterator.hasNext();) { |
| 23 | result = ((ItemTransformer)iterator.next()).transform(result); |
| 24 | } |
| 25 | return result; |
| 26 | } |
| 27 | |
| 28 | public void afterPropertiesSet() throws Exception { |
| 29 | Assert.notEmpty(itemTransformers); |
| 30 | for (Iterator iterator = itemTransformers.iterator(); iterator.hasNext();) { |
| 31 | Assert.isInstanceOf(ItemTransformer.class, iterator.next()); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @param itemTransformers will be chained to produce a composite transformation. |
| 37 | */ |
| 38 | public void setItemTransformers(List itemTransformers) { |
| 39 | this.itemTransformers = itemTransformers; |
| 40 | } |
| 41 | |
| 42 | } |