1 /*
2 * jDTAUS Banking SPI
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.banking.spi;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.LinkedList;
27 import java.util.List;
28 import java.util.Locale;
29 import java.util.Map;
30 import org.jdtaus.banking.Textschluessel;
31 import org.jdtaus.core.text.Message;
32 import org.jdtaus.core.text.Messages;
33
34 /**
35 * Gets thrown whenever an illegal Textschlüssel is passed to a method expecting a legal Textschlüssel.
36 * <p>Example: Throwing an {@code IllegalTextschluesselException}<br/>
37 * <blockquote><pre>
38 * IllegalTextschluesselException e = new IllegalTextschluesselException();
39 * e.addMessage(message);
40 * e.addMessage(Textschluessel.PROP_<i>XYZ</i>, message);
41 * throw e;</pre></blockquote></p>
42 *
43 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
44 * @version $JDTAUS: IllegalTextschluesselException.java 8661 2012-09-27 11:29:58Z schulte $
45 */
46 public class IllegalTextschluesselException extends org.jdtaus.banking.IllegalTextschluesselException
47 {
48
49 /** Serial version UID for backwards compatibility with 1.1.x classes. */
50 private static final long serialVersionUID = 3384133251965896939L;
51
52 /** Creates a new {@code IllegalTextschluesselException} instance. */
53 public IllegalTextschluesselException()
54 {
55 super();
56 }
57
58 /** Key to the list of messages not bound to any particular property. */
59 private static final String PROP_UNSPECIFIED = "org.jdtaus.banking.Textschluessel";
60
61 /**
62 * The messages describing the exception.
63 * @serial
64 */
65 private Map messages = new HashMap( 100 );
66
67 /**
68 * Adds a message to the instance.
69 *
70 * @param message the message to add to the instance.
71 *
72 * @throws NullPointerException if {@code message} is {@code null}.
73 */
74 public void addMessage( final Message message )
75 {
76 this.addMessage( PROP_UNSPECIFIED, message );
77 }
78
79 /**
80 * Adds messages to the instance.
81 *
82 * @param messages The messages to add to the instance.
83 *
84 * @throws NullPointerException if {@code messages} is {@code null}.
85 */
86 public final void addMessages( final Messages messages )
87 {
88 if ( messages == null )
89 {
90 throw new NullPointerException( "messages" );
91 }
92
93 for ( int i = messages.size() - 1; i >= 0; i-- )
94 {
95 this.addMessage( messages.getMessage( i ) );
96 }
97 }
98
99 /**
100 * Adds a message bound to a property to the instance.
101 *
102 * @param propertyName The name of a property {@code message} is bound to.
103 * @param message The message to add to the instance.
104 *
105 * @throws NullPointerException if either {@code message} or {@code propertyName} is {@code null}.
106 */
107 public void addMessage( final String propertyName, final Message message )
108 {
109 if ( propertyName == null )
110 {
111 throw new NullPointerException( "propertyName" );
112 }
113 if ( message == null )
114 {
115 throw new NullPointerException( "message" );
116 }
117
118 List msgs = (List) this.messages.get( propertyName );
119 if ( msgs == null )
120 {
121 msgs = new LinkedList();
122 this.messages.put( propertyName, msgs );
123 }
124
125 msgs.add( message );
126 }
127
128 /**
129 * Adds messages bound to a property to the instance.
130 *
131 * @param propertyName The name of a property {@code messages} are bound to.
132 * @param messages The messages to add to the instance.
133 *
134 * @throws NullPointerException if either {@code messages} or {@code propertyName} is {@code null}.
135 */
136 public final void addMessages( final String propertyName, final Messages messages )
137 {
138 if ( propertyName == null )
139 {
140 throw new NullPointerException( "propertyName" );
141 }
142 if ( messages == null )
143 {
144 throw new NullPointerException( "messages" );
145 }
146
147 for ( int i = messages.size() - 1; i >= 0; i-- )
148 {
149 this.addMessage( propertyName, messages.getMessage( i ) );
150 }
151 }
152
153 /**
154 * Gets all messages describing the exception.
155 *
156 * @return An array of messages describing the exception or an empty array if the instance does not hold any
157 * messages.
158 */
159 public Message[] getMessages()
160 {
161 final List col = new LinkedList();
162 for ( Iterator it = this.messages.keySet().iterator(); it.hasNext(); )
163 {
164 final String propertyName = (String) it.next();
165 col.addAll( (List) this.messages.get( propertyName ) );
166 }
167
168 return (Message[]) col.toArray( new Message[ col.size() ] );
169 }
170
171 /**
172 * Gets messages bound to a property removing these messages from the instance.
173 *
174 * @param propertyName The name of a property to return any messages for.
175 *
176 * @return All messages bound to a property with name {@code propertyName} or an empty array if the instance does
177 * not hold messages for a property with name {@code propertyName}.
178 *
179 * @throws NullPointerException if {@code propertyName} is {@code null}.
180 */
181 public Message[] getMessages( final String propertyName )
182 {
183 if ( propertyName == null )
184 {
185 throw new NullPointerException( "propertyName" );
186 }
187
188 final List msgs = (List) this.messages.remove( propertyName );
189 return msgs == null ? new Message[ 0 ] : (Message[]) msgs.toArray( new Message[ msgs.size() ] );
190 }
191
192 /**
193 * Gets the names of all properties for which the exception holds messages.
194 *
195 * @return An array of the names of all properties for which the exception holds messages or an empty array if the
196 * exception does not hold any message bound to a property.
197 */
198 public String[] getPropertyNames()
199 {
200 final List names = new ArrayList( this.messages.size() );
201 for ( Iterator it = this.messages.keySet().iterator(); it.hasNext(); )
202 {
203 final String name = (String) it.next();
204 if ( !PROP_UNSPECIFIED.equals( name ) )
205 {
206 names.add( name );
207 }
208 }
209
210 return (String[]) names.toArray( new String[ names.size() ] );
211 }
212
213 /**
214 * Creates a string representing the messages of the instance.
215 *
216 * @return A string representing the messages of the instance.
217 */
218 private String internalString()
219 {
220 final StringBuffer buf = new StringBuffer( 200 ).append( '{' );
221 final String[] propertyNames = this.getPropertyNames();
222 final List unspecifiedMsgs = (List) this.messages.get( PROP_UNSPECIFIED );
223
224 for ( int i = 0; i < propertyNames.length; i++ )
225 {
226 buf.append( propertyNames[i] ).append( "={" );
227
228 int j = 0;
229 final List msgs = (List) this.messages.get( propertyNames[i] );
230 for ( Iterator it = msgs.iterator(); it.hasNext(); j++ )
231 {
232 final Message msg = (Message) it.next();
233 buf.append( '[' ).append( j ).append( "]=" ).append( msg.getText( Locale.getDefault() ) );
234 if ( it.hasNext() )
235 {
236 buf.append( ", " );
237 }
238 }
239
240 buf.append( '}' );
241 if ( i + 1 < propertyNames.length )
242 {
243 buf.append( ", " );
244 }
245 }
246
247 if ( unspecifiedMsgs != null && !unspecifiedMsgs.isEmpty() )
248 {
249 if ( propertyNames.length > 0 )
250 {
251 buf.append( ", " );
252 }
253
254 buf.append( PROP_UNSPECIFIED ).append( "={" );
255
256 int i = 0;
257 for ( Iterator it = unspecifiedMsgs.iterator(); it.hasNext(); i++ )
258 {
259 final Message msg = (Message) it.next();
260 buf.append( "[" ).append( i ).append( "]=" ).append( msg.getText( Locale.getDefault() ) );
261 if ( it.hasNext() )
262 {
263 buf.append( ", " );
264 }
265 }
266
267 buf.append( '}' );
268 }
269
270 buf.append( '}' );
271 return buf.toString();
272 }
273
274 /**
275 * Returns a string representation of the object.
276 *
277 * @return A string representation of the object.
278 */
279 public String toString()
280 {
281 return super.toString() + '\n' + this.internalString();
282 }
283
284 }