1 4 package nl.justobjects.pushlet.core; 5 6 import java.io.Serializable ; 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 import java.util.Map ; 10 11 17 public class Event implements Protocol, Serializable { 18 19 protected Map attributes = new HashMap (3); 20 21 public Event(String anEventType) { 22 this(anEventType, null); 23 } 24 25 public Event(String anEventType, Map theAttributes) { 26 27 if (theAttributes != null) { 28 setAttrs(theAttributes); 29 } 30 31 setField(P_EVENT, anEventType); 33 34 setField(P_TIME, System.currentTimeMillis() / 1000); 36 } 37 38 public Event(Map theAttributes) { 39 if (!theAttributes.containsKey(P_EVENT)) { 40 throw new IllegalArgumentException (P_EVENT + " not found in attributes"); 41 } 42 setAttrs(theAttributes); 43 } 44 45 public static Event createDataEvent(String aSubject) { 46 return createDataEvent(aSubject, null); 47 } 48 49 public static Event createDataEvent(String aSubject, Map theAttributes) { 50 Event dataEvent = new Event(E_DATA, theAttributes); 51 dataEvent.setField(P_SUBJECT, aSubject); 52 return dataEvent; 53 } 54 55 public String getEventType() { 56 return getField(P_EVENT); 57 } 58 59 public String getSubject() { 60 return getField(P_SUBJECT); 61 } 62 63 public void setField(String name, String value) { 64 attributes.put(name, value); 65 } 66 67 public void setField(String name, int value) { 68 attributes.put(name, value + ""); 69 } 70 71 public void setField(String name, long value) { 72 attributes.put(name, value + ""); 73 } 74 75 public String getField(String name) { 76 return (String ) attributes.get(name); 77 } 78 79 80 public String getField(String name, String aDefault) { 81 String result = getField(name); 82 return result == null ? aDefault : result; 83 } 84 85 public Iterator getFieldNames() { 86 return attributes.keySet().iterator(); 87 } 88 89 public String toString() { 90 return attributes.toString(); 91 } 92 93 94 public String toQueryString() { 95 String queryString = ""; 96 String amp = ""; 97 for (Iterator iter = getFieldNames(); iter.hasNext();) { 98 String nextAttrName = (String ) iter.next(); 99 String nextAttrValue = getField(nextAttrName); 100 queryString = queryString + amp + nextAttrName + "=" + nextAttrValue; 101 amp = "&"; 103 } 104 105 return queryString; 106 } 107 108 public String toXML() { 109 String xmlString = "<event "; 110 for (Iterator iter = getFieldNames(); iter.hasNext();) { 111 String nextAttrName = (String ) iter.next(); 112 String nextAttrValue = getField(nextAttrName); 113 xmlString = xmlString + nextAttrName + "=\"" + nextAttrValue + "\" "; 114 } 115 116 xmlString += "/>"; 117 return xmlString; 118 } 119 120 public Object clone() { 121 return new Event(attributes); 123 } 124 125 126 private void setAttrs(Map theAttributes) { 127 attributes.putAll(theAttributes); 128 } 129 } 130 131 164 | Popular Tags |