View Javadoc

1   // VarToAttribMap.java, created Oct 31, 2004 1:43:04 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  
13  import jwutil.collections.UnmodifiableIterator;
14  import net.sf.bddbddb.Attribute;
15  import net.sf.bddbddb.InferenceRule;
16  import net.sf.bddbddb.Variable;
17  
18  /***
19   * VarToAttribMap
20   * 
21   * @author jwhaley
22   * @version $Id: VarToAttribMap.java 446 2005-02-24 01:03:29Z cs343 $
23   */
24  public class VarToAttribMap extends AbstractMap {
25      
26      public static Collection convert(Collection vars, InferenceRule ir) {
27          Collection result = new LinkedList();
28          for (Iterator i = vars.iterator(); i.hasNext(); ) {
29              Variable v = (Variable) i.next();
30              Attribute a = (Attribute) ir.getAttribute(v);
31              if (a != null) result.add(a);
32          }
33          return result;
34      }
35      
36      InferenceRule ir;
37      
38      /***
39       * 
40       */
41      public VarToAttribMap(InferenceRule ir) {
42          this.ir = ir;
43      }
44  
45      public Object get(Object key) {
46          Variable v = (Variable) key;
47          Attribute a = ir.getAttribute(v);
48          return a;
49      }
50      
51      /* (non-Javadoc)
52       * @see java.util.Map#entrySet()
53       */
54      public Set entrySet() {
55          return new MapEntrySet(ir);
56      }
57      
58      static class MapEntrySet extends AbstractSet {
59  
60          InferenceRule ir;
61          
62          MapEntrySet(InferenceRule ir) {
63              this.ir = ir;
64          }
65          
66          public Iterator iterator() {
67              return new UnmodifiableIterator() {
68                  Iterator i = ir.getNecessaryVariables().iterator();
69                  Iterator j = ir.getUnnecessaryVariables().iterator();
70                  
71                  public boolean hasNext() {
72                      return i.hasNext() || j.hasNext();
73                  }
74  
75                  public Object next() {
76                      Variable v;
77                      if (i.hasNext()) v = (Variable) i.next();
78                      else v = (Variable) j.next();
79                      return new MapEntry(v, ir);
80                  }
81                  
82              };
83          }
84  
85          public int size() {
86              return ir.numberOfVariables();
87          }
88          
89      };
90  
91      
92      static class MapEntry implements AbstractMap.Entry {
93  
94          Variable v;
95          InferenceRule ir;
96          
97          MapEntry(Variable v, InferenceRule ir) {
98              this.v = v;
99              this.ir = ir;
100         }
101         
102         /* (non-Javadoc)
103          * @see java.util.Map.Entry#getKey()
104          */
105         public Object getKey() {
106             return v;
107         }
108 
109         /* (non-Javadoc)
110          * @see java.util.Map.Entry#getValue()
111          */
112         public Object getValue() {
113             return ir.getAttribute(v);
114         }
115 
116         /* (non-Javadoc)
117          * @see java.util.Map.Entry#setValue(java.lang.Object)
118          */
119         public Object setValue(Object arg0) {
120             throw new UnsupportedOperationException();
121         }
122         
123     }
124 }