001 /*
002 * Copyright 2001,2004 The Apache Software Foundation.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.apache.commons.scaffold.util;
018
019
020 import java.io.Serializable;
021 import java.util.ArrayList;
022 import java.util.Collections;
023 import java.util.Comparator;
024 import java.util.Iterator;
025 import java.util.HashMap;
026 import java.util.List;
027
028 // ------------------------------------------------------------------------ 78
029
030 public class MessagesImpl implements Serializable,Messages {
031
032 /**
033 * The accumulated set of <code>Message</code> objects (represented
034 * as an ArrayList) for each property, keyed by property name.
035 */
036 protected HashMap messages = new HashMap();
037
038 /**
039 * The current number of the property/key being added. This is used
040 * to maintain the order messages are added.
041 */
042 protected int iCount = 0;
043
044 // --------------------------------------------------------- Public Methods
045
046 /**
047 * Create an empty <code>Messages</code> object.
048 */
049 public MessagesImpl() {
050 super();
051 }
052
053 /**
054 * Create an <code>AMessages</code> object initialized with the given
055 * messages.
056 *
057 * @param messages The messages to be initially added to this object.
058 * @since Struts 1.1
059 */
060 public MessagesImpl(Messages messages) {
061 super();
062 this.add(messages);
063 }
064
065
066 // See interface for JavaDoc
067 public void add(String property, Message message) {
068
069 MessageItem item = (MessageItem) messages.get(property);
070 List list = null;
071
072 if (item == null) {
073 list = new ArrayList();
074 item = new MessageItem(list, iCount++);
075
076 messages.put(property, item);
077 } else {
078 list = item.getList();
079 }
080
081 list.add(message);
082
083 }
084
085
086 // See interface for JavaDoc
087 public void add(Message message) {
088
089 add(Messages.GLOBAL_MESSAGE_KEY,message);
090 }
091
092
093 // See interface for JavaDoc
094 public void add(Messages messages) {
095 // loop over properties
096 Iterator props = messages.properties();
097 while (props.hasNext()) {
098 String property = (String) props.next();
099
100 // loop over messages for each property
101 Iterator msgs = messages.get(property);
102 while (msgs.hasNext()) {
103 Message msg = (Message) msgs.next();
104 this.add(property, msg);
105 }
106
107 }
108 }
109
110 // See interface for JavaDoc
111 public void clear() {
112
113 messages.clear();
114
115 }
116
117 // See interface for JavaDoc
118 public boolean empty() {
119 return (this.isEmpty());
120 }
121
122 // See interface for JavaDoc
123 public boolean isEmpty(){
124 return (messages.isEmpty());
125 }
126
127 // See interface for JavaDoc
128 public Iterator get() {
129
130 if (messages.size() == 0) {
131 return (Collections.EMPTY_LIST.iterator());
132 }
133
134 ArrayList results = new ArrayList();
135 ArrayList actionItems = new ArrayList();
136
137 for (Iterator i = messages.values().iterator(); i.hasNext();) {
138 actionItems.add(i.next());
139 }
140
141 // Sort MessageItems based on the initial order the
142 // property/key was added to Messages.
143 Collections.sort(actionItems, new Comparator() {
144 public int compare(Object o1, Object o2) {
145 return ((MessageItem) o1).getOrder() - ((MessageItem) o2).getOrder();
146 }
147 });
148
149 for (Iterator i = actionItems.iterator(); i.hasNext();) {
150 MessageItem ami = (MessageItem) i.next();
151
152 for (Iterator messages = ami.getList().iterator(); messages.hasNext();) {
153 results.add(messages.next());
154 }
155 }
156
157 return (results.iterator());
158
159 }
160
161 // See interface for JavaDoc
162 public Iterator get(String property) {
163
164 MessageItem item = (MessageItem) messages.get(property);
165
166 if (item == null) {
167 return (Collections.EMPTY_LIST.iterator());
168 } else {
169 return (item.getList().iterator());
170 }
171
172 }
173
174 // See interface for JavaDoc
175 public Iterator properties() {
176
177 return (messages.keySet().iterator());
178
179 }
180
181 // See interface for JavaDoc
182 public int size() {
183
184 int total = 0;
185
186 for (Iterator i = messages.values().iterator(); i.hasNext();) {
187 MessageItem ami = (MessageItem) i.next();
188 total += ami.getList().size();
189 }
190
191 return (total);
192
193 }
194
195 // See interface for JavaDoc
196 public int size(String property) {
197
198 MessageItem ami = (MessageItem) messages.get(property);
199
200 if (ami == null)
201 return (0);
202 else
203 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 protected List list = null;
214
215 /**
216 * The position in the list of messages.
217 */
218 protected int iOrder = 0;
219
220 public MessageItem(List list, int iOrder) {
221 this.list = list;
222 this.iOrder = iOrder;
223 }
224
225 public List getList() {
226 return list;
227 }
228
229 public void setList(List list) {
230 this.list = list;
231 }
232
233 public int getOrder() {
234 return iOrder;
235 }
236
237 public void setOrder(int iOrder) {
238 this.iOrder = iOrder;
239 }
240
241 }
242
243 } // end MessagesImpl