gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / namestranslator / BaseNamesTranslator.java @ 2207
History | View | Annotate | Download (9.46 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 |
public int addSource(String sourceName) { |
95 |
if (this.sourceNames.contains(sourceName)) { |
96 |
throw new IllegalArgumentException("Source name already exists in the name translator"); |
97 |
} |
98 |
String sugName = getSuggestion(sourceName);
|
99 |
return this.setTranslation(sourceName, sugName); |
100 |
} |
101 |
|
102 |
@Override
|
103 |
public int setTranslation(String sourceName, String translatedName) { |
104 |
if (StringUtils.isBlank(sourceName)) {
|
105 |
throw new IllegalArgumentException("Source name not valid (null or empty string)."); |
106 |
} |
107 |
if (StringUtils.isBlank(translatedName)) {
|
108 |
throw new IllegalArgumentException("Translated name not valid (null or empty string)."); |
109 |
} |
110 |
int translationToSet = this.sourceNames.indexOf(sourceName); |
111 |
if (translationToSet < 0) { |
112 |
throw new IllegalArgumentException("Don't exist name '" + sourceName + "'."); |
113 |
} |
114 |
|
115 |
int translationToFix = -1; |
116 |
for (int i = 0; i < translatedNames.size(); i++) { |
117 |
if (i == translationToSet) {
|
118 |
continue;
|
119 |
} |
120 |
if (StringUtils.equalsIgnoreCase(translatedName, (this.translatedNames.get(i)))) { |
121 |
translationToFix = i; |
122 |
break;
|
123 |
} |
124 |
} |
125 |
this.translatedNames.set(translationToSet, translatedName);
|
126 |
if (!this.hasTranslations) { |
127 |
this.hasTranslations = !StringUtils.equalsIgnoreCase(this.translatedNames.get(translationToSet), this.sourceNames.get(translationToSet)); |
128 |
} |
129 |
if (translationToFix >= 0) { |
130 |
this.translatedNames.set(translationToFix, this.getSuggestion(this.sourceNames.get(translationToFix))); |
131 |
if (!this.hasTranslations) { |
132 |
this.hasTranslations = StringUtils.equalsIgnoreCase(this.translatedNames.get(translationToFix), this.sourceNames.get(translationToFix)); |
133 |
} |
134 |
} |
135 |
|
136 |
this.source2translation = null; |
137 |
this.translation2source = null; |
138 |
return translationToFix;
|
139 |
} |
140 |
|
141 |
@Override
|
142 |
public String getSuggestion(String name) { |
143 |
String sourceName = name;
|
144 |
if (StringUtils.isBlank(sourceName)) {
|
145 |
sourceName = DEFAULT_FIELD_NAME; |
146 |
} |
147 |
int len = sourceName.length();
|
148 |
if (!this.translatedNames.contains(sourceName)) { |
149 |
return sourceName;
|
150 |
} |
151 |
String translatedName = sourceName;
|
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 |
/*
|
160 |
* Should not get here
|
161 |
*/
|
162 |
return sourceName + "_" + (System.currentTimeMillis() % 1000000); |
163 |
} |
164 |
|
165 |
@Override
|
166 |
public boolean isValid(String name) { |
167 |
String sourceName = name;
|
168 |
if (StringUtils.isBlank(sourceName)) {
|
169 |
sourceName = DEFAULT_FIELD_NAME; |
170 |
} |
171 |
int len = sourceName.length();
|
172 |
return !this.translatedNames.contains(sourceName); |
173 |
} |
174 |
|
175 |
public static void registerPersistence() { |
176 |
|
177 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
178 |
if (manager.getDefinition("ClassName") == null) { |
179 |
DynStruct definition = manager.addDefinition(BaseNamesTranslator.class, |
180 |
"BaseNamesTranslator", "BaseNamesTranslator persistence definition", null, null); |
181 |
definition.addDynFieldBoolean("hasTranslations");
|
182 |
definition.addDynFieldList("translatedNames");
|
183 |
definition.extend(manager.getDefinition("AbstractNamesTranslator"));
|
184 |
} |
185 |
} |
186 |
|
187 |
@Override
|
188 |
public void saveToState(PersistentState state) throws PersistenceException { |
189 |
super.saveToState(state);
|
190 |
state.set("hasTranslations", this.hasTranslations); |
191 |
state.set("translatedNames", this.translatedNames.toList()); |
192 |
} |
193 |
|
194 |
@Override
|
195 |
public void loadFromState(PersistentState state) throws PersistenceException { |
196 |
super.loadFromState(state);
|
197 |
|
198 |
this.hasTranslations = state.getBoolean("hasTranslations"); |
199 |
List translatedNamesState = state.getList("translatedNames"); |
200 |
SimpleList translatedNamesList = new SimpleList();
|
201 |
for (Object object : translatedNamesState) { |
202 |
translatedNamesList.add((String) object);
|
203 |
} |
204 |
this.translatedNames = translatedNamesList;
|
205 |
} |
206 |
|
207 |
@Override
|
208 |
public void rebuild() { |
209 |
this.build();
|
210 |
} |
211 |
|
212 |
@Override
|
213 |
public void updateSourceNames(String[] names) { |
214 |
List<String> asList = Arrays.asList(names); |
215 |
this.updateSourceNames(asList);
|
216 |
} |
217 |
|
218 |
@Override
|
219 |
public void updateSourceNames(Iterable<String> names) { |
220 |
SimpleList updateSources = new SimpleList();
|
221 |
for (String name : names) { |
222 |
updateSources.add(name); |
223 |
} |
224 |
if(this.sourceNames==null) { |
225 |
this.sourceNames = updateSources;
|
226 |
this.rebuild();
|
227 |
return;
|
228 |
} |
229 |
if(this.sourceNames.toList().isEmpty()) { |
230 |
this.sourceNames = updateSources;
|
231 |
this.rebuild();
|
232 |
return;
|
233 |
} |
234 |
|
235 |
SimpleList oldSources; |
236 |
SimpleList oldTranslated; |
237 |
try {
|
238 |
oldSources = this.sourceNames.clone();
|
239 |
oldTranslated = this.translatedNames.clone();
|
240 |
} catch (CloneNotSupportedException ex) { |
241 |
throw new RuntimeException("Can't update names into the translator", ex); |
242 |
} |
243 |
|
244 |
this.sourceNames = updateSources;
|
245 |
|
246 |
this.rebuild();
|
247 |
|
248 |
for (int i = 0; i < oldSources.size(); i++) { |
249 |
String oldSource = oldSources.get(i);
|
250 |
if(this.sourceNames.contains(oldSource)) { |
251 |
this.setTranslation(oldSource, oldTranslated.get(i));
|
252 |
} |
253 |
} |
254 |
|
255 |
for (int i = 0; i < this.sourceNames.size(); i++) { |
256 |
String source = this.sourceNames.get(i); |
257 |
String target = this.translatedNames.get(i); |
258 |
if (!StringUtils.equalsIgnoreCase(source, target)) {
|
259 |
this.hasTranslations = true; |
260 |
break;
|
261 |
} |
262 |
} |
263 |
|
264 |
this.translation2source = null; |
265 |
this.source2translation = null; |
266 |
} |
267 |
|
268 |
@Override
|
269 |
public void updateSourceNames(Iterable objs, Function<Object, String> name_getter) { |
270 |
SimpleList toUpdate = new SimpleList();
|
271 |
for (Object obj : objs) { |
272 |
String name = name_getter.apply(obj);
|
273 |
toUpdate.add(name); |
274 |
} |
275 |
this.updateSourceNames(toUpdate.toList());
|
276 |
} |
277 |
|
278 |
} |