001: /*
002: * Copyright 2001-2006 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.time;
017:
018: /**
019: * Exception thrown when attempting to set a field outside its supported range.
020: *
021: * @author Brian S O'Neill
022: * @since 1.1
023: */
024: public class IllegalFieldValueException extends
025: IllegalArgumentException {
026:
027: /** Serialization lock. */
028: private static final long serialVersionUID = 6305711765985447737L;
029:
030: /**
031: * Creates a message for the exception.
032: *
033: * @param fieldName the field name
034: * @param value the value rejected
035: * @param lowerBound the lower bound allowed
036: * @param upperBound the uppe bound allowed
037: * @param explain an explanation
038: * @return the message
039: */
040: private static String createMessage(String fieldName, Number value,
041: Number lowerBound, Number upperBound, String explain) {
042: StringBuffer buf = new StringBuffer().append("Value ").append(
043: value).append(" for ").append(fieldName).append(' ');
044:
045: if (lowerBound == null) {
046: if (upperBound == null) {
047: buf.append("is not supported");
048: } else {
049: buf.append("must not be larger than ").append(
050: upperBound);
051: }
052: } else if (upperBound == null) {
053: buf.append("must not be smaller than ").append(lowerBound);
054: } else {
055: buf.append("must be in the range [").append(lowerBound)
056: .append(',').append(upperBound).append(']');
057: }
058: if (explain != null) {
059: buf.append(": ").append(explain);
060: }
061:
062: return buf.toString();
063: }
064:
065: /**
066: * Creates a message for the exception.
067: *
068: * @param fieldName the field name
069: * @param value the value rejected
070: * @return the message
071: */
072: private static String createMessage(String fieldName, String value) {
073: StringBuffer buf = new StringBuffer().append("Value ");
074:
075: if (value == null) {
076: buf.append("null");
077: } else {
078: buf.append('"');
079: buf.append(value);
080: buf.append('"');
081: }
082:
083: buf.append(" for ").append(fieldName).append(' ').append(
084: "is not supported");
085:
086: return buf.toString();
087: }
088:
089: private final DateTimeFieldType iDateTimeFieldType;
090: private final DurationFieldType iDurationFieldType;
091: private final String iFieldName;
092: private final Number iNumberValue;
093: private final String iStringValue;
094: private final Number iLowerBound;
095: private final Number iUpperBound;
096: private String iMessage;
097:
098: /**
099: * Constructor.
100: *
101: * @param fieldType type of field being set
102: * @param value illegal value being set
103: * @param lowerBound lower legal field value, or null if not applicable
104: * @param upperBound upper legal field value, or null if not applicable
105: */
106: public IllegalFieldValueException(DateTimeFieldType fieldType,
107: Number value, Number lowerBound, Number upperBound) {
108: super (createMessage(fieldType.getName(), value, lowerBound,
109: upperBound, null));
110: iDateTimeFieldType = fieldType;
111: iDurationFieldType = null;
112: iFieldName = fieldType.getName();
113: iNumberValue = value;
114: iStringValue = null;
115: iLowerBound = lowerBound;
116: iUpperBound = upperBound;
117: iMessage = super .getMessage();
118: }
119:
120: /**
121: * Constructor.
122: *
123: * @param fieldType type of field being set
124: * @param value illegal value being set
125: * @param explain an explanation
126: * @since 1.5
127: */
128: public IllegalFieldValueException(DateTimeFieldType fieldType,
129: Number value, String explain) {
130: super (createMessage(fieldType.getName(), value, null, null,
131: explain));
132: iDateTimeFieldType = fieldType;
133: iDurationFieldType = null;
134: iFieldName = fieldType.getName();
135: iNumberValue = value;
136: iStringValue = null;
137: iLowerBound = null;
138: iUpperBound = null;
139: iMessage = super .getMessage();
140: }
141:
142: /**
143: * Constructor.
144: *
145: * @param fieldType type of field being set
146: * @param value illegal value being set
147: * @param lowerBound lower legal field value, or null if not applicable
148: * @param upperBound upper legal field value, or null if not applicable
149: */
150: public IllegalFieldValueException(DurationFieldType fieldType,
151: Number value, Number lowerBound, Number upperBound) {
152: super (createMessage(fieldType.getName(), value, lowerBound,
153: upperBound, null));
154: iDateTimeFieldType = null;
155: iDurationFieldType = fieldType;
156: iFieldName = fieldType.getName();
157: iNumberValue = value;
158: iStringValue = null;
159: iLowerBound = lowerBound;
160: iUpperBound = upperBound;
161: iMessage = super .getMessage();
162: }
163:
164: /**
165: * Constructor.
166: *
167: * @param fieldName name of field being set
168: * @param value illegal value being set
169: * @param lowerBound lower legal field value, or null if not applicable
170: * @param upperBound upper legal field value, or null if not applicable
171: */
172: public IllegalFieldValueException(String fieldName, Number value,
173: Number lowerBound, Number upperBound) {
174: super (createMessage(fieldName, value, lowerBound, upperBound,
175: null));
176: iDateTimeFieldType = null;
177: iDurationFieldType = null;
178: iFieldName = fieldName;
179: iNumberValue = value;
180: iStringValue = null;
181: iLowerBound = lowerBound;
182: iUpperBound = upperBound;
183: iMessage = super .getMessage();
184: }
185:
186: /**
187: * Constructor.
188: *
189: * @param fieldType type of field being set
190: * @param value illegal value being set
191: */
192: public IllegalFieldValueException(DateTimeFieldType fieldType,
193: String value) {
194: super (createMessage(fieldType.getName(), value));
195: iDateTimeFieldType = fieldType;
196: iDurationFieldType = null;
197: iFieldName = fieldType.getName();
198: iStringValue = value;
199: iNumberValue = null;
200: iLowerBound = null;
201: iUpperBound = null;
202: iMessage = super .getMessage();
203: }
204:
205: /**
206: * Constructor.
207: *
208: * @param fieldType type of field being set
209: * @param value illegal value being set
210: */
211: public IllegalFieldValueException(DurationFieldType fieldType,
212: String value) {
213: super (createMessage(fieldType.getName(), value));
214: iDateTimeFieldType = null;
215: iDurationFieldType = fieldType;
216: iFieldName = fieldType.getName();
217: iStringValue = value;
218: iNumberValue = null;
219: iLowerBound = null;
220: iUpperBound = null;
221: iMessage = super .getMessage();
222: }
223:
224: /**
225: * Constructor.
226: *
227: * @param fieldName name of field being set
228: * @param value illegal value being set
229: */
230: public IllegalFieldValueException(String fieldName, String value) {
231: super (createMessage(fieldName, value));
232: iDateTimeFieldType = null;
233: iDurationFieldType = null;
234: iFieldName = fieldName;
235: iStringValue = value;
236: iNumberValue = null;
237: iLowerBound = null;
238: iUpperBound = null;
239: iMessage = super .getMessage();
240: }
241:
242: //-----------------------------------------------------------------------
243: /**
244: * Returns the DateTimeFieldType whose value was invalid, or null if not applicable.
245: *
246: * @return the datetime field type
247: */
248: public DateTimeFieldType getDateTimeFieldType() {
249: return iDateTimeFieldType;
250: }
251:
252: /**
253: * Returns the DurationFieldType whose value was invalid, or null if not applicable.
254: *
255: * @return the duration field type
256: */
257: public DurationFieldType getDurationFieldType() {
258: return iDurationFieldType;
259: }
260:
261: /**
262: * Returns the name of the field whose value was invalid.
263: *
264: * @return the field name
265: */
266: public String getFieldName() {
267: return iFieldName;
268: }
269:
270: /**
271: * Returns the illegal integer value assigned to the field, or null if not applicable.
272: *
273: * @return the value
274: */
275: public Number getIllegalNumberValue() {
276: return iNumberValue;
277: }
278:
279: /**
280: * Returns the illegal string value assigned to the field, or null if not applicable.
281: *
282: * @return the value
283: */
284: public String getIllegalStringValue() {
285: return iStringValue;
286: }
287:
288: /**
289: * Returns the illegal value assigned to the field as a non-null string.
290: *
291: * @return the value
292: */
293: public String getIllegalValueAsString() {
294: String value = iStringValue;
295: if (value == null) {
296: value = String.valueOf(iNumberValue);
297: }
298: return value;
299: }
300:
301: /**
302: * Returns the lower bound of the legal value range, or null if not applicable.
303: *
304: * @return the lower bound
305: */
306: public Number getLowerBound() {
307: return iLowerBound;
308: }
309:
310: /**
311: * Returns the upper bound of the legal value range, or null if not applicable.
312: *
313: * @return the upper bound
314: */
315: public Number getUpperBound() {
316: return iUpperBound;
317: }
318:
319: public String getMessage() {
320: return iMessage;
321: }
322:
323: /**
324: * Provide additional detail by prepending a message to the existing message.
325: * A colon is separator is automatically inserted between the messages.
326: * @since 1.3
327: */
328: public void prependMessage(String message) {
329: if (iMessage == null) {
330: iMessage = message;
331: } else if (message != null) {
332: iMessage = message + ": " + iMessage;
333: }
334: }
335: }
|