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