1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.cubby.action.impl; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.HashMap; |
20 | |
import java.util.List; |
21 | |
import java.util.Map; |
22 | |
|
23 | |
import org.seasar.cubby.action.ActionErrors; |
24 | |
import org.seasar.cubby.action.FieldInfo; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | 131 | public class ActionErrorsImpl implements ActionErrors { |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 131 | private final List<String> all = new ArrayList<String>(); |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 131 | private final Map<String, List<String>> fields = new NotNullHashMap<String, List<String>>( |
43 | |
new MessageListEmptyValueFactory()); |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 131 | private final Map<String, Map<Integer, List<String>>> indexedFields = new NotNullHashMap<String, Map<Integer, List<String>>>( |
49 | |
new IndexMapEmptyValueFactory()); |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | 131 | private final List<String> others = new ArrayList<String>(); |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public List<String> getAll() { |
62 | 19 | return all; |
63 | |
} |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public Map<String, List<String>> getFields() { |
71 | 86 | return fields; |
72 | |
} |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public Map<String, Map<Integer, List<String>>> getIndexedFields() { |
80 | 81 | return indexedFields; |
81 | |
} |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public List<String> getOthers() { |
89 | 5 | return others; |
90 | |
} |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public boolean isEmpty() { |
98 | 10 | return all.isEmpty(); |
99 | |
} |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public void add(final String message) { |
108 | 3 | this.add(message, new FieldInfo[0]); |
109 | 3 | } |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
public void add(final String message, final String... fieldNames) { |
120 | 1 | final FieldInfo[] fieldInfos = new FieldInfo[fieldNames.length]; |
121 | 3 | for (int i = 0; i < fieldNames.length; i++) { |
122 | 2 | fieldInfos[i] = new FieldInfo(fieldNames[i]); |
123 | |
} |
124 | 1 | this.add(message, fieldInfos); |
125 | 1 | } |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public void add(final String message, final FieldInfo... fieldInfos) { |
136 | 48 | if (fieldInfos == null || fieldInfos.length == 0) { |
137 | 3 | others.add(message); |
138 | |
} else { |
139 | 91 | for (final FieldInfo fieldInfo : fieldInfos) { |
140 | 46 | addFields(message, fieldInfo); |
141 | |
} |
142 | |
} |
143 | 48 | this.all.add(message); |
144 | 48 | } |
145 | |
|
146 | |
private void addFields(final String message, final FieldInfo fieldInfo) { |
147 | |
final String name; |
148 | |
final Integer index; |
149 | 46 | if (fieldInfo == null) { |
150 | 1 | name = null; |
151 | 1 | index = null; |
152 | |
} else { |
153 | 45 | name = fieldInfo.getName(); |
154 | 45 | index = fieldInfo.getIndex(); |
155 | |
} |
156 | |
|
157 | 46 | final List<String> messages = this.fields.get(name); |
158 | 46 | messages.add(message); |
159 | |
|
160 | 46 | final List<String> indexedMessages = this.indexedFields.get(name).get( |
161 | |
index); |
162 | 46 | indexedMessages.add(message); |
163 | 46 | } |
164 | |
|
165 | 307 | private static class NotNullHashMap<K, V> extends HashMap<K, V> { |
166 | |
|
167 | |
private static final long serialVersionUID = 1L; |
168 | |
|
169 | |
private transient final EmptyValueFactory<V> emptyValueFactory; |
170 | |
|
171 | 307 | private NotNullHashMap(final EmptyValueFactory<V> emptyValueFactory) { |
172 | 307 | this.emptyValueFactory = emptyValueFactory; |
173 | 307 | } |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
@SuppressWarnings("unchecked") |
179 | |
@Override |
180 | |
public V get(final Object key) { |
181 | 386 | final V value = super.get(key); |
182 | 386 | if (value != null) { |
183 | 195 | return value; |
184 | |
} |
185 | |
|
186 | 191 | final V emptyValue = emptyValueFactory.create(); |
187 | 191 | this.put((K) key, emptyValue); |
188 | 191 | return emptyValue; |
189 | |
} |
190 | |
|
191 | |
} |
192 | |
|
193 | |
private interface EmptyValueFactory<T> { |
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
T create(); |
200 | |
} |
201 | |
|
202 | 498 | private static class MessageListEmptyValueFactory implements |
203 | |
EmptyValueFactory<List<String>> { |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
public List<String> create() { |
209 | 146 | return new ArrayList<String>(); |
210 | |
} |
211 | |
|
212 | |
} |
213 | |
|
214 | 307 | private static class IndexMapEmptyValueFactory implements |
215 | |
EmptyValueFactory<Map<Integer, List<String>>> { |
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
public Map<Integer, List<String>> create() { |
221 | 45 | return new NotNullHashMap<Integer, List<String>>( |
222 | |
new MessageListEmptyValueFactory()); |
223 | |
} |
224 | |
|
225 | |
} |
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
public void clear() { |
231 | 1 | this.all.clear(); |
232 | 1 | this.fields.clear(); |
233 | 1 | this.indexedFields.clear(); |
234 | 1 | this.others.clear(); |
235 | 1 | } |
236 | |
|
237 | |
} |