1 18 19 package alt.jiapi.event; 20 21 import alt.jiapi.Rule; 22 23 30 public class EventProducer { 31 private String [] resolutions; 32 private Rule[] rules; 33 34 39 private IdentityList locks = new IdentityList(); 40 41 45 public EventProducer() { 46 this(new String [] {"*"}); 47 } 48 49 54 public EventProducer(String resolution) { 55 this(new String [] {resolution}); 56 } 57 58 63 public EventProducer(String [] resolutions) { 64 this.resolutions = resolutions; 65 this.rules = new Rule[resolutions.length]; 66 67 for (int i = 0; i < rules.length; i++) { 68 try { 69 rules[i] = new Rule(resolutions[i]); 70 } 71 catch(Exception e) { 72 System.out.println("Invalid rule: " + e); 73 } 74 } 75 } 76 77 78 82 public boolean match(String s) { 83 for (int i = 0; i < rules.length; i++) { 84 if (rules[i].match(s) == true) { 85 return true; 86 } 87 } 88 89 return false; 90 } 91 92 93 99 public String [] getResolutions() { 100 return resolutions; 101 } 102 103 110 public boolean isProtected(Object sourceObject) { 111 return locks.contains(sourceObject); 112 } 113 114 121 public boolean isProtected(JiapiEvent je) { 122 Object sourceObject = je.getSourceObject(); 123 return locks.contains(sourceObject); 124 } 125 126 146 public void protect(JiapiEvent je) { 147 Object sourceObject = je.getSourceObject(); 148 locks.add(sourceObject); 149 } 150 151 158 public void release(JiapiEvent je) { 159 Object sourceObject = je.getSourceObject(); 160 locks.remove(sourceObject); 161 } 162 } 163 164 173 class IdentityList { 174 private Object []elementData; 175 private int elementCount; 176 177 public IdentityList() { 178 elementData = new Object [10]; 179 elementCount = 0; 180 } 181 182 public synchronized void add(Object o) { 183 checkCapacity(); 184 elementData[elementCount++] = o; 185 } 186 187 public synchronized void remove(Object o) { 188 int index = indexOf(o); 189 190 if (index != -1) { 191 removeElementAt(index); 192 } 193 } 194 195 public synchronized boolean contains(Object o) { 196 return indexOf(o) >= 0; 197 } 198 199 private int indexOf(Object o) { 200 for (int i = 0; i < elementCount; i++) { 201 if (o == elementData[i]) { 202 return i; 203 } 204 } 205 206 return -1; 207 } 208 209 private void removeElementAt(int index) { 210 if (index >= elementCount) { 211 throw new ArrayIndexOutOfBoundsException (index + " >= " + 212 elementCount); 213 } 214 else if (index < 0) { 215 throw new ArrayIndexOutOfBoundsException (index); 216 } 217 218 int j = elementCount - index - 1; 219 if (j > 0) { 220 System.arraycopy(elementData, index + 1, elementData, index, j); 221 } 222 223 elementCount--; 224 elementData[elementCount] = null; 225 } 226 227 private void checkCapacity() { 228 int curCapacity = elementData.length; 229 if (elementCount >= curCapacity) { 230 Object oldData[] = elementData; 231 int newCapacity = curCapacity * 2; 232 elementData = new Object [newCapacity]; 233 System.arraycopy(oldData, 0, elementData, 0, elementCount); 234 } 235 } 236 } 237 | Popular Tags |