01: package org.incava.util;
02:
03: import java.awt.Point;
04: import java.io.*;
05: import java.util.*;
06: import junit.framework.TestCase;
07:
08: public class TestCollect extends TestCase {
09: public TestCollect(String name) {
10: super (name);
11: }
12:
13: public void test() {
14: List list = new ArrayList();
15: list.add("one");
16: list.add("two");
17: list.add("three");
18: list.add("four");
19:
20: List collected = new Collect(list) {
21: public boolean where(Object obj) {
22: return ((String) obj).indexOf('o') != -1;
23: }
24: };
25: assertEquals(3, collected.size());
26: assertEquals("one", (String) collected.get(0));
27: assertEquals("two", (String) collected.get(1));
28: assertEquals("four", (String) collected.get(2));
29: }
30:
31: public void testBlock() {
32: List list = new ArrayList();
33: list.add(new Point(4, 5));
34: list.add(new Point(2, 15));
35: list.add(new Point(9, 11));
36: list.add(new Point(24, 7));
37: list.add(new Point(3, 12));
38:
39: List collected = new Collect(list) {
40: public boolean where(Object obj) {
41: return ((Point) obj).x % 2 == 0;
42: }
43:
44: public Object block(Object obj) {
45: Point pt = (Point) obj;
46: return new Integer(pt.y);
47: }
48: };
49: assertEquals(3, collected.size());
50: assertEquals(new Integer(5), (Integer) collected.get(0));
51: assertEquals(new Integer(15), (Integer) collected.get(1));
52: assertEquals(new Integer(7), (Integer) collected.get(2));
53: }
54:
55: }
|