Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MessagesImpl |
|
| 1.6842105263157894;1.684 | ||||
MessagesImpl$1 |
|
| 1.6842105263157894;1.684 | ||||
MessagesImpl$MessageItem |
|
| 1.6842105263157894;1.684 |
1 | /* | |
2 | * Copyright 2001,2004 The Apache Software Foundation. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | package org.apache.commons.scaffold.util; | |
18 | ||
19 | ||
20 | import java.io.Serializable; | |
21 | import java.util.ArrayList; | |
22 | import java.util.Collections; | |
23 | import java.util.Comparator; | |
24 | import java.util.Iterator; | |
25 | import java.util.HashMap; | |
26 | import java.util.List; | |
27 | ||
28 | // ------------------------------------------------------------------------ 78 | |
29 | ||
30 | public class MessagesImpl implements Serializable,Messages { | |
31 | ||
32 | /** | |
33 | * The accumulated set of <code>Message</code> objects (represented | |
34 | * as an ArrayList) for each property, keyed by property name. | |
35 | */ | |
36 | 0 | protected HashMap messages = new HashMap(); |
37 | ||
38 | /** | |
39 | * The current number of the property/key being added. This is used | |
40 | * to maintain the order messages are added. | |
41 | */ | |
42 | 0 | protected int iCount = 0; |
43 | ||
44 | // --------------------------------------------------------- Public Methods | |
45 | ||
46 | /** | |
47 | * Create an empty <code>Messages</code> object. | |
48 | */ | |
49 | public MessagesImpl() { | |
50 | 0 | super(); |
51 | 0 | } |
52 | ||
53 | /** | |
54 | * Create an <code>AMessages</code> object initialized with the given | |
55 | * messages. | |
56 | * | |
57 | * @param messages The messages to be initially added to this object. | |
58 | * @since Struts 1.1 | |
59 | */ | |
60 | public MessagesImpl(Messages messages) { | |
61 | 0 | super(); |
62 | 0 | this.add(messages); |
63 | 0 | } |
64 | ||
65 | ||
66 | // See interface for JavaDoc | |
67 | public void add(String property, Message message) { | |
68 | ||
69 | 0 | MessageItem item = (MessageItem) messages.get(property); |
70 | 0 | List list = null; |
71 | ||
72 | 0 | if (item == null) { |
73 | 0 | list = new ArrayList(); |
74 | 0 | item = new MessageItem(list, iCount++); |
75 | ||
76 | 0 | messages.put(property, item); |
77 | } else { | |
78 | 0 | list = item.getList(); |
79 | } | |
80 | ||
81 | 0 | list.add(message); |
82 | ||
83 | 0 | } |
84 | ||
85 | ||
86 | // See interface for JavaDoc | |
87 | public void add(Message message) { | |
88 | ||
89 | 0 | add(Messages.GLOBAL_MESSAGE_KEY,message); |
90 | 0 | } |
91 | ||
92 | ||
93 | // See interface for JavaDoc | |
94 | public void add(Messages messages) { | |
95 | // loop over properties | |
96 | 0 | Iterator props = messages.properties(); |
97 | 0 | while (props.hasNext()) { |
98 | 0 | String property = (String) props.next(); |
99 | ||
100 | // loop over messages for each property | |
101 | 0 | Iterator msgs = messages.get(property); |
102 | 0 | while (msgs.hasNext()) { |
103 | 0 | Message msg = (Message) msgs.next(); |
104 | 0 | this.add(property, msg); |
105 | 0 | } |
106 | ||
107 | 0 | } |
108 | 0 | } |
109 | ||
110 | // See interface for JavaDoc | |
111 | public void clear() { | |
112 | ||
113 | 0 | messages.clear(); |
114 | ||
115 | 0 | } |
116 | ||
117 | // See interface for JavaDoc | |
118 | public boolean empty() { | |
119 | 0 | return (this.isEmpty()); |
120 | } | |
121 | ||
122 | // See interface for JavaDoc | |
123 | public boolean isEmpty(){ | |
124 | 0 | return (messages.isEmpty()); |
125 | } | |
126 | ||
127 | // See interface for JavaDoc | |
128 | public Iterator get() { | |
129 | ||
130 | 0 | if (messages.size() == 0) { |
131 | 0 | return (Collections.EMPTY_LIST.iterator()); |
132 | } | |
133 | ||
134 | 0 | ArrayList results = new ArrayList(); |
135 | 0 | ArrayList actionItems = new ArrayList(); |
136 | ||
137 | 0 | for (Iterator i = messages.values().iterator(); i.hasNext();) { |
138 | 0 | actionItems.add(i.next()); |
139 | } | |
140 | ||
141 | // Sort MessageItems based on the initial order the | |
142 | // property/key was added to Messages. | |
143 | 0 | Collections.sort(actionItems, new Comparator() { |
144 | public int compare(Object o1, Object o2) { | |
145 | 0 | return ((MessageItem) o1).getOrder() - ((MessageItem) o2).getOrder(); |
146 | } | |
147 | }); | |
148 | ||
149 | 0 | for (Iterator i = actionItems.iterator(); i.hasNext();) { |
150 | 0 | MessageItem ami = (MessageItem) i.next(); |
151 | ||
152 | 0 | for (Iterator messages = ami.getList().iterator(); messages.hasNext();) { |
153 | 0 | results.add(messages.next()); |
154 | } | |
155 | 0 | } |
156 | ||
157 | 0 | return (results.iterator()); |
158 | ||
159 | } | |
160 | ||
161 | // See interface for JavaDoc | |
162 | public Iterator get(String property) { | |
163 | ||
164 | 0 | MessageItem item = (MessageItem) messages.get(property); |
165 | ||
166 | 0 | if (item == null) { |
167 | 0 | return (Collections.EMPTY_LIST.iterator()); |
168 | } else { | |
169 | 0 | return (item.getList().iterator()); |
170 | } | |
171 | ||
172 | } | |
173 | ||
174 | // See interface for JavaDoc | |
175 | public Iterator properties() { | |
176 | ||
177 | 0 | return (messages.keySet().iterator()); |
178 | ||
179 | } | |
180 | ||
181 | // See interface for JavaDoc | |
182 | public int size() { | |
183 | ||
184 | 0 | int total = 0; |
185 | ||
186 | 0 | for (Iterator i = messages.values().iterator(); i.hasNext();) { |
187 | 0 | MessageItem ami = (MessageItem) i.next(); |
188 | 0 | total += ami.getList().size(); |
189 | 0 | } |
190 | ||
191 | 0 | return (total); |
192 | ||
193 | } | |
194 | ||
195 | // See interface for JavaDoc | |
196 | public int size(String property) { | |
197 | ||
198 | 0 | MessageItem ami = (MessageItem) messages.get(property); |
199 | ||
200 | 0 | if (ami == null) |
201 | 0 | return (0); |
202 | else | |
203 | 0 | return (ami.getList().size()); |
204 | ||
205 | } | |
206 | ||
207 | // See interface for JavaDoc | |
208 | protected class MessageItem implements Serializable { | |
209 | ||
210 | /** | |
211 | * The list of <code>Message</code>s. | |
212 | */ | |
213 | 0 | protected List list = null; |
214 | ||
215 | /** | |
216 | * The position in the list of messages. | |
217 | */ | |
218 | 0 | protected int iOrder = 0; |
219 | ||
220 | 0 | public MessageItem(List list, int iOrder) { |
221 | 0 | this.list = list; |
222 | 0 | this.iOrder = iOrder; |
223 | 0 | } |
224 | ||
225 | public List getList() { | |
226 | 0 | return list; |
227 | } | |
228 | ||
229 | public void setList(List list) { | |
230 | 0 | this.list = list; |
231 | 0 | } |
232 | ||
233 | public int getOrder() { | |
234 | 0 | return iOrder; |
235 | } | |
236 | ||
237 | public void setOrder(int iOrder) { | |
238 | 0 | this.iOrder = iOrder; |
239 | 0 | } |
240 | ||
241 | } | |
242 | ||
243 | } // end MessagesImpl |