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 | 59 | public class ActionErrorsImpl implements ActionErrors { |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 59 | private final List<String> all = new ArrayList<String>(); |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 59 | private final Map<String, List<String>> fields = new NotNullHashMap<String, List<String>>( |
43 | |
new MessageListEmptyValueFactory()); |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 59 | private final Map<String, Map<Integer, List<String>>> indexedFields = new NotNullHashMap<String, Map<Integer, List<String>>>( |
49 | |
new IndexMapEmptyValueFactory()); |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | 59 | private final List<String> others = new ArrayList<String>(); |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public List<String> getAll() { |
62 | 15 | return all; |
63 | |
} |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public Map<String, List<String>> getFields() { |
71 | 53 | return fields; |
72 | |
} |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public Map<String, Map<Integer, List<String>>> getIndexedFields() { |
80 | 5 | 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 | 12 | if (fieldInfos == null || fieldInfos.length == 0) { |
137 | 3 | others.add(message); |
138 | |
} else { |
139 | 19 | for (final FieldInfo fieldInfo : fieldInfos) { |
140 | 10 | addFields(message, fieldInfo); |
141 | |
} |
142 | |
} |
143 | 12 | this.all.add(message); |
144 | 12 | } |
145 | |
|
146 | |
private void addFields(final String message, final FieldInfo fieldInfo) { |
147 | 10 | final String name = fieldInfo == null ? null : fieldInfo.getName(); |
148 | 10 | final Integer index = fieldInfo == null ? null : fieldInfo.getIndex(); |
149 | |
|
150 | 10 | final List<String> messages = this.fields.get(name); |
151 | 10 | messages.add(message); |
152 | |
|
153 | 10 | final List<String> indexedMessages = this.indexedFields.get(name).get( |
154 | |
index); |
155 | 10 | indexedMessages.add(message); |
156 | 10 | } |
157 | |
|
158 | |
class NotNullHashMap<K, V> extends HashMap<K, V> { |
159 | |
|
160 | |
private static final long serialVersionUID = 1L; |
161 | |
|
162 | |
private final EmptyValueFactory<V> emptyValueFactory; |
163 | |
|
164 | 127 | public NotNullHashMap(EmptyValueFactory<V> emptyValueFactory) { |
165 | 127 | this.emptyValueFactory = emptyValueFactory; |
166 | 127 | } |
167 | |
|
168 | |
@SuppressWarnings("unchecked") |
169 | |
@Override |
170 | |
public V get(final Object key) { |
171 | 93 | final V value = super.get(key); |
172 | 93 | if (value != null) { |
173 | 28 | return value; |
174 | |
} |
175 | |
|
176 | 65 | final V emptyValue = emptyValueFactory.create(); |
177 | 65 | this.put((K) key, emptyValue); |
178 | 65 | return emptyValue; |
179 | |
} |
180 | |
|
181 | |
} |
182 | |
|
183 | |
interface EmptyValueFactory<T> { |
184 | |
T create(); |
185 | |
} |
186 | |
|
187 | 124 | class MessageListEmptyValueFactory implements EmptyValueFactory<List<String>> { |
188 | |
|
189 | |
public List<String> create() { |
190 | 56 | return new ArrayList<String>(); |
191 | |
} |
192 | |
|
193 | |
} |
194 | |
|
195 | 59 | class IndexMapEmptyValueFactory implements EmptyValueFactory<Map<Integer, List<String>>> { |
196 | |
|
197 | |
public Map<Integer, List<String>> create() { |
198 | 9 | return new NotNullHashMap<Integer, List<String>>( |
199 | |
new MessageListEmptyValueFactory()); |
200 | |
} |
201 | |
|
202 | |
} |
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
public void clear() { |
208 | 0 | this.all.clear(); |
209 | 0 | this.fields.clear(); |
210 | 0 | this.indexedFields.clear(); |
211 | 0 | this.others.clear(); |
212 | 0 | } |
213 | |
|
214 | |
} |