View Javadoc

1   /*
2    * $Id: StateHolderTools.java,v 1.1 2008/09/17 09:12:20 oeuillot Exp $
3    */
4   package org.rcfaces.core.internal.util;
5   
6   import java.sql.Time;
7   import java.util.Date;
8   import java.util.HashMap;
9   import java.util.HashSet;
10  import java.util.Iterator;
11  import java.util.Map;
12  import java.util.Set;
13  
14  import javax.faces.component.UIComponentBase;
15  import javax.faces.context.FacesContext;
16  
17  /**
18   * 
19   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
20   * @version $Revision: 1.1 $ $Date: 2008/09/17 09:12:20 $
21   */
22  public class StateHolderTools {
23      private static final String REVISION = "$Revision: 1.1 $";
24  
25      private static final Set PRIMITIVE_CLASSES = new HashSet(12);
26      static {
27          PRIMITIVE_CLASSES.add(String.class.getName());
28          PRIMITIVE_CLASSES.add(Long.class.getName());
29          PRIMITIVE_CLASSES.add(Integer.class.getName());
30          PRIMITIVE_CLASSES.add(Short.class.getName());
31          PRIMITIVE_CLASSES.add(Byte.class.getName());
32          PRIMITIVE_CLASSES.add(Boolean.class.getName());
33          PRIMITIVE_CLASSES.add(Double.class.getName());
34          PRIMITIVE_CLASSES.add(Float.class.getName());
35          PRIMITIVE_CLASSES.add(Character.class.getName());
36          PRIMITIVE_CLASSES.add(Date.class.getName());
37          PRIMITIVE_CLASSES.add(Time.class.getName());
38      }
39  
40      public static final boolean isPrimitive(Object value) {
41          if (value == null) {
42              return true;
43          }
44  
45          return PRIMITIVE_CLASSES.contains(value.getClass().getName());
46      }
47  
48      public static final Object saveMap(FacesContext facesContext, Map properties) {
49  
50          if (properties == null || properties.isEmpty()) {
51              return null;
52          }
53  
54          Object rets[] = new Object[properties.size() * 2];
55          int i = 0;
56          for (Iterator it = properties.entrySet().iterator(); it.hasNext();) {
57              Map.Entry entry = (Map.Entry) it.next();
58  
59              rets[i++] = entry.getKey();
60  
61              Object value = entry.getValue();
62  
63              if (StateHolderTools.isPrimitive(value) == false) {
64                  rets[i++] = UIComponentBase.saveAttachedState(facesContext,
65                          value);
66  
67                  continue;
68              }
69  
70              rets[i++] = value;
71          }
72  
73          return rets;
74      }
75  
76      public static final Map loadMap(FacesContext facesContext, Object state) {
77  
78          Object datas[] = (Object[]) state;
79  
80          if (datas == null || datas.length == 0) {
81              return new HashMap();
82          }
83  
84          Map map = new HashMap(datas.length / 2);
85  
86          for (int i = 0; i < datas.length;) {
87              String key = (String) datas[i++];
88              Object value = datas[i++];
89  
90              if (StateHolderTools.isPrimitive(value) == false) {
91                  value = UIComponentBase.restoreAttachedState(facesContext,
92                          value);
93              }
94  
95              map.put(key, value);
96          }
97  
98          return map;
99      }
100 }