KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > IllegalFieldValueException


1 /*
2  * Copyright 2001-2006 Stephen Colebourne
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.joda.time;
17
18 /**
19  * Exception thrown when attempting to set a field outside its supported range.
20  *
21  * @author Brian S O'Neill
22  * @since 1.1
23  */

24 public class IllegalFieldValueException extends IllegalArgumentException JavaDoc {
25     
26     /** Serialization lock. */
27     private static final long serialVersionUID = 6305711765985447737L;
28
29     /**
30      * Creates a message for the exception.
31      *
32      * @param fieldName the field name
33      * @param value the value rejected
34      * @param lowerBound the lower bound allowed
35      * @param upperBound the uppe bound allowed
36      * @return the message
37      */

38     private static String JavaDoc createMessage(String JavaDoc fieldName, Number JavaDoc value,
39                                         Number JavaDoc lowerBound, Number JavaDoc upperBound) {
40         StringBuffer JavaDoc buf = new StringBuffer JavaDoc()
41             .append("Value ").append(value).append(" for ").append(fieldName).append(' ');
42
43         if (lowerBound == null) {
44             if (upperBound == null) {
45                 buf.append("is not supported");
46             } else {
47                 buf.append("must not be larger than ").append(upperBound);
48             }
49         } else if (upperBound == null) {
50             buf.append("must not be smaller than ").append(lowerBound);
51         } else {
52             buf.append("must be in the range [")
53                 .append(lowerBound)
54                 .append(',')
55                 .append(upperBound)
56                 .append(']');
57         }
58
59         return buf.toString();
60     }
61
62     /**
63      * Creates a message for the exception.
64      *
65      * @param fieldName the field name
66      * @param value the value rejected
67      * @return the message
68      */

69     private static String JavaDoc createMessage(String JavaDoc fieldName, String JavaDoc value) {
70         StringBuffer JavaDoc buf = new StringBuffer JavaDoc().append("Value ");
71
72         if (value == null) {
73             buf.append("null");
74         } else {
75             buf.append('"');
76             buf.append(value);
77             buf.append('"');
78         }
79
80         buf.append(" for ").append(fieldName).append(' ').append("is not supported");
81         
82         return buf.toString();
83     }
84
85     private final DateTimeFieldType iDateTimeFieldType;
86     private final DurationFieldType iDurationFieldType;
87     private final String JavaDoc iFieldName;
88     private final Number JavaDoc iNumberValue;
89     private final String JavaDoc iStringValue;
90     private final Number JavaDoc iLowerBound;
91     private final Number JavaDoc iUpperBound;
92     private String JavaDoc iMessage;
93
94     /**
95      * Constructor.
96      *
97      * @param fieldType type of field being set
98      * @param value illegal value being set
99      * @param lowerBound lower legal field value, or null if not applicable
100      * @param upperBound upper legal field value, or null if not applicable
101      */

102     public IllegalFieldValueException(DateTimeFieldType fieldType,
103                                       Number JavaDoc value, Number JavaDoc lowerBound, Number JavaDoc upperBound) {
104         super(createMessage(fieldType.getName(), value, lowerBound, upperBound));
105         iDateTimeFieldType = fieldType;
106         iDurationFieldType = null;
107         iFieldName = fieldType.getName();
108         iNumberValue = value;
109         iStringValue = null;
110         iLowerBound = lowerBound;
111         iUpperBound = upperBound;
112         iMessage = super.getMessage();
113     }
114
115     /**
116      * Constructor.
117      *
118      * @param fieldType type of field being set
119      * @param value illegal value being set
120      * @param lowerBound lower legal field value, or null if not applicable
121      * @param upperBound upper legal field value, or null if not applicable
122      */

123     public IllegalFieldValueException(DurationFieldType fieldType,
124                                       Number JavaDoc value, Number JavaDoc lowerBound, Number JavaDoc upperBound) {
125         super(createMessage(fieldType.getName(), value, lowerBound, upperBound));
126         iDateTimeFieldType = null;
127         iDurationFieldType = fieldType;
128         iFieldName = fieldType.getName();
129         iNumberValue = value;
130         iStringValue = null;
131         iLowerBound = lowerBound;
132         iUpperBound = upperBound;
133         iMessage = super.getMessage();
134     }
135
136     /**
137      * Constructor.
138      *
139      * @param fieldName name of field being set
140      * @param value illegal value being set
141      * @param lowerBound lower legal field value, or null if not applicable
142      * @param upperBound upper legal field value, or null if not applicable
143      */

144     public IllegalFieldValueException(String JavaDoc fieldName,
145                                       Number JavaDoc value, Number JavaDoc lowerBound, Number JavaDoc upperBound) {
146         super(createMessage(fieldName, value, lowerBound, upperBound));
147         iDateTimeFieldType = null;
148         iDurationFieldType = null;
149         iFieldName = fieldName;
150         iNumberValue = value;
151         iStringValue = null;
152         iLowerBound = lowerBound;
153         iUpperBound = upperBound;
154         iMessage = super.getMessage();
155     }
156
157     /**
158      * Constructor.
159      *
160      * @param fieldType type of field being set
161      * @param value illegal value being set
162      */

163     public IllegalFieldValueException(DateTimeFieldType fieldType, String JavaDoc value) {
164         super(createMessage(fieldType.getName(), value));
165         iDateTimeFieldType = fieldType;
166         iDurationFieldType = null;
167         iFieldName = fieldType.getName();
168         iStringValue = value;
169         iNumberValue = null;
170         iLowerBound = null;
171         iUpperBound = null;
172         iMessage = super.getMessage();
173     }
174
175     /**
176      * Constructor.
177      *
178      * @param fieldType type of field being set
179      * @param value illegal value being set
180      */

181     public IllegalFieldValueException(DurationFieldType fieldType, String JavaDoc value) {
182         super(createMessage(fieldType.getName(), value));
183         iDateTimeFieldType = null;
184         iDurationFieldType = fieldType;
185         iFieldName = fieldType.getName();
186         iStringValue = value;
187         iNumberValue = null;
188         iLowerBound = null;
189         iUpperBound = null;
190         iMessage = super.getMessage();
191     }
192
193     /**
194      * Constructor.
195      *
196      * @param fieldName name of field being set
197      * @param value illegal value being set
198      */

199     public IllegalFieldValueException(String JavaDoc fieldName, String JavaDoc value) {
200         super(createMessage(fieldName, value));
201         iDateTimeFieldType = null;
202         iDurationFieldType = null;
203         iFieldName = fieldName;
204         iStringValue = value;
205         iNumberValue = null;
206         iLowerBound = null;
207         iUpperBound = null;
208         iMessage = super.getMessage();
209     }
210
211     //-----------------------------------------------------------------------
212
/**
213      * Returns the DateTimeFieldType whose value was invalid, or null if not applicable.
214      *
215      * @return the datetime field type
216      */

217     public DateTimeFieldType getDateTimeFieldType() {
218         return iDateTimeFieldType;
219     }
220
221     /**
222      * Returns the DurationFieldType whose value was invalid, or null if not applicable.
223      *
224      * @return the duration field type
225      */

226     public DurationFieldType getDurationFieldType() {
227         return iDurationFieldType;
228     }
229
230     /**
231      * Returns the name of the field whose value was invalid.
232      *
233      * @return the field name
234      */

235     public String JavaDoc getFieldName() {
236         return iFieldName;
237     }
238
239     /**
240      * Returns the illegal integer value assigned to the field, or null if not applicable.
241      *
242      * @return the value
243      */

244     public Number JavaDoc getIllegalNumberValue() {
245         return iNumberValue;
246     }
247
248     /**
249      * Returns the illegal string value assigned to the field, or null if not applicable.
250      *
251      * @return the value
252      */

253     public String JavaDoc getIllegalStringValue() {
254         return iStringValue;
255     }
256
257     /**
258      * Returns the illegal value assigned to the field as a non-null string.
259      *
260      * @return the value
261      */

262     public String JavaDoc getIllegalValueAsString() {
263         String JavaDoc value = iStringValue;
264         if (value == null) {
265             value = String.valueOf(iNumberValue);
266         }
267         return value;
268     }
269
270     /**
271      * Returns the lower bound of the legal value range, or null if not applicable.
272      *
273      * @return the lower bound
274      */

275     public Number JavaDoc getLowerBound() {
276         return iLowerBound;
277     }
278
279     /**
280      * Returns the upper bound of the legal value range, or null if not applicable.
281      *
282      * @return the upper bound
283      */

284     public Number JavaDoc getUpperBound() {
285         return iUpperBound;
286     }
287
288     public String JavaDoc getMessage() {
289         return iMessage;
290     }
291
292     /**
293      * Provide additional detail by prepending a message to the existing message.
294      * A colon is separator is automatically inserted between the messages.
295      * @since 1.3
296      */

297     public void prependMessage(String JavaDoc message) {
298         if (iMessage == null) {
299             iMessage = message;
300         } else if (message != null) {
301             iMessage = message + ": " + iMessage;
302         }
303     }
304 }
305
Popular Tags