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