1 30 31 34 package com.nightlabs.notification; 35 36 import java.util.ArrayList ; 37 import java.util.Collection ; 38 import java.util.Collections ; 39 import java.util.EventObject ; 40 import java.util.Iterator ; 41 import java.util.List ; 42 43 46 public class NotificationEvent 47 extends EventObject 48 { 50 private String zone = null; 51 52 private List subjectCarriers = new ArrayList (); 53 54 59 private transient List subjects = null; 60 61 68 private transient List subjectClassesToClear = null; 69 70 protected void clearCache() 71 { 72 subjects = null; 73 subjectClassesToClear = null; 74 } 75 76 80 public NotificationEvent(Object source, Object subject) 81 { 82 this(source, (String )null, subject); 83 } 84 85 93 public NotificationEvent(Object source, String zone, Object subject) 94 { 95 this(source, zone, subject, null); 96 } 97 98 102 public NotificationEvent(Object source, Object subject, Class subjectClassToClear) 103 { 104 this(source, (String )null, subject, subjectClassToClear); 105 } 106 107 114 public NotificationEvent(Object source, String zone, Object subject, Class subjectClassToClear) 115 { 116 super(source); 117 this.zone = zone; 118 if (subject == null && subjectClassToClear == null) 119 throw new NullPointerException ("subject and subjectClass are both null! At least one of them must be defined!"); 120 121 if (subject != null) 122 subjectCarriers.add(new SubjectCarrier(subject)); 123 124 if (subjectClassToClear != null) 125 subjectCarriers.add(new SubjectCarrier(subjectClassToClear)); 126 } 127 128 132 public NotificationEvent(Object source, Object [] subjects) 133 { 134 this(source, (String )null, subjects); 135 } 136 137 141 public NotificationEvent(Object source, String zone, Object [] subjects) 142 { 143 this(source, zone, subjects, (Class [])null); 144 } 145 146 150 public NotificationEvent(Object source, Object [] subjects, Class [] subjectClassesToClear) 151 { 152 this(source, (String )null, subjects, subjectClassesToClear); 153 } 154 155 162 public NotificationEvent(Object source, String zone, Object [] subjects, Class [] subjectClassesToClear) 163 { 164 super(source); 165 this.zone = zone; 166 if (subjects == null && subjectClassesToClear == null) 167 throw new NullPointerException ("subjects and subjectClassesToClear are both null! At least one of them must be defined!"); 168 169 if (subjects != null) { 170 for (int i = 0; i < subjects.length; ++i) { 171 174 if (subjects[i] != null) 175 this.subjectCarriers.add(new SubjectCarrier(subjects[i])); 176 } 177 } 179 if (subjectClassesToClear != null) { 180 for (int i = 0; i < subjectClassesToClear.length; ++i) { 181 if (subjectClassesToClear[i] == null) 182 throw new NullPointerException ("Class array 'subjectClassesToClear' must not contain null!"); 183 184 this.subjectCarriers.add(new SubjectCarrier(subjectClassesToClear[i])); 185 } 186 } 188 if (this.subjectCarriers.isEmpty()) 189 throw new IllegalArgumentException ("subjects and subjectClassesToClear are both empty! Need at least one entry in either of them!"); 190 } 191 192 196 public NotificationEvent(Object source, Collection subjects) 197 { 198 this(source, (String )null, subjects); 199 } 200 201 205 public NotificationEvent(Object source, String zone, Collection subjects) 206 { 207 this(source, zone, subjects, (Collection )null); 208 } 209 210 214 public NotificationEvent(Object source, Collection subjects, Collection subjectClassesToClear) 215 { 216 this(source, (String )null, subjects, subjectClassesToClear); 217 } 218 219 223 public NotificationEvent(Object source, String zone, Collection subjects, Collection subjectClassesToClear) 224 { 225 this(source, zone, subjects, subjectClassesToClear, null); 226 } 227 228 public NotificationEvent(Object source, String zone, Collection subjects, Collection subjectClassesToClear, Collection _subjectCarriers) 229 { 230 super(source); 231 this.zone = zone; 232 if (subjects == null && subjectClassesToClear == null && _subjectCarriers == null) 233 throw new NullPointerException ("subjects and subjectClassesToClear and subjectCarriers are all null! At least one of them must be defined!"); 234 235 if (subjects != null) { 236 for (Iterator it = subjects.iterator(); it.hasNext(); ) { 237 Object subject = it.next(); 238 239 if (subject != null) 240 this.subjectCarriers.add(new SubjectCarrier(subject)); 241 } 242 } 244 if (subjectClassesToClear != null) { 245 for (Iterator it = subjectClassesToClear.iterator(); it.hasNext(); ) { 246 Class subjectClassToClear = (Class )it.next(); 247 if (subjectClassToClear == null) 248 throw new NullPointerException ("Class collection 'subjectClassesToClear' must not contain null!"); 249 250 this.subjectCarriers.add(new SubjectCarrier(subjectClassToClear)); 251 } 252 } 254 if (_subjectCarriers != null) { 255 for (Iterator it = _subjectCarriers.iterator(); it.hasNext(); ) { 256 SubjectCarrier carrier = (SubjectCarrier) it.next(); 257 if (carrier != null) 258 this.subjectCarriers.add(carrier); 259 } 260 } 261 262 if (this.subjectCarriers.isEmpty()) 263 throw new IllegalArgumentException ("subjects and subjectClassesToClear are both empty! Need at least one entry in either of them!"); 264 } 265 266 public NotificationEvent(Object source, String zone, SubjectCarrier subjectCarrier) 267 { 268 super(source); 269 this.zone = zone; 270 if (subjectCarrier != null) 271 subjectCarriers.add(subjectCarrier); 272 } 273 274 public NotificationEvent(Object source, String zone, SubjectCarrier[] subjectCarriers) 275 { 276 super(source); 277 this.zone = zone; 278 if (subjectCarriers != null) 279 for (int i = 0; i < subjectCarriers.length; i++) { 280 addSubjectCarrier(subjectCarriers[i]); 281 } 282 } 283 284 287 public String getZone() 288 { 289 return zone; 290 } 291 292 295 public List getSubjects() 296 { 297 if (subjects == null) { 298 ArrayList l = new ArrayList (); 299 for (Iterator it = subjectCarriers.iterator(); it.hasNext(); ) { 300 SubjectCarrier carrier = (SubjectCarrier) it.next(); 301 if (carrier.getSubject() != null) 302 l.add(carrier.getSubject()); 303 } 304 subjects = Collections.unmodifiableList(l); 305 } 306 return subjects; 307 } 308 309 312 public Object getFirstSubject() 313 { 314 List subjects = getSubjects(); 315 if(subjects.size() > 0) 316 return subjects.get(0); 317 return null; 318 } 319 320 323 public List getSubjectClassesToClear() 324 { 325 if (subjectClassesToClear == null) { 326 ArrayList l = new ArrayList (); 327 for (Iterator it = subjectCarriers.iterator(); it.hasNext(); ) { 328 SubjectCarrier carrier = (SubjectCarrier) it.next(); 329 for (Iterator i2 = carrier.getSubjectClasses().iterator(); i2.hasNext(); ) { 330 Class clazz = (Class ) i2.next(); 331 l.add(clazz); 332 } 333 } 334 subjectClassesToClear = Collections.unmodifiableList(l); 335 } 336 337 return subjectClassesToClear; 338 } 339 340 public void addSubjectCarrier(SubjectCarrier subjectCarrier) 341 { 342 subjectCarriers.add(subjectCarrier); 343 clearCache(); 344 } 345 346 public void removeSubjectCarrier(int index) 347 { 348 subjectCarriers.remove(index); 349 clearCache(); 350 } 351 352 public void removeSubjectCarrier(SubjectCarrier subjectCarrier) 353 { 354 subjectCarriers.remove(subjectCarrier); 355 clearCache(); 356 } 357 358 private transient List subjectCarriersReadOnly = null; 359 362 public List getSubjectCarriers() 363 { 364 if (subjectCarriersReadOnly == null) 365 subjectCarriersReadOnly = Collections.unmodifiableList(subjectCarriers); 366 367 return subjectCarriersReadOnly; 368 } 369 370 } 388 | Popular Tags |