View Javadoc

1   // FilterTranslator.java, created Oct 31, 2004 3:04:05 AM by joewhaley
2   // Copyright (C) 2004 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
4   package net.sf.bddbddb.order;
5   
6   import java.util.Collection;
7   import java.util.Iterator;
8   import java.util.LinkedList;
9   
10  /***
11   * FilterTranslator
12   * 
13   * @author jwhaley
14   * @version $Id: FilterTranslator.java 446 2005-02-24 01:03:29Z cs343 $
15   */
16  public class FilterTranslator implements OrderTranslator {
17      
18      Collection c;
19      
20      /***
21       * 
22       */
23      public FilterTranslator(Collection c) {
24          this.c = c;
25      }
26  
27      /* (non-Javadoc)
28       * @see net.sf.bddbddb.order.OrderTranslator#translate(net.sf.bddbddb.order.Order)
29       */
30      public Order translate(Order o) {
31          LinkedList result = new LinkedList();
32          Collection cCopy = new LinkedList(c);
33          for (Iterator i = o.iterator(); i.hasNext(); ) {
34              Object a = i.next();
35              if (a instanceof Collection) {
36                  Collection result2 = new LinkedList();
37                  for (Iterator j = ((Collection) a).iterator(); j.hasNext(); ) {
38                      Object a2 = j.next();
39                      if (cCopy.contains(a2)){
40                          result2.add(a2);
41                          cCopy.remove(a2);
42                      }
43                  }
44                  if (result2.size() > 1) {
45                      result.add(result2);
46                  } else if (!result2.isEmpty()) {
47                      result.add(result2.iterator().next());
48                  }
49              } else {
50                  if (cCopy.contains(a)){
51                      result.add(a);
52                      cCopy.remove(a);
53                  }
54              }
55          }
56          return new Order(result);
57      }
58      public String toString(){
59         return "FilterTranslator: " + c;
60      }
61  }