1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.trinidad.bean.util;
20
21 import java.util.Map;
22
23 import org.apache.myfaces.trinidad.bean.FacesBean;
24 import org.apache.myfaces.trinidad.bean.PropertyKey;
25 import org.apache.myfaces.trinidad.bean.PropertyMap;
26 import org.apache.myfaces.trinidad.util.ArrayMap;
27
28 import javax.faces.context.FacesContext;
29
30 public class PropertyArrayMap extends ArrayMap<PropertyKey,Object>
31 implements PropertyMap
32 {
33 public PropertyArrayMap(
34 int initialCapacity)
35 {
36 super(initialCapacity);
37 }
38
39 public PropertyArrayMap()
40 {
41 super();
42 }
43
44 public Object get(
45 PropertyKey pKey)
46 {
47 if (pKey.getIndex() < 0)
48 return get(pKey);
49 return getByIdentity(pKey);
50 }
51
52 @Override
53 public Object put(
54 PropertyKey key,
55 Object value)
56 {
57 Object retValue = super.put(key, value);
58 if (_createDeltas())
59 {
60 if (!_equals(value, retValue))
61 _deltas.put(key, value);
62 }
63
64 return retValue;
65 }
66
67 @Override
68 public Object remove(
69 Object key)
70 {
71 if (_createDeltas())
72 {
73 if (!super.containsKey(key))
74 return null;
75
76
77 assert(key instanceof PropertyKey);
78 _deltas.put((PropertyKey) key, null);
79 }
80
81 return super.remove(key);
82 }
83
84 @Override
85 public void putAll(Map<? extends PropertyKey, ? extends Object> t)
86 {
87 if (_createDeltas())
88 _deltas.putAll(t);
89
90 super.putAll(t);
91 }
92
93 public Object saveState(FacesContext context)
94 {
95 if (_initialStateMarked)
96 {
97 if (_deltas == null)
98 return null;
99
100 return StateUtils.saveState(_deltas, context, getUseStateHolder());
101 }
102 else
103 {
104 return StateUtils.saveState(this, context, getUseStateHolder());
105 }
106 }
107
108 public void restoreState(
109 FacesContext context,
110 FacesBean.Type type,
111 Object state)
112 {
113 StateUtils.restoreState(this, context, type, state, getUseStateHolder());
114 }
115
116 protected PropertyMap createDeltaPropertyMap()
117 {
118 PropertyArrayMap map = new PropertyArrayMap(2);
119 map.setUseStateHolder(getUseStateHolder());
120 return map;
121 }
122
123
124 public boolean getUseStateHolder()
125 {
126 return _useStateHolder;
127 }
128
129 public void setUseStateHolder(boolean useStateHolder)
130 {
131 _useStateHolder = useStateHolder;
132 }
133
134
135
136
137
138 public void markInitialState()
139 {
140 _initialStateMarked = true;
141 }
142
143
144 private boolean _createDeltas()
145 {
146 if (_initialStateMarked)
147 {
148 if (_deltas == null)
149 {
150 _deltas = createDeltaPropertyMap();
151 }
152
153 return true;
154 }
155
156 return false;
157 }
158
159 static private boolean _equals(Object a, Object b)
160 {
161 if (a == b)
162 return true;
163
164 if (a == null)
165 return false;
166
167 return a.equals(b);
168 }
169
170 private transient boolean _initialStateMarked;
171 private transient PropertyMap _deltas;
172 private boolean _useStateHolder;
173 }