001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.util.convert;
018
019import java.text.Format;
020import java.util.Locale;
021import java.util.Map;
022
023import org.apache.wicket.util.lang.Args;
024import org.apache.wicket.util.lang.Generics;
025
026
027/**
028 * Thrown for conversion exceptions.
029 * 
030 * @author Eelco Hillenius
031 */
032public class ConversionException extends RuntimeException
033{
034        private static final long serialVersionUID = 1L;
035
036        /** The converter that was used. */
037        private IConverter<?> converter;
038
039        /** Pattern that was used for conversion. */
040        private Format format;
041
042        /** Locale that was used for conversion. */
043        private Locale locale;
044
045        /** The value that was tried to convert. */
046        private Object sourceValue;
047
048        /** Target type for the failed conversion. */
049        private Class<?> targetType;
050
051        /** Resource key for the message that should be displayed */
052        private String resourceKey;
053
054        /** Variable map to use in variable substitution */
055        private Map<String, Object> vars;
056
057        /**
058         * Construct exception with message.
059         * 
060         * @param message
061         *            message
062         */
063        public ConversionException(final String message)
064        {
065                super(message);
066        }
067
068        /**
069         * Construct exception with message and cause.
070         * 
071         * @param message
072         *            message
073         * @param cause
074         *            cause
075         */
076        public ConversionException(final String message, final Throwable cause)
077        {
078                super(message, cause);
079        }
080
081        /**
082         * Construct exception with cause.
083         * 
084         * @param cause
085         *            cause
086         */
087        public ConversionException(final Throwable cause)
088        {
089                super(cause);
090        }
091
092        /**
093         * Gets the used converter.
094         * 
095         * @return the used converter.
096         */
097        public final IConverter<?> getConverter()
098        {
099                return converter;
100        }
101
102        /**
103         * Get the used format.
104         * 
105         * @return the used format
106         */
107        public final Format getFormat()
108        {
109                return format;
110        }
111
112        /**
113         * Get the used locale.
114         * 
115         * @return the used locale
116         */
117        public final Locale getLocale()
118        {
119                return locale;
120        }
121
122        /**
123         * Gets the tried value.
124         * 
125         * @return the tried value.
126         */
127        public final Object getSourceValue()
128        {
129                return sourceValue;
130        }
131
132        /**
133         * Gets the target property type.
134         * 
135         * @return the target property type.
136         */
137        public final Class<?> getTargetType()
138        {
139                return targetType;
140        }
141
142        /**
143         * Sets the used converter.
144         * 
145         * @param converter
146         *            the converter.
147         * @return This
148         */
149        public final ConversionException setConverter(final IConverter<?> converter)
150        {
151                this.converter = converter;
152                return this;
153        }
154
155        /**
156         * Sets the used format.
157         * 
158         * @param format
159         *            the used format.
160         * @return This
161         */
162        public final ConversionException setFormat(final Format format)
163        {
164                this.format = format;
165                return this;
166        }
167
168        /**
169         * Sets the used locale.
170         * 
171         * @param locale
172         *            the used locale.
173         * @return This
174         */
175        public final ConversionException setLocale(final Locale locale)
176        {
177                this.locale = locale;
178                return this;
179        }
180
181        /**
182         * Sets the tried value.
183         * 
184         * @param sourceValue
185         *            the tried value.
186         * @return This
187         */
188        public final ConversionException setSourceValue(final Object sourceValue)
189        {
190                this.sourceValue = sourceValue;
191                return this;
192        }
193
194        /**
195         * Sets the target property type.
196         * 
197         * @param targetType
198         *            sets the target property type
199         * @return This
200         */
201        public final ConversionException setTargetType(final Class<?> targetType)
202        {
203                this.targetType = targetType;
204                return this;
205        }
206
207
208        /**
209         * @return The resource key for the message that should be displayed
210         */
211        public String getResourceKey()
212        {
213                return resourceKey;
214        }
215
216
217        /**
218         * Set the resource key for the message that should be displayed.
219         * 
220         * @param resourceKey
221         *            sets the resource key
222         * @return This
223         */
224        public ConversionException setResourceKey(final String resourceKey)
225        {
226                this.resourceKey = resourceKey;
227                return this;
228        }
229
230        /**
231         * Sets a variable that will be used in substitution
232         * 
233         * @param name
234         *            variable name
235         * @param value
236         *            variable value
237         * @return this for chaining
238         */
239        public ConversionException setVariable(final String name, final Object value)
240        {
241                Args.notEmpty(name, "name");
242                Args.notNull(value, "value");
243
244                if (vars == null)
245                {
246                        vars = Generics.newHashMap(2);
247                }
248
249                vars.put(name, value);
250                return this;
251        }
252
253        /**
254         * Returns the map of variables for this exception.
255         * 
256         * @return map of variables for this exception (or null if no variables were defined)
257         */
258        public Map<String, Object> getVariables()
259        {
260                return vars;
261        }
262}