001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
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: package de.odysseus.calyxo.base;
017:
018: import java.util.ArrayList;
019: import java.util.Collections;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.NoSuchElementException;
025:
026: import de.odysseus.calyxo.base.util.ListOrderedMap;
027:
028: /**
029: * This class collects messages. Messages may be added and retreived
030: * by specifying a (optional) key.
031: *
032: * @see de.odysseus.calyxo.base.Message
033: * @author Christoph Beck
034: */
035: public class Messages {
036: public static final String GLOBAL_KEY = null;
037:
038: private Map messagesMap = ListOrderedMap.decorate(new HashMap());
039:
040: /**
041: * Default constructor
042: */
043: public Messages() {
044: super ();
045: }
046:
047: /**
048: * Add message.
049: * @param key the key under which to add the message
050: * @param message the message to add
051: */
052: public void addMessage(String key, Message message) {
053: Object o = messagesMap.get(key);
054: if (o instanceof List) {
055: ((List) o).add(message);
056: } else if (o instanceof Message) {
057: List l = new ArrayList(4);
058: l.add(o);
059: l.add(message);
060: messagesMap.put(key, l);
061: } else { // o == null
062: messagesMap.put(key, message);
063: }
064: }
065:
066: /**
067: * Get all the keys.
068: * If global messages have been added, this Iterator will also
069: * contain <code>Messages.GLOBAL_KEY</code>.
070: */
071: public Iterator getKeys() {
072: return messagesMap.keySet().iterator();
073: }
074:
075: /**
076: * Answer true iff no messages have been added yet.
077: */
078: public boolean isEmpty() {
079: return messagesMap.isEmpty();
080: }
081:
082: /**
083: * Answer true iff there are message for the specified key
084: */
085: public boolean containsMessages(String key) {
086: return messagesMap.containsKey(key);
087: }
088:
089: /**
090: * Answer true iff there are global messages
091: * Same as <code>containsGlobalMessages(Messages.GLOBAL_KEY)</code>.
092: */
093: public boolean containsGlobalMessages() {
094: return containsMessages(GLOBAL_KEY);
095: }
096:
097: /**
098: * Add a global message.
099: * Same as <code>addMessage(Messages.GLOBAL_KEY, message)</code>.
100: * @param message message to add
101: */
102: public void addGlobalMessage(Message message) {
103: addMessage(GLOBAL_KEY, message);
104: }
105:
106: /**
107: * Get the first message added for the specified key
108: */
109: public Message getFirstMessage(String key) {
110: Object o = messagesMap.get(key);
111: if (o instanceof List) {
112: return (Message) ((List) o).get(0);
113: }
114: // o == null || o instanceof Message
115: return (Message) o;
116: }
117:
118: /**
119: * Get the first added global message
120: * Same as <code>getFirstMessage(Messages.GLOBAL_KEY)</code>.
121: */
122: public Message getFirstGlobalMessage() {
123: return getFirstMessage(GLOBAL_KEY);
124: }
125:
126: /**
127: * Get messages (instances of {@link Message}) which have been
128: * added with the specified key (in the order they have been added).
129: * @param key the message key
130: */
131: public Iterator getMessages(final String key) {
132: final Object o = messagesMap.get(key);
133: if (o instanceof List) {
134: return new Iterator() {
135: Iterator delegate = ((List) o).iterator();
136:
137: public boolean hasNext() {
138: return delegate.hasNext();
139: }
140:
141: public Object next() {
142: return delegate.next();
143: }
144:
145: public void remove() {
146: delegate.remove();
147: if (((List) o).isEmpty()) {
148: messagesMap.remove(key);
149: }
150: }
151: };
152: } else if (o instanceof Message) {
153: return new Iterator() {
154: boolean next = true;
155:
156: public boolean hasNext() {
157: return next;
158: }
159:
160: public Object next() {
161: if (next) {
162: next = false;
163: return o;
164: }
165: throw new NoSuchElementException(
166: "No more messages...");
167: }
168:
169: public void remove() {
170: messagesMap.remove(key);
171: }
172: };
173: } else { // o == null
174: return Collections.EMPTY_LIST.iterator();
175: }
176: }
177:
178: /**
179: * Get global messages (instances of {@link Message}).
180: * Same as <code>getMessages(Messages.GLOBAL_KEY)</code>.
181: */
182: public Iterator getGlobalMessages() {
183: return getMessages(GLOBAL_KEY);
184: }
185:
186: /**
187: * Get all messages (instances of {@link Message}) in the
188: * order they have been added.
189: */
190: public Iterator getAllMessages() {
191: final Iterator keys = messagesMap.keySet().iterator();
192: return new Iterator() {
193: Iterator delegate;
194:
195: public boolean hasNext() {
196: return delegate != null && delegate.hasNext()
197: || keys.hasNext();
198: }
199:
200: public Object next() {
201: if (delegate == null || !delegate.hasNext()) {
202: delegate = getMessages((String) keys.next());
203: }
204: return delegate.next();
205: }
206:
207: public void remove() {
208: delegate.remove();
209: }
210: };
211: }
212: }
|