1
2
3
4 package org.rcfaces.core.converter;
5
6 import javax.faces.component.UIComponent;
7 import javax.faces.context.FacesContext;
8 import javax.faces.convert.ConverterException;
9
10
11
12
13
14
15 public class LongConverter extends AbstractNumberConverter {
16 private static final String REVISION = "$Revision: 1.1 $";
17
18 public LongConverter() {
19 setIntegerOnly(true);
20 }
21
22 public Object getAsObject(FacesContext context, UIComponent component,
23 String value) {
24
25 if (value == null || value.trim().length() < 1) {
26 Object v = getDefaultValue();
27 if (v instanceof Long) {
28 return v;
29 }
30 if (v instanceof Number) {
31 return new Long(((Number) v).longValue());
32 }
33 value = (String) v;
34 }
35
36 Number number = (Number) super.getAsObject(context, component, value);
37
38 if (number == null || (number instanceof Long)) {
39 return number;
40 }
41
42 double d = number.doubleValue();
43 if (d < Long.MIN_VALUE || d > Long.MAX_VALUE) {
44 throw new ConverterException(
45 "Number can not be converted to long. (value=" + d + ")");
46 }
47
48 return new Long(number.longValue());
49 }
50 }