1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.status;
15
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Objects;
20
21 abstract public class StatusBase implements Status {
22
23 static private final List<Status> EMPTY_LIST = new ArrayList<Status>(0);
24
25 int level;
26 final String message;
27 final Object origin;
28 List<Status> childrenList;
29 Throwable throwable;
30 long timestamp;
31
32 StatusBase(int level, String msg, Object origin) {
33 this(level, msg, origin, null);
34 }
35
36 StatusBase(int level, String msg, Object origin, Throwable t) {
37 this.level = level;
38 this.message = msg;
39 this.origin = origin;
40 this.throwable = t;
41 this.timestamp = System.currentTimeMillis();
42 }
43
44 public synchronized void add(Status child) {
45 if (child == null) {
46 throw new NullPointerException("Null values are not valid Status.");
47 }
48 if (childrenList == null) {
49 childrenList = new ArrayList<Status>();
50 }
51 childrenList.add(child);
52 }
53
54 public synchronized boolean hasChildren() {
55 return ((childrenList != null) && (childrenList.size() > 0));
56 }
57
58 public synchronized Iterator<Status> iterator() {
59 if (childrenList != null) {
60 return childrenList.iterator();
61 } else {
62 return EMPTY_LIST.iterator();
63 }
64 }
65
66 public synchronized boolean remove(Status statusToRemove) {
67 if (childrenList == null) {
68 return false;
69 }
70
71 return childrenList.remove(statusToRemove);
72 }
73
74 public int getLevel() {
75 return level;
76 }
77
78
79
80
81
82 public synchronized int getEffectiveLevel() {
83 int result = level;
84 int effLevel;
85
86 Iterator<Status> it = iterator();
87 Status s;
88 while (it.hasNext()) {
89 s = (Status) it.next();
90 effLevel = s.getEffectiveLevel();
91 if (effLevel > result) {
92 result = effLevel;
93 }
94 }
95 return result;
96 }
97
98 public String getMessage() {
99 return message;
100 }
101
102 public Object getOrigin() {
103 return origin;
104 }
105
106 public Throwable getThrowable() {
107 return throwable;
108 }
109
110 public long getTimestamp() {
111 return timestamp;
112 }
113
114 @Override
115 public String toString() {
116 StringBuilder buf = new StringBuilder();
117 switch (getEffectiveLevel()) {
118 case INFO:
119 buf.append("INFO");
120 break;
121 case WARN:
122 buf.append("WARN");
123 break;
124 case ERROR:
125 buf.append("ERROR");
126 break;
127 }
128 if (origin != null) {
129 buf.append(" in ");
130 buf.append(origin);
131 buf.append(" -");
132 }
133
134 buf.append(" ");
135 buf.append(message);
136
137 if (throwable != null) {
138 buf.append(" ");
139 buf.append(throwable);
140 }
141
142 return buf.toString();
143 }
144
145 @Override
146 public boolean equals(Object o) {
147 if (this == o)
148 return true;
149 if (o == null || getClass() != o.getClass())
150 return false;
151 StatusBase that = (StatusBase) o;
152 return level == that.level && timestamp == that.timestamp && Objects.equals(message, that.message);
153 }
154
155
156
157 @Override
158 public int hashCode() {
159 return Objects.hash(level, message, timestamp);
160 }
161
162 }