Revision 2217

View differences:

org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/namestranslator/AbstractNamesTranslator.java
60 60
        }
61 61

  
62 62
        public void put(String name, String element) {
63
            this.map.put(name.toLowerCase(), element);
63
                this.map.put(name.toLowerCase(), element);
64 64
        }
65 65

  
66 66
        public Map<String, String> toMap() {
67 67
            return this.map;
68 68
        }
69
        
70
        public int size() {
71
            return this.map.size();
72
        }
69 73

  
70 74
        @Override
71 75
        public SimpleMap clone() throws CloneNotSupportedException {
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/namestranslator/BaseNamesTranslator.java
1 1
package org.gvsig.tools.namestranslator;
2 2

  
3
import java.util.ArrayList;
3 4
import java.util.Arrays;
4 5
import java.util.List;
5 6
import java.util.function.Function;
......
58 59
        return this.translatedNames.toList();
59 60
    }
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

  
61 71
    @Override
62 72
    public String getTranslation(String sourceName) {
63 73
        if (this.hasTranslations) {
64 74
            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
                }
75
                createSource2TranslationMap();
69 76
            }
70 77
            return this.source2translation.get(sourceName);
71 78
        }
72 79
        return sourceName;
73 80
    }
74 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

  
75 91
    @Override
76 92
    public String getSource(String translatedName) {
77 93
        if (this.hasTranslations) {
78 94
            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
                }
95
                createTranslation2SourceMap();
83 96
            }
84 97
            return this.translation2source.get(translatedName);
85 98
        }
......
93 106

  
94 107
    @Override
95 108
    public int addSource(String sourceName) {
96
        if (this.sourceNames.contains(sourceName)) {
97
            throw new IllegalArgumentException("Source name already exists in the name translator");
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;
98 114
        }
99
        String sugName = getSuggestion(sourceName);
100
        return this.setTranslation(sourceName, sugName);
115
        this.source2translation = null;
116
        this.translation2source = null;
117
        return this.sourceNames.size() - 1;
101 118
    }
102 119

  
103 120
    @Override
......
218 235
        for (String name : names) {
219 236
            updateSources.add(name);
220 237
        }
221
        if(this.sourceNames==null) {
238
        if (this.sourceNames == null) {
222 239
            this.sourceNames = updateSources;
223 240
            this.rebuild();
224 241
            return;
......
237 254
        } catch (CloneNotSupportedException ex) {
238 255
            throw new RuntimeException("Can't update names into the translator", ex);
239 256
        }
240
        
257

  
241 258
        this.sourceNames = updateSources;
242
        
259

  
243 260
        this.rebuild();
244
        
261

  
245 262
        for (int i = 0; i < oldSources.size(); i++) {
246 263
            String oldSource = oldSources.get(i);
247
            if(this.sourceNames.contains(oldSource)) {
264
            if (this.sourceNames.contains(oldSource)) {
248 265
                this.setTranslation(oldSource, oldTranslated.get(i));
249 266
            }
250 267
        }
251
        
268

  
252 269
        for (int i = 0; i < this.sourceNames.size(); i++) {
253 270
            String source = this.sourceNames.get(i);
254 271
            String target = this.translatedNames.get(i);
......
257 274
                break;
258 275
            }
259 276
        }
260
        
277

  
261 278
        this.translation2source = null;
262 279
        this.source2translation = null;
263 280
    }
......
272 289
        this.updateSourceNames(toUpdate.toList());
273 290
    }
274 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

  
275 325
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/namestranslator/DummyNamesTranslator.java
24 24

  
25 25
package org.gvsig.tools.namestranslator;
26 26

  
27
import java.util.ArrayList;
27 28
import java.util.List;
28 29
import java.util.function.Function;
29 30

  
......
122 123
    public void updateSourceNames(Iterable objs, Function<Object, String> name_getter) {
123 124
        
124 125
    }
126

  
127
    @Override
128
    public List<String> getAllTranslations(String sourceName) {
129
        return new ArrayList();
130
    }
131

  
132
    @Override
133
    public void clear() {
134
        
135
    }
136

  
137
    @Override
138
    public boolean hasDuplicateSources() {
139
        return false;
140
    }
125 141
  
126 142
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/namestranslator/TrimNamesTranslator.java
38 38
        extends BaseNamesTranslator {
39 39

  
40 40
    protected int maxNameLen;
41
    
41

  
42 42
    public TrimNamesTranslator() {
43
        
43

  
44 44
    }
45 45

  
46 46
    protected TrimNamesTranslator(int maxNameLen) {
......
49 49
        }
50 50
        this.maxNameLen = maxNameLen;
51 51
    }
52
    
52

  
53 53
    @Override
54
     public NamesTranslator clone() throws CloneNotSupportedException {
54
    public NamesTranslator clone() throws CloneNotSupportedException {
55 55
        TrimNamesTranslator clone = (TrimNamesTranslator) super.clone();
56 56
        return clone;
57
     }
57
    }
58 58

  
59 59
    @Override
60 60
    public boolean isValid(String name) {
......
126 126
        translatedName = StringUtils.left(translatedName, this.maxNameLen);
127 127
        return super.setTranslation(sourceName, translatedName);
128 128
    }
129
    
130
        public static void registerPersistence() {
129

  
130
    public static void registerPersistence() {
131 131
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
132 132
        if (manager.getDefinition("TrimNamesTranslator") == null) {
133 133
            DynStruct definition = manager.addDefinition(TrimNamesTranslator.class,
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/namestranslator/NamesTranslator.java
173 173
  public void updateSourceNames(Iterable<String> names);
174 174

  
175 175
  public void updateSourceNames(Iterable objs, Function<Object, String> name_getter);
176
  
177
  public List<String> getAllTranslations(String sourceName);
178
  
179
  public void clear();
180
  
181
  public boolean hasDuplicateSources();
176 182

  
177 183
}

Also available in: Unified diff