View Javadoc

1   //
2   // Copyright (c) 2004, International Decision Systems
3   // all rights reserved
4   /*
5    * Copyright (c) 2004 International Decision Systems, Inc.  All Rights Reserved.
6    *
7    * By using this Software, You acknowledge that the Software is a valuable asset
8    * and trade secret of either International Decision Systems, Inc. ("IDSI") or a
9    * third party supplier of IDSI and constitutes confidential and proprietary
10   * information.
11   *
12   * NEITHER IDSI NOR ANY AGENT OR PERSON ACTING FOR OR WITH IDSI HAS MADE OR DOES
13   * MAKE ANY STATEMENTS, AFFIRMATIONS, REPRESENTATIONS OR WARRANTIES WHATSOEVER
14   * TO YOU, WHETHER EXPRESS OR IMPLIED, AS TO THE SOFTWARE, THE QUALITY OR
15   * CONDITION OF THE SOFTWARE, OR THE OPERATING CHARACTERISTICS OR RELIABILITY OF
16   * THE SOFTWARE, OR ITS SUITABILITY FOR ANY GENERAL OR PARTICULAR PURPOSE, OR AS
17   * TO ANY OTHER MATTER WHATSOEVER; ANY AND ALL OTHER WARRANTIES INCLUDING
18   * WITHOUT LIMITATION ANY WARRANTIES IMPLIED BY LAW, SUCH AS THE IMPLIED
19   * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND TITLE,
20   * USE AND NON-INFRINGEMENT; ARE HEREBY EXPRESSLY DISCLAIMED AND EXCLUDED.
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  }