1
2
3
4 package org.rcfaces.core.internal.lang;
5
6 import java.io.Serializable;
7 import java.util.Collections;
8 import java.util.Date;
9 import java.util.HashMap;
10 import java.util.Iterator;
11 import java.util.Map;
12
13 import javax.faces.FacesException;
14
15 import org.rcfaces.core.lang.IClientStorage;
16 import org.rcfaces.core.lang.Time;
17 import org.w3c.dom.Document;
18
19
20
21
22
23
24 public class ClientStorage implements IClientStorage, Serializable {
25 private static final String REVISION = "$Revision: 1.1 $";
26
27 private static final long serialVersionUID = -4206007717671659424L;
28
29 private static final Iterator EMPTY_ITERATOR = Collections.EMPTY_LIST
30 .iterator();
31
32 private Map values = null;
33
34
35
36 public ClientStorage() {
37 }
38
39 public synchronized Object getAttribute(String name) {
40 if (values == null) {
41 return null;
42 }
43
44 return values.get(name);
45 }
46
47 public synchronized Object setAttribute(String name, Object value) {
48 verifyValue(value);
49
50 if (values == null) {
51 values = new HashMap();
52 }
53
54 String ret = (String) values.put(name, value);
55
56
57
58
59
60
61 return ret;
62 }
63
64 public synchronized Object removeAttribute(String name) {
65 if (values == null) {
66 return null;
67 }
68
69 String ret = (String) values.remove(name);
70
71
72
73
74
75 if (values.isEmpty()) {
76 values = null;
77 }
78
79 return ret;
80 }
81
82 public synchronized Iterator listAttributeNames() {
83 if (values == null) {
84 return EMPTY_ITERATOR;
85 }
86
87 return values.keySet().iterator();
88 }
89
90 public synchronized int getSize() {
91 if (values == null) {
92 return 0;
93 }
94
95 return values.size();
96 }
97
98 public synchronized boolean isEmpty() {
99 if (values == null) {
100 return true;
101 }
102
103 return values.isEmpty();
104 }
105
106 public Map getInternalMap() {
107 return values;
108 }
109
110 private static void verifyValue(Object value) {
111 if (value == null) {
112 return;
113 }
114
115 if (value instanceof String) {
116 return;
117 }
118 if (value instanceof Date) {
119 return;
120 }
121 if (value instanceof Time) {
122 return;
123 }
124 if (value instanceof Number) {
125 return;
126 }
127 if (value instanceof Document) {
128 return;
129 }
130
131 throw new FacesException("Invalid object type '" + value.getClass()
132 + "' for a clientStorage value.");
133 }
134
135
136
137
138 }