1 28 29 package com.idaremedia.antx; 30 31 import org.apache.tools.ant.BuildException; 32 import org.apache.tools.ant.Project; 33 34 import com.idaremedia.antx.apis.ProblemHandler; 35 36 import java.util.HashMap ; 37 import java.util.Iterator ; 38 import java.util.Map ; 39 import java.util.Stack ; 40 41 69 70 public final class FixtureOverlays implements FixtureCore, FixtureAdministrator, Cloneable 71 { 72 75 FixtureOverlays() 76 { } 77 78 79 80 85 public synchronized Object clone() 86 { 87 try { 88 FixtureOverlays copy = (FixtureOverlays)super.clone(); 89 copy.m_Env = (HashMap )m_Env.clone(); 90 Iterator itr = m_Env.entrySet().iterator(); 91 while (itr.hasNext()) { 92 Map.Entry mE= (Map.Entry )itr.next(); 93 copy.m_Env.put(mE.getKey(),((Stack )mE.getValue()).clone()); 94 } 95 copy.m_invalidCalls=0; 96 return copy; 97 } catch(CloneNotSupportedException clnx) { 98 throw new InternalError (AntX.uistrs().get(AntX.CLONE_BROKEN_MSGID)); 99 } 100 } 101 102 103 104 109 public synchronized Object nearest(Object category) 110 { 111 Stack all = callstack(category); 112 if (all==null || all.isEmpty()) { 113 return null; 114 } 115 return all.peek(); 116 } 117 118 119 120 130 public synchronized Object install(Object category, Object current) 131 { 132 Object previous = null; 133 Stack all = callstack(category); 134 135 if (all==null) { 136 all = create(category); 137 } else if (!all.isEmpty()) { 138 previous = all.peek(); 139 } 140 141 all.push(current); 142 return previous; 143 } 144 145 146 147 153 public synchronized boolean unwind(Object category) 154 { 155 boolean popped = true; 156 Stack all = callstack(category); 157 158 if (all!=null && !all.isEmpty()) { 159 all.pop(); 160 } else { 161 m_invalidCalls++; 162 popped = false; 163 } 164 return popped; 165 } 166 167 168 169 178 public synchronized boolean clear(Object category) 179 { 180 Stack all = callstack(category); 181 if (all!=null) { 182 all.clear(); 183 return true; 184 } 185 return false; 186 } 187 188 189 190 196 public synchronized boolean isInstalled(Object category, Object current) 197 { 198 if (current==null) { 199 throw new IllegalArgumentException ("isInstalled- NULL object"); 200 } 201 Stack all = callstack(category); 202 if (all!=null) { 203 return all.contains(current); 204 } 205 return false; 206 } 207 208 209 210 222 public static Object installIfNot(Object category, Object next, 223 ProblemHandler noInstallHandler, 224 String what) 225 { 226 FixtureOverlays overlays = FixtureOverlays.getContextInstance(); 227 synchronized(overlays) { 228 if (overlays.isInstalled(category,next)) { 229 String error = AntX.uistrs().get("context.install.once",what); 230 if (noInstallHandler!=null) { 231 noInstallHandler.problem(error,Project.MSG_ERR); 232 } 233 throw new BuildException(error); 234 } 235 return overlays.install(category,next); 236 } 237 } 238 239 240 241 252 public static void uninstallIfIs(Object category, 253 ProblemHandler noUninstallHandler, 254 String what) 255 { 256 FixtureOverlays overlays = FixtureOverlays.getContextInstance(); 257 if (!overlays.unwind(category) && noUninstallHandler!=null) { 258 String warning = AntX.uistrs().get("context.all.uninstalled",what); 259 noUninstallHandler.problem(warning,Project.MSG_WARN); 260 } 261 } 262 263 264 265 269 public synchronized int numberInvalidUnwinds() 270 { 271 return m_invalidCalls; 272 } 273 274 275 276 280 private Stack callstack(Object category) 281 { 282 if (category==null) { 283 throw new IllegalArgumentException ("callstack- NULL category"); 284 } 285 return (Stack )m_Env.get(category); 286 } 287 288 289 290 294 private Stack create(Object category) 295 { 296 Stack all = new Stack (); 297 m_Env.put(category, all); 298 return all; 299 } 300 301 302 private HashMap m_Env = new HashMap (31,0.85f); 303 private int m_invalidCalls; 304 305 306 308 319 public static final class Handle extends InheritableThreadLocal 320 { 321 324 public Handle() 325 { 326 super(); 327 } 328 329 332 protected Object initialValue() 333 { 334 return new FixtureOverlays(); 335 } 336 337 340 protected Object childValue(Object parentValue) 341 { 342 return ((FixtureOverlays)parentValue).clone(); 343 } 344 } 345 346 347 349 350 358 public static final FixtureOverlays getContextInstance() 359 { 360 return (FixtureOverlays)Iteration.fixtureOverlays().get(); 361 } 362 } 363 364 365 | Popular Tags |