01: /*
02: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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 de.odysseus.calyxo.forms.convert;
17:
18: import java.text.ParseException;
19:
20: /**
21: * Integer converter.
22: *
23: * @author Christoph Beck
24: * @author Oliver Stuhr
25: */
26: public class IntegerConverter extends NumberConverter {
27: /**
28: * Default constructor.
29: */
30: public IntegerConverter() {
31: super ();
32:
33: setParseIntegerOnly(true);
34: }
35:
36: public Integer getDefault() {
37: return (Integer) getParsedNullOrEmpty();
38: }
39:
40: public void setDefault(Integer value) {
41: setParsedNullOrEmpty(value);
42: }
43:
44: /**
45: * Format method.
46: */
47: public String format(Object value) {
48: return super .format((Integer) value);
49: }
50:
51: /**
52: * Parse method.
53: */
54: public Object parse(String value) throws ParseException {
55: Number d = (Number) super .parse(value);
56: if (d == null || d instanceof Integer)
57: return d;
58: long l = d.longValue();
59: if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE)
60: throw new ParseException("", 0);
61: return new Integer(d.intValue());
62: }
63: }
|