| 1 | /* |
| 2 | * jDTAUS Core API |
| 3 | * Copyright (C) 2005 Christian Schulte |
| 4 | * <cs@schulte.it> |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | * |
| 20 | */ |
| 21 | package org.jdtaus.core.text; |
| 22 | |
| 23 | import java.io.Serializable; |
| 24 | import java.util.Collection; |
| 25 | import java.util.LinkedList; |
| 26 | |
| 27 | /** |
| 28 | * Collection of messages. |
| 29 | * |
| 30 | * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> |
| 31 | * @version $JDTAUS: Messages.java 8743 2012-10-07 03:06:20Z schulte $ |
| 32 | */ |
| 33 | public class Messages implements Cloneable, Serializable |
| 34 | { |
| 35 | //--Constants--------------------------------------------------------------- |
| 36 | |
| 37 | /** Serial version UID for backwards compatibility with 1.0.x classes. */ |
| 38 | private static final long serialVersionUID = 834125519895929330L; |
| 39 | |
| 40 | //---------------------------------------------------------------Constants-- |
| 41 | //--Messages---------------------------------------------------------------- |
| 42 | |
| 43 | /** |
| 44 | * Value of property {@code messages}. |
| 45 | * @serial |
| 46 | */ |
| 47 | private Collection messages; |
| 48 | |
| 49 | /** Creates a new {@code Messages} instance. */ |
| 50 | public Messages() |
| 51 | { |
| 52 | super(); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Getter for property {@code messages}. |
| 57 | * |
| 58 | * @return messages held by the collection. |
| 59 | */ |
| 60 | public Message[] getMessages() |
| 61 | { |
| 62 | if ( this.messages == null ) |
| 63 | { |
| 64 | this.messages = new LinkedList(); |
| 65 | } |
| 66 | |
| 67 | return ( Message[] ) this.messages.toArray( |
| 68 | new Message[ this.messages.size() ] ); |
| 69 | |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Accessor to messages of a given type. |
| 74 | * |
| 75 | * @param type the type of the messages to return. |
| 76 | * |
| 77 | * @return a collection of messages of type {@code type}. |
| 78 | * |
| 79 | * @throws NullPointerException if {@code type} is {@code null}. |
| 80 | */ |
| 81 | public final Messages getMessages( final Class type ) |
| 82 | { |
| 83 | if ( type == null ) |
| 84 | { |
| 85 | throw new NullPointerException( "type" ); |
| 86 | } |
| 87 | |
| 88 | final Message[] msgs = this.getMessages(); |
| 89 | final Messages ret = new Messages(); |
| 90 | for ( int i = msgs.length - 1; i >= 0; i-- ) |
| 91 | { |
| 92 | if ( type.isAssignableFrom( msgs[i].getClass() ) ) |
| 93 | { |
| 94 | ret.addMessage( msgs[i] ); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Accessor to an indexed message. |
| 103 | * |
| 104 | * @param index the index of the message to return. |
| 105 | * |
| 106 | * @return a reference to the message at {@code index}. |
| 107 | * |
| 108 | * @throws IndexOutOfBoundsException if {@code index} is negativ, |
| 109 | * greater than or equal to {@code size()}. |
| 110 | */ |
| 111 | public final Message getMessage( final int index ) |
| 112 | { |
| 113 | if ( index < 0 || index >= this.size() ) |
| 114 | { |
| 115 | throw new ArrayIndexOutOfBoundsException( index ); |
| 116 | } |
| 117 | |
| 118 | return this.getMessages()[index]; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Adds a message to the collection. |
| 123 | * |
| 124 | * @param message the message to add to the collection. |
| 125 | * |
| 126 | * @throws NullPointerException if {@code message} is {@code null}. |
| 127 | */ |
| 128 | public void addMessage( final Message message ) |
| 129 | { |
| 130 | if ( message == null ) |
| 131 | { |
| 132 | throw new NullPointerException( "message" ); |
| 133 | } |
| 134 | |
| 135 | if ( this.messages == null ) |
| 136 | { |
| 137 | this.messages = new LinkedList(); |
| 138 | } |
| 139 | |
| 140 | this.messages.add( message ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Adds messages to the collection. |
| 145 | * |
| 146 | * @param messages collection of messages to add to the collection. |
| 147 | * |
| 148 | * @throws NullPointerException if {@code messages} is {@code null}. |
| 149 | */ |
| 150 | public final void addMessages( final Messages messages ) |
| 151 | { |
| 152 | if ( messages == null ) |
| 153 | { |
| 154 | throw new NullPointerException( "messages" ); |
| 155 | } |
| 156 | |
| 157 | final Message[] msgs = messages.getMessages(); |
| 158 | for ( int i = msgs.length - 1; i >= 0; i-- ) |
| 159 | { |
| 160 | this.addMessage( msgs[i] ); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Adds an array of messages to the collection. |
| 166 | * |
| 167 | * @param messages array of messages to add to the collection. |
| 168 | * |
| 169 | * @throws NullPointerException if {@code messages} is {@code null} or |
| 170 | * contains {@code null} elements. |
| 171 | */ |
| 172 | public final void addMessages( final Message[] messages ) |
| 173 | { |
| 174 | if ( messages == null ) |
| 175 | { |
| 176 | throw new NullPointerException( "messages" ); |
| 177 | } |
| 178 | |
| 179 | for ( int i = messages.length - 1; i >= 0; i-- ) |
| 180 | { |
| 181 | this.addMessage( messages[i] ); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Removes a message from the collection. |
| 187 | * |
| 188 | * @param message the message to remove from the collection. |
| 189 | * |
| 190 | * @throws NullPointerException if {@code message} is {@code null}. |
| 191 | */ |
| 192 | public void removeMessage( final Message message ) |
| 193 | { |
| 194 | if ( message == null ) |
| 195 | { |
| 196 | throw new NullPointerException( "message" ); |
| 197 | } |
| 198 | |
| 199 | if ( this.messages == null ) |
| 200 | { |
| 201 | this.messages = new LinkedList(); |
| 202 | } |
| 203 | |
| 204 | this.messages.remove( message ); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Removes messages from the collection. |
| 209 | * |
| 210 | * @param messages collection of messages to remove from the collection. |
| 211 | * |
| 212 | * @throws NullPointerException if {@code messages} is {@code null}. |
| 213 | */ |
| 214 | public final void removeMessages( final Messages messages ) |
| 215 | { |
| 216 | if ( messages == null ) |
| 217 | { |
| 218 | throw new NullPointerException( "messages" ); |
| 219 | } |
| 220 | |
| 221 | final Message[] msgs = messages.getMessages(); |
| 222 | for ( int i = msgs.length - 1; i >= 0; i-- ) |
| 223 | { |
| 224 | this.removeMessage( msgs[i] ); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Removes messages of a given type. |
| 230 | * |
| 231 | * @param type the type of the messages to remove. |
| 232 | * |
| 233 | * @return the collection of messages of type {@code type} removed from the |
| 234 | * collection. |
| 235 | * |
| 236 | * @throws NullPointerException if {@code type} is {@code null}. |
| 237 | */ |
| 238 | public final Messages removeMessages( final Class type ) |
| 239 | { |
| 240 | if ( type == null ) |
| 241 | { |
| 242 | throw new NullPointerException( "type" ); |
| 243 | } |
| 244 | |
| 245 | final Messages removed = this.getMessages( type ); |
| 246 | this.removeMessages( removed ); |
| 247 | return removed; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Getter for property {@code size}. |
| 252 | * |
| 253 | * @return the number of elements in this collection. |
| 254 | */ |
| 255 | public final int size() |
| 256 | { |
| 257 | return this.getMessages().length; |
| 258 | } |
| 259 | |
| 260 | /** Removes all messages from the collection. */ |
| 261 | public void clear() |
| 262 | { |
| 263 | if ( this.messages == null ) |
| 264 | { |
| 265 | this.messages = new LinkedList(); |
| 266 | } |
| 267 | |
| 268 | this.messages.clear(); |
| 269 | } |
| 270 | |
| 271 | //----------------------------------------------------------------Messages-- |
| 272 | //--Object------------------------------------------------------------------ |
| 273 | |
| 274 | /** |
| 275 | * Creates and returns a deep copy of this object. |
| 276 | * |
| 277 | * @return a clone of this instance. |
| 278 | */ |
| 279 | public Object clone() |
| 280 | { |
| 281 | try |
| 282 | { |
| 283 | final Message[] msgs = this.getMessages(); |
| 284 | final Messages ret = ( Messages ) super.clone(); |
| 285 | |
| 286 | ret.clear(); |
| 287 | for ( int i = msgs.length - 1; i >= 0; i-- ) |
| 288 | { |
| 289 | ret.addMessage( ( Message ) msgs[i].clone() ); |
| 290 | } |
| 291 | |
| 292 | return ret; |
| 293 | } |
| 294 | catch ( final CloneNotSupportedException e ) |
| 295 | { |
| 296 | throw new AssertionError( e ); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | //------------------------------------------------------------------Object-- |
| 301 | } |