View Javadoc

1   /*
2    * $Id: NumberFormatTypeConverter.java,v 1.1 2006/11/09 19:09:09 oeuillot Exp $
3    */
4   package org.rcfaces.core.internal.converter;
5   
6   import java.util.HashMap;
7   import java.util.Iterator;
8   import java.util.Map;
9   
10  import javax.faces.component.UIComponent;
11  import javax.faces.context.FacesContext;
12  import javax.faces.convert.Converter;
13  
14  import org.rcfaces.core.component.capability.INumberFormatTypeCapability;
15  import org.rcfaces.core.model.AbstractConverter;
16  
17  /**
18   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
19   * @version $Revision: 1.1 $ $Date: 2006/11/09 19:09:09 $
20   */
21  public class NumberFormatTypeConverter extends AbstractConverter {
22      private static final String REVISION = "$Revision: 1.1 $";
23  
24      public static final Converter SINGLETON = new NumberFormatTypeConverter();
25  
26      private static final Integer DEFAULT_NUMBER_FORMAT_TYPE = new Integer(
27              INumberFormatTypeCapability.NUMBER_FORMAT_TYPE);
28  
29      private static Map NUMBER_FORMAT_TYPES = new HashMap(5);
30      static {
31          NUMBER_FORMAT_TYPES.put(
32                  INumberFormatTypeCapability.NUMBER_FORMAT_TYPE_NAME,
33                  new Integer(INumberFormatTypeCapability.NUMBER_FORMAT_TYPE));
34          NUMBER_FORMAT_TYPES.put(
35                  INumberFormatTypeCapability.INTEGER_FORMAT_TYPE_NAME,
36                  new Integer(INumberFormatTypeCapability.INTEGER_FORMAT_TYPE));
37          NUMBER_FORMAT_TYPES.put(
38                  INumberFormatTypeCapability.PERCENT_FORMAT_TYPE_NAME,
39                  new Integer(INumberFormatTypeCapability.PERCENT_FORMAT_TYPE));
40          NUMBER_FORMAT_TYPES.put(
41                  INumberFormatTypeCapability.CURRENCY_FORMAT_TYPE_NAME,
42                  new Integer(INumberFormatTypeCapability.CURRENCY_FORMAT_TYPE));
43      }
44  
45      public Object getAsObject(FacesContext context, UIComponent component,
46              String value) {
47  
48          if (value == null || value.length() < 1 || "default".equals(value)) {
49              return DEFAULT_NUMBER_FORMAT_TYPE;
50          }
51  
52          value = value.toLowerCase();
53  
54          Integer i = (Integer) NUMBER_FORMAT_TYPES.get(value);
55          if (i != null) {
56              return i;
57          }
58  
59          throw new IllegalArgumentException("Keyword '" + value
60                  + "' is not supported for a number format type !");
61      }
62  
63      public String getAsString(FacesContext context, UIComponent component,
64              Object value) {
65  
66          if (value == null) {
67              return (String) NUMBER_FORMAT_TYPES.get(DEFAULT_NUMBER_FORMAT_TYPE);
68          }
69  
70          if ((value instanceof Integer) == false) {
71              throw new IllegalArgumentException("Value must be an Integer !");
72          }
73  
74          for (Iterator it = NUMBER_FORMAT_TYPES.entrySet().iterator(); it
75                  .hasNext();) {
76              Map.Entry entry = (Map.Entry) it.next();
77  
78              if (value.equals(entry.getValue())) {
79                  return (String) entry.getKey();
80              }
81          }
82  
83          throw new IllegalArgumentException("Value '" + value
84                  + "' is not supported for a number format type !");
85      }
86  
87      public static final String getName(int numberFormatType) {
88          switch (numberFormatType) {
89          case INumberFormatTypeCapability.INTEGER_FORMAT_TYPE:
90              return INumberFormatTypeCapability.INTEGER_FORMAT_TYPE_NAME;
91  
92          case INumberFormatTypeCapability.NUMBER_FORMAT_TYPE:
93              return INumberFormatTypeCapability.NUMBER_FORMAT_TYPE_NAME;
94  
95          case INumberFormatTypeCapability.PERCENT_FORMAT_TYPE:
96              return INumberFormatTypeCapability.PERCENT_FORMAT_TYPE_NAME;
97  
98          case INumberFormatTypeCapability.CURRENCY_FORMAT_TYPE:
99              return INumberFormatTypeCapability.CURRENCY_FORMAT_TYPE_NAME;
100         }
101 
102         return null;
103     }
104 }