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