View Javadoc

1   package org.rcfaces.core.lang;
2   
3   import java.util.Collection;
4   import java.util.HashMap;
5   import java.util.Iterator;
6   import java.util.Map;
7   import java.util.Map.Entry;
8   
9   import javax.faces.FacesException;
10  import javax.faces.context.FacesContext;
11  
12  import org.apache.commons.logging.Log;
13  import org.apache.commons.logging.LogFactory;
14  import org.rcfaces.core.internal.renderkit.AbstractProperties;
15  import org.rcfaces.core.model.IFilterProperties;
16  
17  /**
18   * 
19   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
20   * @version $Revision: 1.1 $ $Date: 2007/05/25 16:27:31 $
21   */
22  public final class FilterPropertiesMap extends AbstractProperties implements
23          IFilterProperties {
24  
25      private static final String REVISION = "$Revision: 1.1 $";
26  
27      private static final Log LOG = LogFactory.getLog(FilterPropertiesMap.class);
28  
29      static final boolean TEST_MAP_CONTENT = false;
30  
31      private static final long serialVersionUID = -6566852140453141630L;
32  
33      private final Map map;
34  
35      public FilterPropertiesMap(Map map) {
36          this.map = new HashMap(map);
37      }
38  
39      public FilterPropertiesMap(IFilterProperties filterProperties) {
40          this.map = copyMap(filterProperties);
41      }
42  
43      public FilterPropertiesMap() {
44          this.map = new HashMap();
45      }
46  
47      public void clear() {
48          map.clear();
49      }
50  
51      public boolean isEmpty() {
52          return map.isEmpty();
53      }
54  
55      public Object put(String key, Object value) {
56          return map.put(key, value);
57      }
58  
59      public void putAll(Map t) {
60          if (TEST_MAP_CONTENT) {
61              for (Iterator it = t.entrySet().iterator(); it.hasNext();) {
62                  Map.Entry entry = (Map.Entry) it.next();
63  
64                  Object key = entry.getKey();
65                  if (key != null && (key instanceof String) == false) {
66                      throw new FacesException("Key is not a String !");
67                  }
68  
69                  Object value = entry.getValue();
70                  if (value != null && (value instanceof String) == false) {
71                      throw new FacesException("Value is not a String !");
72                  }
73              }
74          }
75  
76          map.putAll(t);
77      }
78  
79      public Object remove(String key) {
80          return map.remove(key);
81      }
82  
83      public int size() {
84          return map.size();
85      }
86  
87      public boolean containsKey(String propertyName) {
88          return map.containsKey(propertyName);
89      }
90  
91      public String[] listNames() {
92          Collection c = map.keySet();
93  
94          return (String[]) c.toArray(new String[c.size()]);
95      }
96  
97      public Map toMap() {
98          return new HashMap(map);
99      }
100 
101     public Object saveState(FacesContext context) {
102         if (map.isEmpty()) {
103             return null;
104         }
105 
106         Object ret[] = new Object[map.size() * 2];
107 
108         int idx = 0;
109         for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
110             Map.Entry entry = (Entry) it.next();
111 
112             ret[idx++] = entry.getKey();
113             ret[idx++] = entry.getValue();
114         }
115 
116         return ret;
117     }
118 
119     public void restoreState(FacesContext context, Object state) {
120         if (state == null) {
121             return;
122         }
123 
124         Object p[] = (Object[]) state;
125 
126         for (int i = 0; i < p.length;) {
127             map.put(p[i++], p[i++]);
128         }
129     }
130 
131     public boolean isTransient() {
132         return false;
133     }
134 
135     public void setTransient(boolean newTransientValue) {
136     }
137 
138     public Object getProperty(String name) {
139         return map.get(name);
140     }
141 
142     public int hashCode() {
143         final int PRIME = 31;
144         int result = 1;
145         result = PRIME * result + ((map == null) ? 0 : map.hashCode());
146         return result;
147     }
148 
149     public boolean equals(Object obj) {
150         if (this == obj) {
151             return true;
152         }
153 
154         if (obj == null || this.getClass() != obj.getClass()) {
155             return false;
156         }
157 
158         final FilterPropertiesMap other = (FilterPropertiesMap) obj;
159         if (map == null) {
160             if (other.map != null) {
161                 return false;
162             }
163 
164         } else if (!map.equals(other.map)) {
165             return false;
166         }
167 
168         return true;
169     }
170 
171     private Map copyMap(IFilterProperties filterProperties) {
172         if (filterProperties instanceof FilterPropertiesMap) {
173             // Pas besoin de copie !
174             return filterProperties.toMap();
175         }
176 
177         return new HashMap(filterProperties.toMap());
178     }
179 
180     static {
181         if (TEST_MAP_CONTENT) {
182             LOG.info("TEST_MAP_CONTENT=" + TEST_MAP_CONTENT);
183         }
184     }
185 
186 }