BATCH-1543: fix OrderedComposite
This commit is contained in:
@@ -16,11 +16,10 @@
|
||||
package org.springframework.batch.core.listener;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
@@ -35,8 +34,10 @@ class OrderedComposite<S> {
|
||||
|
||||
private List<S> unordered = new ArrayList<S>();
|
||||
|
||||
private List<S> ordered = new ArrayList<S>();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Collection<S> ordered = new TreeSet<S>(new AnnotationAwareOrderComparator());
|
||||
private Comparator<? super S> comparator = new AnnotationAwareOrderComparator();
|
||||
|
||||
private List<S> list = new ArrayList<S>();
|
||||
|
||||
@@ -72,6 +73,7 @@ class OrderedComposite<S> {
|
||||
else if (!unordered.contains(item)) {
|
||||
unordered.add(item);
|
||||
}
|
||||
Collections.sort(ordered, comparator);
|
||||
list.clear();
|
||||
list.addAll(ordered);
|
||||
list.addAll(unordered);
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.batch.core.listener;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
@@ -40,6 +41,14 @@ public class OrderedCompositeTests {
|
||||
assertEquals("2", iterator.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetSameObject() {
|
||||
list.setItems(Arrays.asList(new Object[] { "1", "1" }));
|
||||
Iterator<Object> iterator = list.iterator();
|
||||
assertEquals("1", iterator.next());
|
||||
assertFalse(iterator.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd() {
|
||||
list.setItems(Arrays.asList((Object) "1"));
|
||||
@@ -81,6 +90,25 @@ public class OrderedCompositeTests {
|
||||
assertEquals("1", iterator.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddDuplicateOrdered() {
|
||||
list.setItems(Arrays.asList((Object) "1"));
|
||||
list.add(new Ordered() {
|
||||
public int getOrder() {
|
||||
return 1;
|
||||
}
|
||||
});
|
||||
list.add(new Ordered() {
|
||||
public int getOrder() {
|
||||
return 1;
|
||||
}
|
||||
});
|
||||
Iterator<Object> iterator = list.iterator();
|
||||
assertEquals(1, ((Ordered) iterator.next()).getOrder());
|
||||
assertEquals(1, ((Ordered) iterator.next()).getOrder());
|
||||
assertEquals("1", iterator.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddAnnotationOrdered() {
|
||||
list.add(new Ordered() {
|
||||
|
||||
Reference in New Issue
Block a user