1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package javax.faces.component;
20
21 import java.io.Serializable;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28
29 import javax.el.ValueExpression;
30 import javax.faces.context.FacesContext;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144 class _DeltaStateHelper implements StateHelper
145 {
146
147
148
149
150
151
152
153 private UIComponent _component;
154
155
156
157
158 private Map<Serializable, Object> _fullState;
159
160
161
162
163 private Map<Serializable, Object> _deltas;
164
165
166 private Object[] _initialState;
167
168
169
170
171
172
173
174 private boolean _transient = false;
175
176 public _DeltaStateHelper(UIComponent component)
177 {
178 super();
179 this._component = component;
180 _fullState = new HashMap<Serializable, Object>();
181 _deltas = null;
182
183 }
184
185
186
187
188
189
190 private boolean _createDeltas()
191 {
192 if (isInitialStateMarked())
193 {
194 if (_deltas == null)
195 {
196 _deltas = new HashMap<Serializable, Object>(2);
197 }
198 return true;
199 }
200
201 return false;
202 }
203
204 protected boolean isInitialStateMarked()
205 {
206 return _component.initialStateMarked();
207 }
208
209 public void add(Serializable key, Object value)
210 {
211 if (_createDeltas())
212 {
213
214 Map<Object, Boolean> deltaListMapValues = (Map<Object, Boolean>) _deltas
215 .get(key);
216 if (deltaListMapValues == null)
217 {
218 deltaListMapValues = new InternalDeltaListMap<Object, Boolean>(
219 3);
220 _deltas.put(key, deltaListMapValues);
221 }
222 deltaListMapValues.put(value, Boolean.TRUE);
223 }
224
225
226 List<Object> fullListValues = (List<Object>) _fullState.get(key);
227 if (fullListValues == null)
228 {
229 fullListValues = new InternalList<Object>(3);
230 _fullState.put(key, fullListValues);
231 }
232 fullListValues.add(value);
233 }
234
235 public Object eval(Serializable key)
236 {
237 Object returnValue = _fullState.get(key);
238 if (returnValue != null)
239 {
240 return returnValue;
241 }
242 ValueExpression expression = _component.getValueExpression(key
243 .toString());
244 if (expression != null)
245 {
246 return expression.getValue(_component.getFacesContext()
247 .getELContext());
248 }
249 return null;
250 }
251
252 public Object eval(Serializable key, Object defaultValue)
253 {
254 Object returnValue = _fullState.get(key);
255 if (returnValue != null)
256 {
257 return returnValue;
258 }
259 ValueExpression expression = _component.getValueExpression(key
260 .toString());
261 if (expression != null)
262 {
263 return expression.getValue(_component.getFacesContext()
264 .getELContext());
265 }
266 return defaultValue;
267 }
268
269 public Object get(Serializable key)
270 {
271 return _fullState.get(key);
272 }
273
274 public Object put(Serializable key, Object value)
275 {
276 Object returnValue = null;
277 if (_createDeltas())
278 {
279 if (_deltas.containsKey(key))
280 {
281 returnValue = _deltas.put(key, value);
282 _fullState.put(key, value);
283 }
284 else if (value == null && !_fullState.containsKey(key))
285 {
286 returnValue = null;
287 }
288 else
289 {
290 _deltas.put(key, value);
291 returnValue = _fullState.put(key, value);
292 }
293 }
294 else
295 {
296
297
298
299
300
301
302 returnValue = _fullState.put(key, value);
303 }
304 return returnValue;
305 }
306
307 public Object put(Serializable key, String mapKey, Object value)
308 {
309 boolean returnSet = false;
310 Object returnValue = null;
311 if (_createDeltas())
312 {
313
314 Map<String, Object> mapValues = (Map<String, Object>) _deltas
315 .get(key);
316 if (mapValues == null)
317 {
318 mapValues = new InternalMap<String, Object>();
319 _deltas.put(key, mapValues);
320 }
321 if (mapValues.containsKey(mapKey))
322 {
323 returnValue = mapValues.put(mapKey, value);
324 returnSet = true;
325 }
326 else
327 {
328 mapValues.put(mapKey, value);
329 }
330 }
331
332
333 Map<String, Object> mapValues = (Map<String, Object>) _fullState
334 .get(key);
335 if (mapValues == null)
336 {
337 mapValues = new InternalMap<String, Object>();
338 _fullState.put(key, mapValues);
339 }
340 if (returnSet)
341 {
342 mapValues.put(mapKey, value);
343 }
344 else
345 {
346 returnValue = mapValues.put(mapKey, value);
347 }
348 return returnValue;
349 }
350
351 public Object remove(Serializable key)
352 {
353 Object returnValue = null;
354 if (_createDeltas())
355 {
356 if (_deltas.containsKey(key))
357 {
358
359 returnValue = _deltas.put(key, null);
360 _fullState.remove(key);
361 }
362 else
363 {
364
365 _deltas.put(key, null);
366 returnValue = _fullState.remove(key);
367 }
368 }
369 else
370 {
371 returnValue = _fullState.remove(key);
372 }
373 return returnValue;
374 }
375
376 public Object remove(Serializable key, Object valueOrKey)
377 {
378
379
380
381
382
383
384 Object collectionOrMap = _fullState.get(key);
385 Object returnValue = null;
386 if (collectionOrMap instanceof InternalMap)
387 {
388 if (_createDeltas())
389 {
390 returnValue = _removeValueOrKeyFromMap(_deltas, key,
391 valueOrKey, true);
392 _removeValueOrKeyFromMap(_fullState, key, valueOrKey, false);
393 }
394 else
395 {
396 returnValue = _removeValueOrKeyFromMap(_fullState, key,
397 valueOrKey, false);
398 }
399 }
400 else if (collectionOrMap instanceof InternalList)
401 {
402 if (_createDeltas())
403 {
404 returnValue = _removeValueOrKeyFromCollectionDelta(_deltas,
405 key, valueOrKey);
406 _removeValueOrKeyFromCollection(_fullState, key, valueOrKey);
407 }
408 else
409 {
410 returnValue = _removeValueOrKeyFromCollection(_fullState, key,
411 valueOrKey);
412 }
413 }
414 return returnValue;
415 }
416
417 private static Object _removeValueOrKeyFromCollectionDelta(
418 Map<Serializable, Object> stateMap, Serializable key,
419 Object valueOrKey)
420 {
421 Object returnValue = null;
422 Map<Object, Boolean> c = (Map<Object, Boolean>) stateMap.get(key);
423 if (c != null)
424 {
425 if (c.containsKey(valueOrKey))
426 {
427 returnValue = valueOrKey;
428 }
429 c.put(valueOrKey, Boolean.FALSE);
430 }
431 return returnValue;
432 }
433
434 private static Object _removeValueOrKeyFromCollection(
435 Map<Serializable, Object> stateMap, Serializable key,
436 Object valueOrKey)
437 {
438 Object returnValue = null;
439 Collection c = (Collection) stateMap.get(key);
440 if (c != null)
441 {
442 if (c.remove(valueOrKey))
443 {
444 returnValue = valueOrKey;
445 }
446 if (c.isEmpty())
447 {
448 stateMap.remove(key);
449 }
450 }
451 return returnValue;
452 }
453
454 private static Object _removeValueOrKeyFromMap(
455 Map<Serializable, Object> stateMap, Serializable key,
456 Object valueOrKey, boolean delta)
457 {
458 if (valueOrKey == null)
459 {
460 return null;
461 }
462
463 Object returnValue = null;
464 Map<String, Object> map = (Map<String, Object>) stateMap.get(key);
465 if (map != null)
466 {
467 if (delta)
468 {
469
470 returnValue = map.put((String) valueOrKey, null);
471 }
472 else
473 {
474 returnValue = map.remove(valueOrKey);
475 }
476
477 if (map.isEmpty())
478 {
479
480 stateMap.put(key, null);
481 }
482 }
483 return returnValue;
484 }
485
486 public boolean isTransient()
487 {
488 return _transient;
489 }
490
491
492
493
494
495
496
497
498
499
500
501
502
503 public Object saveState(FacesContext context)
504 {
505 Map serializableMap = (isInitialStateMarked()) ? _deltas : _fullState;
506
507 if (_initialState != null && _deltas != null && !_deltas.isEmpty()
508 && isInitialStateMarked())
509 {
510
511
512
513
514
515
516
517
518
519 for (int i = 0; i < _initialState.length; i+=2)
520 {
521 Serializable key = (Serializable) _initialState[i];
522 Object defaultValue = _initialState[i+1];
523
524
525
526
527 if (_deltas.containsKey(key))
528 {
529 Object deltaValue = _deltas.get(key);
530 if (deltaValue == null && defaultValue == null)
531 {
532 _deltas.remove(key);
533 if (_deltas.isEmpty())
534 {
535 break;
536 }
537 }
538 if (deltaValue != null && deltaValue.equals(defaultValue))
539 {
540 _deltas.remove(key);
541 if (_deltas.isEmpty())
542 {
543 break;
544 }
545 }
546 }
547 }
548 }
549 if (serializableMap == null || serializableMap.size() == 0)
550 {
551 return null;
552 }
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568 Map.Entry<Serializable, Object> entry;
569
570 Object[] retArr = new Object[serializableMap.entrySet().size() * 2];
571
572
573 Iterator<Map.Entry<Serializable, Object>> it = serializableMap
574 .entrySet().iterator();
575 int cnt = 0;
576 while (it.hasNext())
577 {
578 entry = it.next();
579 retArr[cnt] = entry.getKey();
580
581 Object value = entry.getValue();
582
583
584
585
586 if (value instanceof StateHolder ||
587 value instanceof List ||
588 !(value instanceof Serializable))
589 {
590 Object savedValue = UIComponentBase.saveAttachedState(context,
591 value);
592 retArr[cnt + 1] = savedValue;
593 }
594 else
595 {
596 retArr[cnt + 1] = value;
597 }
598 cnt += 2;
599 }
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634 return retArr;
635 }
636
637 public void restoreState(FacesContext context, Object state)
638 {
639 if (state == null)
640 return;
641
642 Object[] serializedState = (Object[]) state;
643
644 if (!isInitialStateMarked() && !_fullState.isEmpty())
645 {
646 _fullState.clear();
647 if(_deltas != null)
648 {
649 _deltas.clear();
650 }
651 }
652
653 for (int cnt = 0; cnt < serializedState.length; cnt += 2)
654 {
655 Serializable key = (Serializable) serializedState[cnt];
656 Object savedValue = UIComponentBase.restoreAttachedState(context,
657 serializedState[cnt + 1]);
658
659 if (isInitialStateMarked())
660 {
661 if (savedValue instanceof InternalDeltaListMap)
662 {
663 for (Map.Entry<Object, Boolean> mapEntry : ((Map<Object, Boolean>) savedValue)
664 .entrySet())
665 {
666 boolean addOrRemove = mapEntry.getValue();
667 if (addOrRemove)
668 {
669
670 this.add(key, mapEntry.getKey());
671 }
672 else
673 {
674
675 this.remove(key, mapEntry.getKey());
676 }
677 }
678 }
679 else if (savedValue instanceof InternalMap)
680 {
681 for (Map.Entry<String, Object> mapEntry : ((Map<String, Object>) savedValue)
682 .entrySet())
683 {
684 this.put(key, mapEntry.getKey(), mapEntry.getValue());
685 }
686 }
687
688
689
690
691
692
693
694
695
696
697 else
698 {
699 put(key, savedValue);
700 }
701 }
702 else
703 {
704 put(key, savedValue);
705 }
706 }
707 }
708
709 public void setTransient(boolean transientValue)
710 {
711 _transient = transientValue;
712 }
713
714
715
716 static class InternalMap<K, V> extends HashMap<K, V> implements StateHolder
717 {
718 public InternalMap()
719 {
720 super();
721 }
722
723 public InternalMap(int initialCapacity, float loadFactor)
724 {
725 super(initialCapacity, loadFactor);
726 }
727
728 public InternalMap(Map<? extends K, ? extends V> m)
729 {
730 super(m);
731 }
732
733 public InternalMap(int initialSize)
734 {
735 super(initialSize);
736 }
737
738 public boolean isTransient()
739 {
740 return false;
741 }
742
743 public void setTransient(boolean newTransientValue)
744 {
745
746 }
747
748 public void restoreState(FacesContext context, Object state)
749 {
750 Object[] listAsMap = (Object[]) state;
751 for (int cnt = 0; cnt < listAsMap.length; cnt += 2)
752 {
753 this.put((K) listAsMap[cnt], (V) UIComponentBase
754 .restoreAttachedState(context, listAsMap[cnt + 1]));
755 }
756 }
757
758 public Object saveState(FacesContext context)
759 {
760 int cnt = 0;
761 Object[] mapArr = new Object[this.size() * 2];
762 for (Map.Entry<K, V> entry : this.entrySet())
763 {
764 mapArr[cnt] = entry.getKey();
765 Object value = entry.getValue();
766
767 if (value instanceof StateHolder ||
768 value instanceof List ||
769 !(value instanceof Serializable))
770 {
771 mapArr[cnt + 1] = UIComponentBase.saveAttachedState(context, value);
772 }
773 else
774 {
775 mapArr[cnt + 1] = value;
776 }
777 cnt += 2;
778 }
779 return mapArr;
780 }
781 }
782
783
784
785
786 static class InternalDeltaListMap<K, V> extends InternalMap<K, V>
787 {
788
789 public InternalDeltaListMap()
790 {
791 super();
792 }
793
794 public InternalDeltaListMap(int initialCapacity, float loadFactor)
795 {
796 super(initialCapacity, loadFactor);
797 }
798
799 public InternalDeltaListMap(int initialSize)
800 {
801 super(initialSize);
802 }
803
804 public InternalDeltaListMap(Map<? extends K, ? extends V> m)
805 {
806 super(m);
807 }
808 }
809
810 static class InternalList<T> extends ArrayList<T> implements StateHolder
811 {
812 public InternalList()
813 {
814 super();
815 }
816
817 public InternalList(Collection<? extends T> c)
818 {
819 super(c);
820 }
821
822 public InternalList(int initialSize)
823 {
824 super(initialSize);
825 }
826
827 public boolean isTransient()
828 {
829 return false;
830 }
831
832 public void setTransient(boolean newTransientValue)
833 {
834 }
835
836 public void restoreState(FacesContext context, Object state)
837 {
838 Object[] listAsArr = (Object[]) state;
839
840
841 for (Object elem : listAsArr)
842 {
843 add((T) UIComponentBase.restoreAttachedState(context, elem));
844 }
845 }
846
847 public Object saveState(FacesContext context)
848 {
849 Object[] values = new Object[size()];
850 for (int i = 0; i < size(); i++)
851 {
852 Object value = get(i);
853
854 if (value instanceof StateHolder ||
855 value instanceof List ||
856 !(value instanceof Serializable))
857 {
858 values[i] = UIComponentBase.saveAttachedState(context, value);
859 }
860 else
861 {
862 values[i] = value;
863 }
864 }
865 return values;
866 }
867 }
868
869 public void markPropertyInInitialState(Object[] defaultInitialState)
870 {
871
872 boolean canApplyDefaultInitialState = true;
873 for (int i = 0; i < defaultInitialState.length; i+=2)
874 {
875 Serializable key = (Serializable) defaultInitialState[i];
876 if (_fullState.containsKey(key))
877 {
878 canApplyDefaultInitialState = false;
879 break;
880 }
881 }
882 if (canApplyDefaultInitialState)
883 {
884
885 _initialState = defaultInitialState;
886 }
887 else
888 {
889
890 Object[] initialState = new Object[defaultInitialState.length];
891 for (int i = 0; i < defaultInitialState.length; i+=2)
892 {
893 Serializable key = (Serializable) defaultInitialState[i];
894 initialState[i] = key;
895 if (_fullState.containsKey(key))
896 {
897 initialState[i+1] = _fullState.get(key);
898 }
899 else
900 {
901 initialState[i+1] = defaultInitialState[i+1];
902 }
903 }
904 _initialState = initialState;
905 }
906 }
907 }