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: * Float converter.
22: *
23: * @author Christoph Beck
24: * @author Oliver Stuhr
25: */
26: public class FloatConverter extends NumberConverter {
27:
28: /**
29: * Default constructor.
30: */
31: public FloatConverter() {
32: super ();
33: }
34:
35: public Float getDefault() {
36: return (Float) getParsedNullOrEmpty();
37: }
38:
39: public void setDefault(Float value) {
40: setParsedNullOrEmpty(value);
41: }
42:
43: /**
44: * Format method.
45: */
46: public String format(Object value) {
47: return super .format((Float) value);
48: }
49:
50: /**
51: * Parse method.
52: */
53: public Object parse(String value) throws ParseException {
54: Number n = (Number) super .parse(value);
55: if (n == null || n instanceof Float)
56: return n;
57: double d = n.doubleValue();
58: if (d == Double.NEGATIVE_INFINITY)
59: return new Float(Float.NEGATIVE_INFINITY);
60: if (d == Double.POSITIVE_INFINITY)
61: return new Float(Float.POSITIVE_INFINITY);
62: if (d < -Float.MAX_VALUE || d > Float.MAX_VALUE)
63: throw new ParseException("", 0);
64: return new Float(n.floatValue());
65: }
66: }
|