View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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           * min possible value
34           */
35          private final Double minValue;
36  
37          /**
38           * max possible value
39           */
40          private final Double maxValue;
41  
42          /**
43           * display length, -1 means unknown
44           */
45          private final int length;
46  
47          /**
48           * do this type has a fractional part
49           */
50          private final boolean hasFractional;
51  
52          /**
53           * if this is a number
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; // strip off the .0 part after string conversion
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 }