1 package net.sourceforge.argval.message.impl;
2
3
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.HashMap;
7 import java.util.Iterator;
8 import java.util.LinkedList;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Set;
12
13 import net.sourceforge.argval.ArgumentValidation;
14 import net.sourceforge.argval.impl.ArgumentValidationImpl;
15 import net.sourceforge.argval.message.MessageItem;
16 import net.sourceforge.argval.message.MessageStore;
17 import net.sourceforge.argval.message.MessageStoreVisitor;
18 import net.sourceforge.argval.message.MessageStoreVisitorAccepter;
19 import net.sourceforge.argval.utils.StringUtil;
20
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24
25 public class MessageStoreImpl implements MessageStore, MessageStoreVisitorAccepter {
26 public final static String PRIORITY_FATAL = "fatal";
27 public final static String PRIORITY_ERROR = "error";
28 public final static String PRIORITY_WARN = "warn";
29 public final static String PRIORITY_INFO = "info";
30 public final static String PRIORITY_DEBUG = "debug";
31
32
33 private transient static Logger logger = LoggerFactory.getLogger(MessageStoreImpl.class);
34
35
36 private static List<String> initialPriorityList;
37
38 private List<String> priorityList;
39
40 private Map<String, List<MessageItem>> priorityMap;
41
42 private List<MessageItemImpl> messageItemList;
43
44
45 static {
46 initialPriorityList = new ArrayList<String>();
47 initialPriorityList.add(PRIORITY_FATAL);
48 initialPriorityList.add(PRIORITY_ERROR);
49 initialPriorityList.add(PRIORITY_WARN);
50 initialPriorityList.add(PRIORITY_INFO);
51 initialPriorityList.add(PRIORITY_DEBUG);
52 }
53
54
55 public MessageStoreImpl(List<String> priorityList) {
56 super();
57 priorityMap = new HashMap<String, List<MessageItem>>();
58 messageItemList = new LinkedList<MessageItemImpl>();
59 this.priorityList = priorityList;
60 for (Iterator<String> iter = priorityList.iterator(); iter.hasNext();) {
61 priorityMap.put(iter.next(), new LinkedList<MessageItem>());
62 }
63 }
64
65
66
67
68
69 public MessageStoreImpl() {
70 this(initialPriorityList);
71 }
72
73
74 public Set<String> getPriorities() {
75 return priorityMap.keySet();
76 }
77
78
79 public List<String> getOrderedPriorities() {
80 return priorityList;
81 }
82
83
84 public List<MessageItem> getMessageItemList(String priority) {
85 return priorityMap.get(priority);
86 }
87 public List<MessageItem> getMessageItemList() {
88 return Collections.<MessageItem>unmodifiableList(messageItemList);
89 }
90
91
92 public void addMessage(String priority, String message) {
93
94 addMessage(priority, message, null);
95 }
96 public void addMessage(String priority, String message, Throwable cause) {
97 message = ArgumentValidationImpl.polish(message);
98 ArgumentValidation argVal = new ArgumentValidationImpl();
99 argVal.isValidWhenInSet("priority", priority, priorityMap.keySet());
100 if (message == null && cause == null) {
101 argVal.addError("Both arguments 'message' and 'cause' can not be left empty.");
102 }
103
104 if (argVal.containsIllegalArgument()) throw argVal.createIllegalArgumentException();
105
106 MessageItemImpl messageItem = new MessageItemImpl(priority, message, cause);
107 priorityMap.get(priority).add(messageItem);
108 messageItemList.add(messageItem);
109 }
110
111
112 public void addFatal(String message) {
113 addMessage(PRIORITY_FATAL, message);
114 }
115 public void addFatal(String message, Throwable cause) {
116 addMessage(PRIORITY_FATAL, message, cause);
117 }
118 public void addError(String message) {
119 addMessage(PRIORITY_ERROR, message);
120 }
121 public void addError(String message, Throwable cause) {
122 addMessage(PRIORITY_ERROR, message, cause);
123 }
124 public void addWarn(String message) {
125 addMessage(PRIORITY_WARN, message);
126 }
127 public void addWarn(String message, Throwable cause) {
128 addMessage(PRIORITY_WARN, message, cause);
129 }
130 public void addInfo(String message) {
131 addMessage(PRIORITY_INFO, message);
132 }
133 public void addInfo(String message, Throwable cause) {
134 addMessage(PRIORITY_INFO, message, cause);
135 }
136 public void addDebug(String message) {
137 addMessage(PRIORITY_DEBUG, message);
138 }
139 public void addDebug(String message, Throwable cause) {
140 addMessage(PRIORITY_DEBUG, message, cause);
141 }
142
143
144 public boolean isFatalAdded() {
145 return isMessageAdded(PRIORITY_FATAL);
146 }
147 public boolean isErrorAdded() {
148 return isMessageAdded(PRIORITY_ERROR);
149 }
150 public boolean isWarnAdded() {
151 return isMessageAdded(PRIORITY_WARN);
152 }
153 public boolean isInfoAdded() {
154 return isMessageAdded(PRIORITY_INFO);
155 }
156 public boolean isDebugAdded() {
157 return isMessageAdded(PRIORITY_DEBUG);
158 }
159
160
161 private boolean isMessageAdded(String priority) {
162 boolean isMessageAdded = false;
163 for (Iterator<String> iter = priorityList.iterator(); iter.hasNext();) {
164 String prio = (String)iter.next();
165 logger.debug(priorityList.indexOf(prio) + " (" + prio +") <= "
166 + priorityList.indexOf(priority) + " (" + priority +") "
167 + " " + (priorityList.indexOf(prio) <= priorityList.indexOf(priority)));
168 if (priorityList.indexOf(prio) <= priorityList.indexOf(priority)) {
169 isMessageAdded = isMessageAdded && priorityMap.get(priority).size() > 0;
170 }
171
172 }
173 return isMessageAdded;
174 }
175
176
177 public boolean isEmpty() {
178 boolean isEmpty = true;
179 for (Iterator<String> iter = priorityMap.keySet().iterator(); isEmpty && iter.hasNext(); ) {
180 String priority = (String) iter.next();
181 isEmpty = priorityMap.get(priority).isEmpty();
182 }
183 return isEmpty;
184 }
185
186
187
188
189
190
191 public void accept(MessageStoreVisitor visitor) {
192 visitor.visit(this);
193 for (MessageStoreVisitorAccepter acceptor: messageItemList) {
194 acceptor.accept(visitor);
195 }
196 }
197
198
199 public StringBuffer toStringBuffer(StringBuffer strBuf, String priority) {
200 for (Iterator<MessageItem> iter = priorityMap.get(priority).iterator(); iter.hasNext();) {
201 MessageItemImpl message = (MessageItemImpl)iter.next();
202 if (strBuf.length() > 0) {
203 strBuf.append(StringUtil.LINE_SEPARATOR);
204 }
205 if (message.getMessage() != null) {
206 strBuf.append(priority).append(": ").append(message.getMessage());
207 }
208 if (message.getCause() != null) {
209 if (strBuf.length() > 0) {
210 strBuf.append(StringUtil.LINE_SEPARATOR);
211 }
212 strBuf.append(priority).append(": ").append(message.getCause().getMessage());
213 }
214 }
215 return strBuf;
216 }
217
218
219 public String toString() {
220 StringBuffer strBuf = new StringBuffer();
221 for (Iterator<String> iter = priorityList.iterator(); iter.hasNext();) {
222 toStringBuffer(strBuf, (String)iter.next());
223 }
224 return strBuf.toString();
225 }
226
227 }