gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / namestranslator / BaseNamesTranslator.java @ 2222
History | View | Annotate | Download (11.3 KB)
1 |
package org.gvsig.tools.namestranslator; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Arrays; |
5 |
import java.util.List; |
6 |
import java.util.function.Function; |
7 |
import org.apache.commons.lang3.StringUtils; |
8 |
import org.gvsig.tools.ToolsLocator; |
9 |
import org.gvsig.tools.dynobject.DynStruct; |
10 |
import org.gvsig.tools.persistence.PersistenceManager; |
11 |
import org.gvsig.tools.persistence.Persistent; |
12 |
import org.gvsig.tools.persistence.PersistentState; |
13 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
14 |
|
15 |
/**
|
16 |
*
|
17 |
* @author omartinez
|
18 |
*/
|
19 |
public class BaseNamesTranslator extends AbstractNamesTranslator implements NamesTranslator, Persistent { |
20 |
|
21 |
protected SimpleMap source2translation;
|
22 |
protected SimpleMap translation2source;
|
23 |
protected boolean hasTranslations; |
24 |
public static final String DEFAULT_FIELD_NAME = "field"; |
25 |
protected SimpleList translatedNames;
|
26 |
|
27 |
protected BaseNamesTranslator() {
|
28 |
this.sourceNames = new SimpleList(); |
29 |
this.translatedNames = new SimpleList(); |
30 |
|
31 |
} |
32 |
|
33 |
@Override
|
34 |
public NamesTranslator clone() throws CloneNotSupportedException { |
35 |
BaseNamesTranslator clone = (BaseNamesTranslator) super.clone();
|
36 |
clone.translatedNames = this.translatedNames.clone();
|
37 |
return clone;
|
38 |
} |
39 |
|
40 |
@Override
|
41 |
protected void build() { |
42 |
this.translatedNames = new SimpleList(); |
43 |
if (!sourceNames.hasUniqueValues()) {
|
44 |
throw new IllegalArgumentException("Source names contains duplicates."); |
45 |
} |
46 |
|
47 |
this.hasTranslations = false; |
48 |
for (String sourceName : this.sourceNames) { |
49 |
String translatedName = this.getSuggestion(sourceName); |
50 |
this.translatedNames.add(translatedName);
|
51 |
if (!sourceName.equals(translatedName)) {
|
52 |
this.hasTranslations = true; |
53 |
} |
54 |
} |
55 |
this.source2translation = null; |
56 |
this.translation2source = null; |
57 |
} |
58 |
|
59 |
@Override
|
60 |
public List<String> getTranslatedNames() { |
61 |
return this.translatedNames.toList(); |
62 |
} |
63 |
|
64 |
private void createSource2TranslationMap() { |
65 |
this.source2translation = new SimpleMap(); |
66 |
for (int i = 0; i < this.sourceNames.size(); i++) { |
67 |
if (!this.source2translation.toMap().containsKey(this.sourceNames.get(i))) { |
68 |
this.source2translation.put(this.sourceNames.get(i), this.translatedNames.get(i)); |
69 |
} |
70 |
} |
71 |
} |
72 |
|
73 |
@Override
|
74 |
public String getTranslation(String sourceName) { |
75 |
if (this.hasTranslations) { |
76 |
if (this.source2translation == null) { |
77 |
createSource2TranslationMap(); |
78 |
} |
79 |
return this.source2translation.get(sourceName); |
80 |
} |
81 |
return sourceName;
|
82 |
} |
83 |
|
84 |
private void createTranslation2SourceMap() { |
85 |
this.translation2source = new SimpleMap(); |
86 |
for (int i = 0; i < this.sourceNames.size(); i++) { |
87 |
if (!this.translation2source.toMap().containsKey(this.translatedNames.get(i))) { |
88 |
this.translation2source.put(this.translatedNames.get(i), this.sourceNames.get(i)); |
89 |
} |
90 |
} |
91 |
} |
92 |
|
93 |
@Override
|
94 |
public String getSource(String translatedName) { |
95 |
if (this.hasTranslations) { |
96 |
if (this.translation2source == null) { |
97 |
createTranslation2SourceMap(); |
98 |
} |
99 |
return this.translation2source.get(translatedName); |
100 |
} |
101 |
return translatedName;
|
102 |
} |
103 |
|
104 |
@Override
|
105 |
public String getTranslation(int index) { |
106 |
return this.translatedNames.get(index); |
107 |
} |
108 |
|
109 |
@Override
|
110 |
public int addSource(String sourceName) { |
111 |
this.sourceNames.add(sourceName);
|
112 |
String sugName = getSuggestion(sourceName);
|
113 |
this.translatedNames.add(sugName);
|
114 |
if (!StringUtils.equalsAnyIgnoreCase(sourceName, sugName)) {
|
115 |
this.hasTranslations = true; |
116 |
} |
117 |
this.source2translation = null; |
118 |
this.translation2source = null; |
119 |
return this.sourceNames.size() - 1; |
120 |
} |
121 |
|
122 |
@Override
|
123 |
public int setTranslation(String sourceName, String translatedName) { |
124 |
if (StringUtils.isBlank(sourceName)) {
|
125 |
throw new IllegalArgumentException("Source name not valid (null or empty string)."); |
126 |
} |
127 |
if (StringUtils.isBlank(translatedName)) {
|
128 |
throw new IllegalArgumentException("Translated name not valid (null or empty string)."); |
129 |
} |
130 |
int translationToSet = this.sourceNames.indexOf(sourceName); |
131 |
int translation = this.setTranslation(translationToSet, translatedName); |
132 |
return translation;
|
133 |
} |
134 |
|
135 |
@Override
|
136 |
public int setTranslation(int sourcePosition, String translatedName) { |
137 |
|
138 |
int translationToSet = sourcePosition;
|
139 |
String sourceName = this.getSource(sourcePosition); |
140 |
if (translationToSet < 0) { |
141 |
throw new IllegalArgumentException("Don't exist name '" + sourceName + "'."); |
142 |
} |
143 |
|
144 |
int translationToFix = -1; |
145 |
for (int i = 0; i < translatedNames.size(); i++) { |
146 |
if (i == translationToSet) {
|
147 |
continue;
|
148 |
} |
149 |
if (StringUtils.equalsIgnoreCase(translatedName, (this.translatedNames.get(i)))) { |
150 |
translationToFix = i; |
151 |
break;
|
152 |
} |
153 |
} |
154 |
this.translatedNames.set(translationToSet, translatedName);
|
155 |
if (!this.hasTranslations) { |
156 |
this.hasTranslations = !StringUtils.equalsIgnoreCase(this.translatedNames.get(translationToSet), this.sourceNames.get(translationToSet)); |
157 |
} |
158 |
if (translationToFix >= 0) { |
159 |
this.translatedNames.set(translationToFix, this.getSuggestion(this.sourceNames.get(translationToFix))); |
160 |
if (!this.hasTranslations) { |
161 |
this.hasTranslations = StringUtils.equalsIgnoreCase(this.translatedNames.get(translationToFix), this.sourceNames.get(translationToFix)); |
162 |
} |
163 |
} |
164 |
|
165 |
this.source2translation = null; |
166 |
this.translation2source = null; |
167 |
return translationToFix;
|
168 |
} |
169 |
|
170 |
@Override
|
171 |
public String getSuggestion(String name) { |
172 |
String sourceName = name;
|
173 |
if (StringUtils.isBlank(sourceName)) {
|
174 |
sourceName = DEFAULT_FIELD_NAME; |
175 |
} |
176 |
if (!this.translatedNames.contains(sourceName)) { |
177 |
return sourceName;
|
178 |
} |
179 |
String translatedName;
|
180 |
for (int i = 0; i < 255; i++) { |
181 |
translatedName = sourceName + i; |
182 |
if (!this.translatedNames.contains(translatedName)) { |
183 |
return translatedName;
|
184 |
} |
185 |
} |
186 |
|
187 |
// Should not get here
|
188 |
return sourceName + "_" + (System.currentTimeMillis() % 1000000); |
189 |
} |
190 |
|
191 |
@Override
|
192 |
public boolean isValid(String name) { |
193 |
String sourceName = name;
|
194 |
if (StringUtils.isBlank(sourceName)) {
|
195 |
sourceName = DEFAULT_FIELD_NAME; |
196 |
} |
197 |
return !this.translatedNames.contains(sourceName); |
198 |
} |
199 |
|
200 |
public static void registerPersistence() { |
201 |
|
202 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
203 |
if (manager.getDefinition("BaseNamesTranslator") == null) { |
204 |
DynStruct definition = manager.addDefinition(BaseNamesTranslator.class, |
205 |
"BaseNamesTranslator", "BaseNamesTranslator persistence definition", null, null); |
206 |
definition.addDynFieldBoolean("hasTranslations");
|
207 |
definition.addDynFieldList("translatedNames");
|
208 |
definition.extend(manager.getDefinition("AbstractNamesTranslator"));
|
209 |
} |
210 |
} |
211 |
|
212 |
@Override
|
213 |
public void saveToState(PersistentState state) throws PersistenceException { |
214 |
super.saveToState(state);
|
215 |
state.set("hasTranslations", this.hasTranslations); |
216 |
state.set("translatedNames", this.translatedNames.toList()); |
217 |
} |
218 |
|
219 |
@Override
|
220 |
public void loadFromState(PersistentState state) throws PersistenceException { |
221 |
super.loadFromState(state);
|
222 |
|
223 |
this.hasTranslations = state.getBoolean("hasTranslations"); |
224 |
List translatedNamesState = state.getList("translatedNames"); |
225 |
SimpleList translatedNamesList = new SimpleList();
|
226 |
for (Object object : translatedNamesState) { |
227 |
translatedNamesList.add((String) object);
|
228 |
} |
229 |
this.translatedNames = translatedNamesList;
|
230 |
} |
231 |
|
232 |
@Override
|
233 |
public void rebuild() { |
234 |
this.build();
|
235 |
} |
236 |
|
237 |
@Override
|
238 |
public void updateSourceNames(String[] names) { |
239 |
List<String> asList = Arrays.asList(names); |
240 |
this.updateSourceNames(asList);
|
241 |
} |
242 |
|
243 |
@Override
|
244 |
public void updateSourceNames(Iterable<String> names) { |
245 |
SimpleList updateSources = new SimpleList();
|
246 |
for (String name : names) { |
247 |
updateSources.add(name); |
248 |
} |
249 |
if (this.sourceNames == null) { |
250 |
this.sourceNames = updateSources;
|
251 |
this.rebuild();
|
252 |
return;
|
253 |
} |
254 |
if(this.sourceNames.isEmpty()) { |
255 |
this.sourceNames = updateSources;
|
256 |
this.rebuild();
|
257 |
return;
|
258 |
} |
259 |
|
260 |
SimpleList oldSources; |
261 |
SimpleList oldTranslated; |
262 |
try {
|
263 |
oldSources = this.sourceNames.clone();
|
264 |
oldTranslated = this.translatedNames.clone();
|
265 |
} catch (CloneNotSupportedException ex) { |
266 |
throw new RuntimeException("Can't update names into the translator", ex); |
267 |
} |
268 |
|
269 |
this.sourceNames = updateSources;
|
270 |
|
271 |
this.rebuild();
|
272 |
|
273 |
for (int i = 0; i < oldSources.size(); i++) { |
274 |
String oldSource = oldSources.get(i);
|
275 |
if (this.sourceNames.contains(oldSource)) { |
276 |
this.setTranslation(oldSource, oldTranslated.get(i));
|
277 |
} |
278 |
} |
279 |
|
280 |
for (int i = 0; i < this.sourceNames.size(); i++) { |
281 |
String source = this.sourceNames.get(i); |
282 |
String target = this.translatedNames.get(i); |
283 |
if (!StringUtils.equalsIgnoreCase(source, target)) {
|
284 |
this.hasTranslations = true; |
285 |
break;
|
286 |
} |
287 |
} |
288 |
|
289 |
this.translation2source = null; |
290 |
this.source2translation = null; |
291 |
} |
292 |
|
293 |
@Override
|
294 |
public void updateSourceNames(Iterable objs, Function<Object, String> name_getter) { |
295 |
SimpleList toUpdate = new SimpleList();
|
296 |
for (Object obj : objs) { |
297 |
String name = name_getter.apply(obj);
|
298 |
toUpdate.add(name); |
299 |
} |
300 |
this.updateSourceNames(toUpdate.toList());
|
301 |
} |
302 |
|
303 |
@Override
|
304 |
public List<String> getAllTranslations(String sourceName) { |
305 |
ArrayList toList = new ArrayList(); |
306 |
if (this.hasTranslations) { |
307 |
for (int i = 0; i < this.sourceNames.size(); i++) { |
308 |
String translatorSource = this.sourceNames.get(i); |
309 |
if (translatorSource.equalsIgnoreCase(sourceName)) {
|
310 |
toList.add(this.translatedNames.get(i));
|
311 |
} |
312 |
} |
313 |
} else {
|
314 |
toList.add(this.getTranslation(sourceName));
|
315 |
} |
316 |
return toList;
|
317 |
} |
318 |
|
319 |
@Override
|
320 |
public void clear() { |
321 |
this.sourceNames = new SimpleList(); |
322 |
this.translatedNames = new SimpleList(); |
323 |
this.source2translation = null; |
324 |
this.translation2source = null; |
325 |
this.hasTranslations = false; |
326 |
} |
327 |
|
328 |
@Override
|
329 |
public boolean hasDuplicateSources() { |
330 |
if (this.source2translation == null) { |
331 |
createSource2TranslationMap(); |
332 |
} |
333 |
return this.source2translation.size() != this.sourceNames.size(); |
334 |
} |
335 |
|
336 |
} |