001/*
002 *  jDTAUS Core API
003 *  Copyright (C) 2005 Christian Schulte
004 *  <cs@schulte.it>
005 *
006 *  This library is free software; you can redistribute it and/or
007 *  modify it under the terms of the GNU Lesser General Public
008 *  License as published by the Free Software Foundation; either
009 *  version 2.1 of the License, or any later version.
010 *
011 *  This library is distributed in the hope that it will be useful,
012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 *  Lesser General Public License for more details.
015 *
016 *  You should have received a copy of the GNU Lesser General Public
017 *  License along with this library; if not, write to the Free Software
018 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
019 *
020 */
021package org.jdtaus.core.text;
022
023import java.io.Serializable;
024import java.util.Collection;
025import java.util.LinkedList;
026
027/**
028 * Collection of messages.
029 *
030 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
031 * @version $JDTAUS: Messages.java 8743 2012-10-07 03:06:20Z schulte $
032 */
033public class Messages implements Cloneable, Serializable
034{
035    //--Constants---------------------------------------------------------------
036
037    /** Serial version UID for backwards compatibility with 1.0.x classes. */
038    private static final long serialVersionUID = 834125519895929330L;
039
040    //---------------------------------------------------------------Constants--
041    //--Messages----------------------------------------------------------------
042
043    /**
044     * Value of property {@code messages}.
045     * @serial
046     */
047    private Collection messages;
048
049    /** Creates a new {@code Messages} instance. */
050    public Messages()
051    {
052        super();
053    }
054
055    /**
056     * Getter for property {@code messages}.
057     *
058     * @return messages held by the collection.
059     */
060    public Message[] getMessages()
061    {
062        if ( this.messages == null )
063        {
064            this.messages = new LinkedList();
065        }
066
067        return ( Message[] ) this.messages.toArray(
068            new Message[ this.messages.size() ] );
069
070    }
071
072    /**
073     * Accessor to messages of a given type.
074     *
075     * @param type the type of the messages to return.
076     *
077     * @return a collection of messages of type {@code type}.
078     *
079     * @throws NullPointerException if {@code type} is {@code null}.
080     */
081    public final Messages getMessages( final Class type )
082    {
083        if ( type == null )
084        {
085            throw new NullPointerException( "type" );
086        }
087
088        final Message[] msgs = this.getMessages();
089        final Messages ret = new Messages();
090        for ( int i = msgs.length - 1; i >= 0; i-- )
091        {
092            if ( type.isAssignableFrom( msgs[i].getClass() ) )
093            {
094                ret.addMessage( msgs[i] );
095            }
096        }
097
098        return ret;
099    }
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}