01 /*
02 * Created on Aug 30, 2007
03 */
04 package com.x8ing.mc.bp;
05
06 import java.text.SimpleDateFormat;
07 import java.util.ArrayList;
08 import java.util.Calendar;
09 import java.util.Iterator;
10 import java.util.List;
11
12 /**
13 * A very simple log book to journalize what was going on.
14 *
15 * @author Patrick Heusser
16 */
17 public class LogBook {
18
19 private SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.GLOBAL_DATE_FORMAT);
20
21 /**
22 * type: LogEntry
23 */
24 private List logs = new ArrayList();
25
26 public void addLogEntry(Calendar dateOfEvent, String text) {
27 logs.add(new LogEntry(dateOfEvent, text));
28
29 if (Constants.TRACE_DEBUG) {
30 System.out.println(simpleDateFormat.format(dateOfEvent.getTime()) + "\tLogBook:\t" + text);
31 }
32 }
33
34 /**
35 * for bean mode
36 */
37 public String getPrintLogBook() {
38 return printLogBook(true);
39 }
40
41 public String printLogBook(boolean supressSameDates) {
42
43 StringBuffer ret = new StringBuffer();
44
45 String lastPrintedDate = "";
46
47 for (Iterator it = logs.iterator(); it.hasNext();) {
48 LogEntry logEntry = (LogEntry) it.next();
49
50 String currentDate = simpleDateFormat.format(logEntry.dateOfEvent.getTime());
51
52 if (supressSameDates && currentDate.equals(lastPrintedDate)) {
53 ret.append(" ");
54 } else {
55 ret.append(currentDate);
56 ret.append(":");
57 }
58 lastPrintedDate = currentDate;
59
60 ret.append("\t");
61 ret.append(logEntry.text);
62 ret.append("\n");
63
64 }
65
66 return ret.toString();
67
68 }
69
70 public String toString() {
71 return printLogBook(false);
72 }
73
74 private static class LogEntry {
75
76 public Calendar dateOfEvent = null;
77
78 public String text = null;
79
80 public LogEntry(Calendar dateOfEvent, String text) {
81 super();
82 this.dateOfEvent = (Calendar) dateOfEvent.clone();
83 this.text = text;
84 }
85 }
86
87 }
|