View Javadoc

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  	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  	protected int iCount = 0;
43  
44  	// --------------------------------------------------------- Public Methods
45  
46  	/**
47  	 * Create an empty <code>Messages</code> object.
48  	 */
49  	public MessagesImpl() {
50  		super();
51  	}
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  		super();
62  		this.add(messages);
63  	}
64  
65  
66  	// See interface for JavaDoc
67  	public void add(String property, Message message) {
68  
69  		MessageItem item = (MessageItem) messages.get(property);
70  		List list = null;
71  
72  		if (item == null) {
73  			list = new ArrayList();
74  			item = new MessageItem(list, iCount++);
75  
76  			messages.put(property, item);
77  		} else {
78  			list = item.getList();
79  		}
80  
81  		list.add(message);
82  
83  	}
84  
85  
86  	// See interface for JavaDoc
87  	public void add(Message message) {
88  		
89  		add(Messages.GLOBAL_MESSAGE_KEY,message);
90  	}
91  
92  
93  	// See interface for JavaDoc
94  	public void add(Messages messages) {
95  		// loop over properties
96  		Iterator props = messages.properties();
97  		while (props.hasNext()) {
98  			String property = (String) props.next();
99  
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