Menu

[r1]: / src / Event.java  Maximize  Restore  History

Download this file

278 lines (236 with data), 7.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import java.io.*;
import java.text.*;
import java.util.*;
public class Event implements Comparable, Serializable {
static final long serialVersionUID = 3748688294296L;
private long eventID; // unique identifier
private Date timestamp; // time of event (used for ordering)
private Set<String> tags; // boolean tags
private Map<String, String> properties; // O(1) data items
private Map<String, Attachment> attachments; // O(n) data items (stored in files)
private List<Event> subEvents; // merged events
private EventLog log;
private Event parent;
public Event(long id, Date timestamp, EventLog log) {
this.eventID = id;
this.timestamp = (Date)timestamp.clone();
this.tags = new HashSet<String>();
this.properties = new HashMap<String, String>();
this.attachments = new HashMap<String, Attachment>();
this.subEvents = new ArrayList<Event>();
this.log = log;
this.parent = null;
}
public Event(long id, Date timestamp, EventLog log, String tag) {
this(id, timestamp, log);
addTag(tag);
}
// {{{ data accessors
public long getID() {
return eventID;
}
public Date getTimestamp() {
return (Date)timestamp.clone();
}
public void addTag(String tag) {
if (!hasTag(tag)) {
tags.add(tag);
}
}
public Set<String> getTags() {
return tags;
}
public boolean hasTag(String tag) {
return tags.contains(tag);
}
public void setProperty(String key, String value) {
properties.put(key, value);
}
public boolean hasProperty(String key) {
return properties.containsKey(key);
}
public String getProperty(String key) {
return properties.get(key);
}
public void setAttachment(String key, Attachment value) {
attachments.put(key, value);
}
public boolean hasAttachment(String key) {
return attachments.containsKey(key);
}
public Attachment getAttachment(String key) {
return attachments.get(key);
}
public int getAttachmentCount() {
return attachments.size();
}
public String getFormattedTimestamp() {
return DateFormat.getTimeInstance(DateFormat.SHORT).format(timestamp);
}
public String getDetailPreview() {
String details = getDetails(), desc = "";
if (details != null) {
int pos = details.indexOf('\n');
if (pos >=0) {
desc += " " + details.substring(0,pos) + " ...";
} else if (details.length() < 100) {
desc += " " + details;
}
}
return desc;
}
public String getDetails() {
String details = null;
if (hasProperty("details")) {
details = getProperty("details");
} else if (hasAttachment("details")) {
details = getAttachment("details").getTextData();
}
return details;
}
public boolean isAggregate() {
return hasTag("aggregate");
}
public void addSubEvent(Event evt) {
subEvents.add(evt);
}
public List<Event> getSubEvents() {
return subEvents;
}
public Event getSubEvent(int i) {
return subEvents.get(i);
}
public void setEventLog(EventLog log) {
this.log = log;
for (Attachment att : attachments.values()) {
att.setEventLog(log);
}
for (Event e : subEvents) {
e.setEventLog(log);
}
}
public EventLog getEventLog() {
return log;
}
public void setParent(Event parent) {
this.parent = parent;
}
public Event getParent() {
return parent;
}
// }}}
public int hashCode() {
return timestamp.hashCode();
}
public boolean equals(Object o) {
if (o instanceof Event) {
return timestamp.equals(((Event)o).getTimestamp());
} else {
throw new ClassCastException();
}
}
public int compareTo(Object o) {
if (o instanceof Event) {
return timestamp.compareTo(((Event)o).getTimestamp());
} else {
throw new ClassCastException();
}
}
private void writeObject(java.io.ObjectOutputStream out)
throws IOException {
out.writeLong(eventID);
out.writeObject(timestamp);
out.writeInt(tags.size());
for (String t : tags) {
out.writeUTF(t);
}
out.writeInt(properties.size());
for (String key : properties.keySet()) {
out.writeUTF(key);
out.writeUTF(properties.get(key));
}
out.writeInt(attachments.size());
for (String key : attachments.keySet()) {
out.writeUTF(key);
out.writeObject(attachments.get(key));
}
out.writeInt(subEvents.size());
for (Event e : subEvents) {
out.writeObject(e);
}
}
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException {
String key, value;
Attachment att;
Event evt;
int num, i;
tags = new HashSet<String>();
properties = new HashMap<String, String>();
attachments = new HashMap<String, Attachment>();
subEvents = new ArrayList<Event>();
eventID = in.readLong();
timestamp = (Date)in.readObject();
num = in.readInt();
for (i=0; i<num; i++) {
tags.add(in.readUTF());
}
num = in.readInt();
for (i=0; i<num; i++) {
key = in.readUTF();
value = in.readUTF();
properties.put(key, value);
}
num = in.readInt();
for (i=0; i<num; i++) {
key = in.readUTF();
att = (Attachment)in.readObject();
attachments.put(key, att);
}
num = in.readInt();
for (i=0; i<num; i++) {
evt = (Event)in.readObject();
evt.parent = this;
subEvents.add(evt);
}
log = null;
parent = null;
}
private void readObjectNoData()
throws ObjectStreamException {
eventID = 0;
timestamp = new Date();
tags = new HashSet<String>();
properties = new HashMap<String, String>();
attachments = new HashMap<String, Attachment>();
subEvents = new ArrayList<Event>();
log = null;
parent = null;
}
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append("======================================================================\n");
buffer.append("EVENT " + eventID + " - " + timestamp.toString() + ": ");
for (String tag : tags) {
buffer.append("[");
buffer.append(tag);
buffer.append("] ");
}
buffer.append("\n");
for (String key : properties.keySet()) {
buffer.append("property: ");
buffer.append(key);
buffer.append("=\"");
buffer.append(properties.get(key));
buffer.append("\"\n");
}
for (String key : attachments.keySet()) {
buffer.append("attachment: ");
buffer.append(key);
buffer.append(" ");
buffer.append(attachments.get(key).toString());
buffer.append("\n");
}
return buffer.toString();
}
}
MongoDB Logo MongoDB