1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.orchestra.dynaForm.metadata.utils;
20
21 import java.math.BigDecimal;
22 import java.math.BigInteger;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 public class TypeInfos
27 {
28 private final static Map<Class<?>, Info> INFOS = new HashMap<Class<?>, Info>(10);
29
30 public static class Info
31 {
32
33
34
35 private final Double minValue;
36
37
38
39
40 private final Double maxValue;
41
42
43
44
45 private final int length;
46
47
48
49
50 private final boolean hasFractional;
51
52
53
54
55 private final boolean number;
56
57 private Info(boolean number, Double minValue, Double maxValue, boolean hasFractional)
58 {
59 if (minValue != null)
60 {
61 int length = String.valueOf(minValue).length();
62 if (!hasFractional)
63 {
64 length = length - 2;
65 }
66 this.length = length;
67 }
68 else
69 {
70 length = -1;
71 }
72 this.minValue = minValue;
73 this.maxValue = maxValue;
74 this.hasFractional = hasFractional;
75 this.number = number;
76 }
77
78 public int getLength()
79 {
80 return length;
81 }
82
83 public Double getMaxValue()
84 {
85 return maxValue;
86 }
87
88 public Double getMinValue()
89 {
90 return minValue;
91 }
92
93 public boolean isHasFractional()
94 {
95 return hasFractional;
96 }
97
98 public boolean isNumber()
99 {
100 return number;
101 }
102 }
103
104 static
105 {
106 addInfo(new Info(true, (double) Byte.MIN_VALUE, (double) Byte.MAX_VALUE, false),
107 Byte.class, Byte.TYPE);
108 addInfo(new Info(true, (double) Short.MIN_VALUE, (double) Short.MAX_VALUE, false),
109 Short.class, Short.TYPE);
110 addInfo(new Info(true, (double) Integer.MIN_VALUE, (double) Integer.MAX_VALUE, false),
111 Integer.class, Integer.TYPE);
112 addInfo(new Info(true, (double) Long.MIN_VALUE, (double) Long.MAX_VALUE, false),
113 Long.class, Long.TYPE);
114 addInfo(new Info(true, null, null, false), BigInteger.class);
115 addInfo(new Info(true, null, (double) Float.MAX_VALUE, true), Float.class, Float.TYPE);
116 addInfo(new Info(true, null, Double.MAX_VALUE, true), Double.class, Double.TYPE);
117 addInfo(new Info(true, null, null, true), BigDecimal.class);
118 addInfo(new Info(false, null, null, false), String.class);
119 }
120
121 private TypeInfos()
122 {
123 }
124
125 private static void addInfo(Info info, Class<?>... types)
126 {
127 for (Class<?> type : types)
128 {
129 INFOS.put(type, info);
130 }
131 }
132
133 public static Info getInfo(Class<?> type)
134 {
135 return INFOS.get(type);
136 }
137 }