1 package cz.cuni.amis.pogamut.base.component.bus.event.impl; 2 3 import java.lang.ref.SoftReference; 4 import java.lang.reflect.Method; 5 6 import cz.cuni.amis.pogamut.base.component.IComponent; 7 import cz.cuni.amis.pogamut.base.component.bus.IComponentEvent; 8 import cz.cuni.amis.utils.NullCheck; 9 10 /** 11 * Note that this is sort of utility class providing pretty-print (reflection based) for toString(), not every 12 * event must be descendant of this class. For listener event class specification use always interfaces! 13 * @author Jimmy 14 * 15 * @param <SOURCE> 16 */ 17 public class ComponentEvent<SOURCE extends IComponent> implements IComponentEvent<SOURCE> { 18 19 private SOURCE source; 20 21 private SoftReference<String> desc = null; 22 23 public ComponentEvent(SOURCE source) { 24 this.source = source; 25 NullCheck.check(this.source, "source"); 26 } 27 28 @Override 29 public SOURCE getSource() { 30 return source; 31 } 32 33 // @Override 34 // public String toString() { 35 // if (desc != null) { 36 // String desc = this.desc.get(); 37 // if (desc != null) return desc; 38 // } 39 // StringBuffer sb = new StringBuffer(); 40 // sb.append(getClass().getSimpleName()); 41 // sb.append("["); 42 // Method[] methods = getClass().getMethods(); 43 // boolean first = true; 44 // for (Method method : methods) { 45 // if (!method.getName().startsWith("get") || method.getParameterTypes().length != 0 || method.getReturnType().equals(Void.class)) { 46 // continue; 47 // } 48 // if (first) first = false; 49 // else sb.append(", "); 50 // sb.append(method.getName().substring(3)); 51 // sb.append("="); 52 // Object obj; 53 // try { 54 // obj = method.invoke(this); 55 // } catch (Exception e) { 56 // sb.append("can't be obtained (" + e.getMessage() + ")"); 57 // continue; 58 // } 59 // sb.append(obj == null ? "null" : obj.toString()); 60 // } 61 // sb.append("]"); 62 // String desc = sb.toString(); 63 // this.desc = new SoftReference<String>(sb.toString()); 64 // return desc; 65 // } 66 67 public String toString() { 68 return getClass().getSimpleName() + "[source=" + source + "]"; 69 } 70 71 }