1 46 package org.codehaus.groovy.runtime; 47 48 import groovy.lang.Closure; 49 50 import java.util.HashMap ; 51 import java.util.Map ; 52 53 54 60 public class ClassExtender { 61 private Map variables; 62 private Map methods; 63 64 public synchronized Object get(String name) { 65 if (variables != null) { 66 return variables.get(name); 67 } 68 return null; 69 } 70 71 public synchronized void set(String name, Object value) { 72 if (variables == null) { 73 variables = createMap(); 74 } 75 variables.put(name, value); 76 } 77 78 public synchronized void remove(String name) { 79 if (variables != null) { 80 variables.remove(name); 81 } 82 } 83 84 public void call(String name, Object params) { 85 Closure closure = null; 86 synchronized (this) { 87 if (methods != null) { 88 closure = (Closure) methods.get(name); 89 } 90 } 91 if (closure != null) { 92 closure.call(params); 93 } 94 99 } 100 101 public synchronized void addMethod(String name, Closure closure) { 102 if (methods == null) { 103 methods = createMap(); 104 } 105 methods.put(name, methods); 106 } 107 108 public synchronized void removeMethod(String name) { 109 if (methods != null) { 110 methods.remove(name); 111 } 112 } 113 114 protected Map createMap() { 115 return new HashMap (); 116 } 117 } 118 | Popular Tags |