001 /*
002 * jDTAUS - DTAUS fileformat.
003 * Copyright (c) 2005 Christian Schulte <cs@schulte.it>
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either
008 * version 2.1 of the License, or any later version.
009 *
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013 * Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public
016 * License along with this library; if not, write to the Free Software
017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
018 *
019 */
020 package org.jdtaus.core.text;
021
022 import java.io.Serializable;
023 import java.util.Collection;
024 import java.util.LinkedList;
025
026 /**
027 * Collection of messages.
028 *
029 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
030 * @version $Id: Messages.java 1914 2007-03-01 02:20:44Z schulte2005 $
031 */
032 public class Messages implements Cloneable, Serializable
033 {
034
035 //--Messages----------------------------------------------------------------
036
037 /**
038 * Value of property {@code messages}.
039 * @serial
040 */
041 private Collection messages;
042
043 /**
044 * Getter for property {@code messages}.
045 *
046 * @return messages held by the collection.
047 */
048 public Message[] getMessages()
049 {
050 if(this.messages == null)
051 {
052 this.messages = new LinkedList();
053 }
054
055 return (Message[]) this.messages.
056 toArray(new Message[this.messages.size()]);
057
058 }
059
060 /**
061 * Accessor to messages of a given type.
062 *
063 * @param type the type of the messages to return.
064 *
065 * @return a collection of messages of type {@code type}.
066 *
067 * @throws NullPointerException if {@code type} is {@code null}.
068 */
069 public final Messages getMessages(final Class type)
070 {
071 if(type == null)
072 {
073 throw new NullPointerException("type");
074 }
075
076 final Message[] msgs = this.getMessages();
077 final Messages ret = new Messages();
078 for(int i = msgs.length - 1; i >= 0; i--)
079 {
080 if(type.isAssignableFrom(msgs[i].getClass()))
081 {
082 ret.addMessage(msgs[i]);
083 }
084 }
085
086 return ret;
087 }
088
089 /**
090 * Accessor to an indexed message.
091 *
092 * @param index the index of the message to return.
093 *
094 * @return a reference to the message at {@code index}.
095 *
096 * @throws IndexOutOfBoundsException if {@code index} is negativ,
097 * greater than or equal to {@code size()}.
098 */
099 public final Message getMessage(final int index)
100 {
101 if(index < 0 || index >= this.size())
102 {
103 throw new ArrayIndexOutOfBoundsException(index);
104 }
105
106 return this.getMessages()[index];
107 }
108
109 /**
110 * Adds a message to the collection.
111 *
112 * @param message the message to add to the collection.
113 *
114 * @throws NullPointerException if {@code message} is {@code null}.
115 */
116 public void addMessage(final Message message)
117 {
118 if(message == null)
119 {
120 throw new NullPointerException("message");
121 }
122
123 if(this.messages == null)
124 {
125 this.messages = new LinkedList();
126 }
127
128 this.messages.add(message);
129 }
130
131 /**
132 * Adds messages to the collection.
133 *
134 * @param messages collection of messages to add to the collection.
135 *
136 * @throws NullPointerException if {@code messages} is {@code null}.
137 */
138 public final void addMessages(final Messages messages)
139 {
140 if(messages == null)
141 {
142 throw new NullPointerException("messages");
143 }
144
145 final Message[] msgs = messages.getMessages();
146 for(int i = msgs.length - 1; i >= 0; i--)
147 {
148 this.addMessage(msgs[i]);
149 }
150 }
151
152 /**
153 * Removes a message from the collection.
154 *
155 * @param message the message to remove from the collection.
156 *
157 * @throws NullPointerException if {@code message} is {@code null}.
158 */
159 public void removeMessage(final Message message)
160 {
161 if(message == null)
162 {
163 throw new NullPointerException("message");
164 }
165
166 if(this.messages == null)
167 {
168 this.messages = new LinkedList();
169 }
170
171 this.messages.remove(message);
172 }
173
174 /**
175 * Removes messages from the collection.
176 *
177 * @param messages collection of messages to remove from the collection.
178 *
179 * @throws NullPointerException if {@code messages} is {@code null}.
180 */
181 public final void removeMessages(final Messages messages)
182 {
183 if(messages == null)
184 {
185 throw new NullPointerException("messages");
186 }
187
188 final Message[] msgs = messages.getMessages();
189 for(int i = msgs.length - 1; i >= 0; i--)
190 {
191 this.removeMessage(msgs[i]);
192 }
193 }
194
195 /**
196 * Removes messages of a given type.
197 *
198 * @param type the type of the messages to remove.
199 *
200 * @return the collection of messages of type {@code type} removed from the
201 * collection.
202 *
203 * @throws NullPointerException if {@code type} is {@code null}.
204 */
205 public final Messages removeMessages(final Class type)
206 {
207 if(type == null)
208 {
209 throw new NullPointerException("type");
210 }
211
212 final Messages removed = this.getMessages(type);
213 this.removeMessages(removed);
214 return removed;
215 }
216
217 /**
218 * Getter for property {@code size}.
219 *
220 * @return the number of elements in this collection.
221 */
222 public final int size()
223 {
224 return this.getMessages().length;
225 }
226
227 /** Removes all messages from the collection. */
228 public void clear()
229 {
230 if(this.messages == null)
231 {
232 this.messages = new LinkedList();
233 }
234
235 this.messages.clear();
236 }
237
238 //----------------------------------------------------------------Messages--
239 //--Object------------------------------------------------------------------
240
241 /**
242 * Creates and returns a deep copy of this object.
243 *
244 * @return a clone of this instance.
245 */
246 public Object clone()
247 {
248 try
249 {
250 final Message[] msgs = this.getMessages();
251 final Messages ret = (Messages) super.clone();
252
253 ret.clear();
254 for(int i = msgs.length - 1; i >= 0; i--)
255 {
256 ret.addMessage((Message) msgs[i].clone());
257 }
258
259 return ret;
260 }
261 catch(CloneNotSupportedException e)
262 {
263 throw new AssertionError(e);
264 }
265 }
266
267 //------------------------------------------------------------------Object--
268
269 }