View Javadoc

1   /*$Id: Vars.java,v 1.7 2004/12/09 16:39:12 clq2 Exp $
2    * Created on 28-Jul-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
9    *
10  **/
11  package org.astrogrid.jes.jobscheduler.impl.groovy;
12  
13  import org.apache.commons.logging.Log;
14  import org.apache.commons.logging.LogFactory;
15  
16  import groovy.lang.Binding;
17  import groovy.lang.MissingPropertyException;
18  
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  
25  /*** represents set of variable bindings in scope.
26   * allows additional scopes to be added and removed.
27   * scopes introduce new bindings.
28   * asignements to existing bindings in parent scopes update, rather than shadow.
29   * @author Noel Winstanley nw@jb.man.ac.uk 28-Jul-2004
30   *
31   */
32  public class Vars{
33      /***
34       * Commons Logger for this class
35       */
36      private static final Log logger = LogFactory.getLog(Vars.class);
37  
38      /*** Construct a new Bindings
39       * 
40       */
41      public Vars() {
42          super();
43          l = new ArrayList();
44          l.add(new HashMap());
45      }
46  
47      protected List l;
48  
49      
50      
51      public Object get(String name) {
52          Map e = findMapWithKey(name);
53          if (e == null) {
54              return null;
55          }
56          return e.get(name);
57      }
58  
59      public void set(String name,Object value) {
60          Map e = findMapWithKey(name);
61          if (e == null) {
62              e = getInnermost();
63          }
64          e.put(name,value);
65      }
66      
67      private Map getInnermost() {
68          return (Map)l.get(l.size()  -1);
69      }
70      private Map findMapWithKey(String name) {
71          for (int i = l.size() - 1; i >= 0; i--) {
72              Map m = (Map)l.get(i);
73              if (m.containsKey(name)) {
74                  return m;
75              }
76          }
77          return null;
78      }
79      
80      
81      public void unset(String name) {
82          Map e = findMapWithKey(name);
83          if (e !=null) {
84              e.remove(name);
85          }
86      }
87      /*** Branch these vars
88       * this is a very shallow copy. existing scopes are shared between the original and copy.
89       * however, new scopes in the copy do not appear in the original.
90       */
91      public Vars branchVars() {
92          Vars copy = new Vars();
93          copy.l.clear();
94          copy.l.addAll(this.l);
95          return copy;
96      }
97      
98  public void newScope() {
99      l.add(new HashMap());
100 }
101 
102 public void removeScope()     {
103     l.remove(l.size() - 1);
104 }
105     
106 
107     /*** Add the variables defined in this collection to a binding, plus the 'vars' object itself.
108      * @param bodyBinding
109      */
110     public void addToBinding(Binding bodyBinding) {
111         /*** NWW - 9/12/04 don't like this - not necessary - problematic
112         bodyBinding.setVariable("vars",this);
113         */
114         for (Iterator j = l.iterator(); j.hasNext(); ) {
115             Map e = (Map)j.next();
116             for (Iterator i = e.entrySet().iterator(); i.hasNext(); ) {
117                 Map.Entry var = (Map.Entry)i.next();
118                 bodyBinding.setVariable(var.getKey().toString(),var.getValue());
119             }
120         }
121     }
122 
123     /*** Find the variables defined in this collection in the binding, copy back across again.
124      *<p>
125      *any new variables defined in the script must have been done directly - through accessing this object already. so just need to look for the values of existing vars.
126      * @param bodyBinding
127      */
128     
129     public void readFromBinding(Binding bodyBinding) {
130         for (int j = 0; j < l.size(); j++){
131             Map e = (Map)l.get(j);           
132             for (Iterator i = e.entrySet().iterator(); i.hasNext(); ) {
133                 Map.Entry var = (Map.Entry)i.next();
134                 try {
135                     Object newValue = bodyBinding.getVariable(var.getKey().toString());
136                     var.setValue(newValue);
137                 } catch (MissingPropertyException missing) {
138                     /*** don't think this is possible */
139                     logger.warn(missing);
140                 }
141             }
142     }
143     
144     /* beta-6 need a different impleemntation for beta-7 - as empty vars throws expcetions
145     public void readFromBinding(Binding bodyBinding) {
146         for (int j = 0; j < l.size(); j++){
147             Map e = (Map)l.get(j);           
148             for (Iterator i = e.entrySet().iterator(); i.hasNext(); ) {
149                 Map.Entry var = (Map.Entry)i.next();
150                 Object newValue = bodyBinding.getVariable(var.getKey().toString());
151                 if (newValue == null) {
152                 // dunno if its been explicitly set to null, or whether its a new binding added by the script. arse.
153                 // safest thing is to ignore this.
154                 //var.setValue(null);
155                 } else {
156                     var.setValue(newValue);
157                 }
158             }
159         }*/
160     }
161     
162 
163     /***
164      * Override hashCode.
165      *
166      * @return the Objects hashcode.
167      */
168     public int hashCode() {
169         int hashCode = 1;
170         hashCode = 31 * hashCode + (l == null ? 0 : l.hashCode());
171         return hashCode;
172     }
173     public String toString() {
174         StringBuffer buffer = new StringBuffer();
175         buffer.append("[Vars:");
176         buffer.append(" l: ");
177         buffer.append(l);
178         buffer.append("]");
179         return buffer.toString();
180     }
181     /***
182      * Returns <code>true</code> if this <code>Vars</code> is the same as the o argument.
183      *
184      * @return <code>true</code> if this <code>Vars</code> is the same as the o argument.
185      */
186     public boolean equals(Object o) {
187         if (this == o) {
188             return true;
189         }
190         if (o == null) {
191             return false;
192         }
193         if (o.getClass() != getClass()) {
194             return false;
195         }
196         Vars castedObj = (Vars) o;
197         return ((this.l == null ? castedObj.l == null : this.l
198             .equals(castedObj.l)));
199     }
200 }
201 
202 
203 /* 
204 $Log: Vars.java,v $
205 Revision 1.7  2004/12/09 16:39:12  clq2
206 nww_jes_panic
207 
208 Revision 1.6.4.1  2004/12/09 13:22:59  nw
209 made variable persistance a little harder.
210 
211 Revision 1.6  2004/11/29 20:00:24  clq2
212 jes-nww-714
213 
214 Revision 1.5.12.1  2004/11/26 01:31:18  nw
215 updated dependency on groovy to 1.0-beta7.
216 updated code and tests to fit.
217 
218 Revision 1.5  2004/11/05 16:52:42  jdt
219 Merges from branch nww-itn07-scratchspace
220 
221 Revision 1.4.58.1  2004/11/05 16:03:57  nw
222 optimized: uses int rather than iterator
223 
224 Revision 1.4  2004/08/09 17:34:10  nw
225 implemented parfor.
226 removed references to rulestore
227 
228 Revision 1.3  2004/08/03 14:27:38  nw
229 added set/unset/scope features.
230 
231 Revision 1.2  2004/07/30 15:42:34  nw
232 merged in branch nww-itn06-bz#441 (groovy scripting)
233 
234 Revision 1.1.2.3  2004/07/30 15:10:04  nw
235 removed policy-based implementation,
236 adjusted tests, etc to use groovy implementation
237 
238 Revision 1.1.2.2  2004/07/30 14:00:10  nw
239 first working draft
240 
241 Revision 1.1.2.1  2004/07/28 16:24:23  nw
242 finished groovy beans.
243 moved useful tests from old python package.
244 removed python implemntation
245  
246 */