1 24 25 package com.mckoi.database; 26 27 import java.util.HashMap ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.ListIterator ; 32 import java.math.BigDecimal ; 33 34 45 46 public final class StatementTree implements java.io.Serializable , Cloneable { 47 48 static final long serialVersionUID = -5907058730080713004L; 49 50 56 private String statement_class; 57 58 67 private HashMap map; 68 69 75 public StatementTree(String statement_class) { 76 if (!statement_class.startsWith("com.mckoi.database.interpret.")) { 77 throw new Error ("statement_class must be in the " + 78 "com.mckoi.database.interpret package."); 79 } 80 this.statement_class = statement_class; 81 map = new HashMap (); 82 } 83 84 87 public void putObject(String entry_name, Object ob) { 88 if (entry_name == null) { 89 throw new NullPointerException ("entry_name is null."); 90 } 91 if (ob == null || 93 ob instanceof Boolean || 94 ob instanceof String || 95 ob instanceof BigDecimal || 96 ob instanceof Variable || 97 ob instanceof Integer || 98 ob instanceof TObject || 99 ob instanceof TType || 100 ob instanceof Expression || 101 ob instanceof Expression[] || 102 ob instanceof List || 103 ob instanceof StatementTree || 104 ob instanceof StatementTreeObject) { 105 106 Object v = map.put(entry_name, ob); 107 if (v != null) { 108 throw new Error ("Entry '" + entry_name + 109 "' is already present in this tree."); 110 } 111 112 } 113 else { 114 throw new Error ("ob of entry '" + entry_name + 115 "' is not derived from a recognised class"); 116 } 117 118 } 119 120 123 public void putBoolean(String entry_name, boolean b) { 124 putObject(entry_name, b ? Boolean.TRUE : Boolean.FALSE); 125 } 126 127 130 public void putInt(String entry_name, int v) { 131 putObject(entry_name, new Integer (v)); 132 } 133 134 135 138 public Object getObject(String entry_name) { 139 return map.get(entry_name); 140 } 141 142 145 public boolean getBoolean(String entry_name) { 146 Object ob = map.get(entry_name); 147 return ((Boolean ) ob).booleanValue(); 148 } 149 150 153 public int getInt(String entry_name) { 154 Object ob = map.get(entry_name); 155 return ((Integer ) ob).intValue(); 156 } 157 158 159 160 161 164 public String getClassName() { 165 return statement_class; 166 } 167 168 174 public void prepareAllExpressions(ExpressionPreparer preparer) 175 throws DatabaseException { 176 Iterator i = map.values().iterator(); 177 178 while (i.hasNext()) { 179 Object v = i.next(); 180 if (v != null) { 181 prepareExpressionsInObject(v, preparer); 182 } 183 } 184 185 } 186 187 private void prepareExpressionsInObject(Object v, 188 ExpressionPreparer preparer) throws DatabaseException { 189 if (v instanceof Expression) { 191 ((Expression) v).prepare(preparer); 192 } 193 else if (v instanceof Expression[]) { 195 Expression[] exp_list = (Expression[]) v; 196 for (int n = 0; n < exp_list.length; ++n) { 197 exp_list[n].prepare(preparer); 198 } 199 } 200 else if (v instanceof StatementTreeObject) { 202 StatementTreeObject stob = (StatementTreeObject) v; 203 stob.prepareExpressions(preparer); 204 } 205 else if (v instanceof StatementTree) { 207 StatementTree st = (StatementTree) v; 208 st.prepareAllExpressions(preparer); 209 } 210 else if (v instanceof List ) { 212 List list = (List ) v; 213 for (int n = 0; n < list.size(); ++n) { 214 Object ob = list.get(n); 215 prepareExpressionsInObject(ob, preparer); 216 } 217 } 218 } 219 220 223 public static Object cloneSingleObject(Object entry) 224 throws CloneNotSupportedException { 225 226 if (entry == null || 228 entry instanceof TObject || 229 entry instanceof TType || 230 entry instanceof Boolean || 231 entry instanceof String || 232 entry instanceof BigDecimal || 233 entry instanceof Integer ) { 234 } 236 else if (entry instanceof Expression) { 237 entry = ((Expression) entry).clone(); 238 } 239 else if (entry instanceof Expression[]) { 240 Expression[] exps = (Expression[]) ((Expression[]) entry).clone(); 241 for (int n = 0; n < exps.length; ++n) { 243 exps[n] = (Expression) exps[n].clone(); 244 } 245 entry = exps; 246 } 247 else if (entry instanceof Variable) { 248 entry = ((Variable) entry).clone(); 249 } 250 else if (entry instanceof StatementTreeObject) { 251 entry = ((StatementTreeObject) entry).clone(); 252 } 253 else if (entry instanceof StatementTree) { 254 entry = ((StatementTree) entry).clone(); 255 } 256 else if (entry instanceof List ) { 257 List list = (List ) entry; 260 ArrayList cloned_list = new ArrayList (list.size()); 261 Iterator i = list.iterator(); 262 while (i.hasNext()) { 263 cloned_list.add(cloneSingleObject(i.next())); 264 } 265 entry = cloned_list; 266 } 267 else { 268 throw new CloneNotSupportedException (entry.getClass().toString()); 269 } 270 271 return entry; 272 } 273 274 278 public Object clone() throws CloneNotSupportedException { 279 StatementTree v = (StatementTree) super.clone(); 281 HashMap cloned_map = new HashMap (); 283 v.map = cloned_map; 284 285 Iterator i = map.keySet().iterator(); 287 while (i.hasNext()) { 288 Object key = i.next(); 289 Object entry = map.get(key); 290 291 entry = cloneSingleObject(entry); 292 293 cloned_map.put(key, entry); 294 } 295 296 return v; 297 } 298 299 302 public String toString() { 303 return "[ " + getClassName() + " [ " + map + " ] ]"; 304 } 305 306 } 307 | Popular Tags |