| 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 | package org.springframework.batch.core.listener; |
| 17 | |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.Collections; |
| 20 | import java.util.Comparator; |
| 21 | import java.util.Iterator; |
| 22 | import java.util.List; |
| 23 | |
| 24 | import org.springframework.core.Ordered; |
| 25 | import org.springframework.core.annotation.AnnotationAwareOrderComparator; |
| 26 | import org.springframework.core.annotation.AnnotationUtils; |
| 27 | import org.springframework.core.annotation.Order; |
| 28 | |
| 29 | /** |
| 30 | * @author Dave Syer |
| 31 | * |
| 32 | */ |
| 33 | class OrderedComposite<S> { |
| 34 | |
| 35 | private List<S> unordered = new ArrayList<S>(); |
| 36 | |
| 37 | private List<S> ordered = new ArrayList<S>(); |
| 38 | |
| 39 | @SuppressWarnings("unchecked") |
| 40 | private Comparator<? super S> comparator = new AnnotationAwareOrderComparator(); |
| 41 | |
| 42 | private List<S> list = new ArrayList<S>(); |
| 43 | |
| 44 | /** |
| 45 | * Public setter for the listeners. |
| 46 | * |
| 47 | * @param items |
| 48 | */ |
| 49 | public void setItems(List<? extends S> items) { |
| 50 | unordered.clear(); |
| 51 | ordered.clear(); |
| 52 | for (S s : items) { |
| 53 | add(s); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Register additional item. |
| 59 | * |
| 60 | * @param item |
| 61 | */ |
| 62 | public void add(S item) { |
| 63 | if (item instanceof Ordered) { |
| 64 | if (!ordered.contains(item)) { |
| 65 | ordered.add(item); |
| 66 | } |
| 67 | } |
| 68 | else if (AnnotationUtils.isAnnotationDeclaredLocally(Order.class, item.getClass())) { |
| 69 | if (!ordered.contains(item)) { |
| 70 | ordered.add(item); |
| 71 | } |
| 72 | } |
| 73 | else if (!unordered.contains(item)) { |
| 74 | unordered.add(item); |
| 75 | } |
| 76 | Collections.sort(ordered, comparator); |
| 77 | list.clear(); |
| 78 | list.addAll(ordered); |
| 79 | list.addAll(unordered); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Public getter for the list of items. The {@link Ordered} items come |
| 84 | * first, followed by any unordered ones. |
| 85 | * @return an iterator over the list of items |
| 86 | */ |
| 87 | public Iterator<S> iterator() { |
| 88 | return new ArrayList<S>(list).iterator(); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Public getter for the list of items in reverse. The {@link Ordered} items |
| 93 | * come last, after any unordered ones. |
| 94 | * @return an iterator over the list of items |
| 95 | */ |
| 96 | public Iterator<S> reverse() { |
| 97 | ArrayList<S> result = new ArrayList<S>(list); |
| 98 | Collections.reverse(result); |
| 99 | return result.iterator(); |
| 100 | } |
| 101 | |
| 102 | } |