View Javadoc

1   // AttribToDomainMap.java, created Oct 31, 2004 1:57:38 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.AbstractMap;
7   import java.util.AbstractSet;
8   import java.util.Collection;
9   import java.util.Iterator;
10  import java.util.LinkedList;
11  import java.util.Set;
12  import jwutil.collections.UnmodifiableIterator;
13  import net.sf.bddbddb.Attribute;
14  import net.sf.bddbddb.Relation;
15  import net.sf.bddbddb.Solver;
16  
17  /***
18   * AttribToDomainMap
19   * 
20   * @author jwhaley
21   * @version $Id: AttribToDomainMap.java 502 2005-04-13 00:26:23Z joewhaley $
22   */
23  public class AttribToDomainMap extends AbstractMap {
24      
25      public static Collection convert(Collection attribs) {
26          Collection result = new LinkedList();
27          for (Iterator i = attribs.iterator(); i.hasNext(); ) {
28              Attribute a = (Attribute) i.next();
29              result.add(a.getDomain());
30          }
31          return result;
32      }
33      
34      Solver s;
35      transient Collection allAttribs;
36      
37      /***
38       * 
39       */
40      public AttribToDomainMap(Solver s) {
41          this.s = s;
42      }
43  
44      public Object get(Object o) {
45          Attribute a = (Attribute) o;
46          return a.getDomain();
47      }
48      
49      private void buildAttribs() {
50          allAttribs = new LinkedList();
51          for (Iterator i = s.getRelations().iterator(); i.hasNext(); ) {
52              Relation r = (Relation) i.next();
53              allAttribs.addAll(r.getAttributes());
54          }
55      }
56      
57      /* (non-Javadoc)
58       * @see java.util.Map#entrySet()
59       */
60      public Set entrySet() {
61          if (allAttribs == null) buildAttribs();
62          return new AbstractSet() {
63              public Iterator iterator() {
64                  final Iterator i = allAttribs.iterator();
65                  return new UnmodifiableIterator() {
66                      public boolean hasNext() {
67                          return i.hasNext();
68                      }
69                      public Object next() {
70                          Attribute a = (Attribute) i.next();
71                          return a.getDomain();
72                      }
73                  };
74              }
75              public int size() {
76                  return allAttribs.size();
77              }
78          };
79      }
80  }