1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.sourceforge.addam.ddlrun.filters;
23
24 import java.util.*;
25
26 /**
27 * Returns the scripts that have not already executed; used to determine which
28 * scripts to run within a ScriptGroup.
29 *
30 * @author TIM3
31 * @since Mar 5, 2005
32 */
33 public class DifferenceFilter implements Filter {
34
35 public DifferenceFilter(Collection itemsToIgnore, boolean ignoreDuplicates) {
36 this.itemsToIgnore.addAll(itemsToIgnore);
37 this.ignoreDuplicates = ignoreDuplicates;
38 }
39
40 public Iterator getIterator(List collection) {
41 return new Iterator(collection);
42 }
43
44 public class Iterator implements java.util.Iterator {
45 private final java.util.Iterator source;
46 private Object next;
47
48 public Iterator(List collection) {
49 source = collection.iterator();
50 }
51
52 public boolean hasNext() {
53 if (next == null) {
54 while (source.hasNext()) {
55 next = source.next();
56 if (shouldInclude(next)) {
57 break;
58 } else {
59 next = null;
60 }
61 }
62 }
63 return (next != null);
64 }
65
66 public Object next() {
67 if (!hasNext()) throw new NoSuchElementException();
68 Object returnValue = next;
69 if (ignoreDuplicates) {
70 itemsToIgnore.add(next);
71 }
72 next = null;
73 return returnValue;
74 }
75
76 public void remove() {
77 throw new UnsupportedOperationException();
78 }
79 }
80
81 public boolean shouldInclude(Object item) {
82 String line = item.toString().trim();
83 return line.length() != 0 && !line.startsWith(COMMENT_PREFIX) && !itemsToIgnore.contains(line);
84 }
85
86 private final Set itemsToIgnore = new HashSet();
87 private final boolean ignoreDuplicates;
88 }