Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / namestranslator / AbstractNamesTranslator.java @ 2212

History | View | Annotate | Download (6.84 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2020 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA 02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.tools.namestranslator;
25

    
26
import java.util.ArrayList;
27
import java.util.Arrays;
28
import java.util.HashMap;
29
import java.util.HashSet;
30
import java.util.Iterator;
31
import java.util.List;
32
import java.util.Map;
33
import java.util.Set;
34
import java.util.function.Function;
35
import org.apache.commons.lang3.StringUtils;
36
import org.gvsig.tools.ToolsLocator;
37
import org.gvsig.tools.dynobject.DynStruct;
38
import org.gvsig.tools.persistence.PersistenceManager;
39
import org.gvsig.tools.persistence.Persistent;
40
import org.gvsig.tools.persistence.PersistentState;
41
import org.gvsig.tools.persistence.exception.PersistenceException;
42
import org.gvsig.tools.lang.Cloneable;
43

    
44
/**
45
 *
46
 * @author jjdelcerro
47
 */
48
public abstract class AbstractNamesTranslator implements NamesTranslator, Persistent {
49

    
50
    protected static class SimpleMap implements Cloneable {
51

    
52
        public Map<String, String> map;
53

    
54
        public SimpleMap() {
55
            this.map = new HashMap<>();
56
        }
57

    
58
        public String get(String name) {
59
            return this.map.get(name.toLowerCase());
60
        }
61

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

    
66
        public Map<String, String> toMap() {
67
            return this.map;
68
        }
69

    
70
        @Override
71
        public SimpleMap clone() throws CloneNotSupportedException {
72
            SimpleMap clone = (SimpleMap) super.clone();
73
            clone.map = new HashMap<>(this.map);
74
            return clone;
75
        }
76

    
77
    }
78

    
79
    protected static class SimpleList implements Iterable<String>, Cloneable {
80

    
81
        public List<String> list;
82

    
83
        public SimpleList() {
84
            this.list = new ArrayList<>();
85
        }
86

    
87
        @Override
88
        public SimpleList clone() throws CloneNotSupportedException {
89
            SimpleList clone = (SimpleList) super.clone();
90
            clone.list = new ArrayList<>(list);
91
            return clone;
92
        }
93

    
94
        public void add(String s) {
95
            list.add(s);
96
        }
97

    
98
        public boolean contains(String s) {
99
            for (String listElement : list) {
100
                if (StringUtils.equalsIgnoreCase(s, listElement)) {
101
                    return true;
102
                }
103
            }
104
            return false;
105
        }
106

    
107
        public int indexOf(String s) {
108
            for (int i = 0; i < list.size(); i++) {
109
                String element = list.get(i);
110
                if (StringUtils.equalsIgnoreCase(s, element)) {
111
                    return i;
112
                }
113
            }
114
            return -1;
115

    
116
        }
117

    
118
        public int size() {
119
            return list.size();
120

    
121
        }
122

    
123
        public String get(int index) {
124
            return list.get(index);
125

    
126
        }
127

    
128
        public void set(int index, String s) {
129
            list.set(index, s);
130
        }
131

    
132
        @Override
133
        public Iterator<String> iterator() {
134
            return list.iterator();
135

    
136
        }
137
        
138
        public boolean isEmpty() {
139
          return this.list.isEmpty();
140
        }
141

    
142
        public List<String> toList() {
143
            return list;
144
        }
145

    
146
        public boolean hasUniqueValues() {
147
            Set<String> sourceNamesSet = new HashSet<>();
148
            for (String sourceName : this.list) {
149
                sourceNamesSet.add(sourceName.toLowerCase());
150
            }
151
            return sourceNamesSet.size() == this.list.size();
152
        }
153
    }
154

    
155
    protected SimpleList sourceNames;
156

    
157
    protected AbstractNamesTranslator() {
158

    
159
    }
160

    
161
    @Override
162
    public NamesTranslator clone() throws CloneNotSupportedException {
163
        AbstractNamesTranslator clone = (AbstractNamesTranslator) super.clone();
164
        clone.sourceNames = this.sourceNames.clone();
165
        return clone;
166
    }
167

    
168
    @Override
169
    public List<String> getSourceNames() {
170
        return this.sourceNames.toList();
171
    }
172

    
173
    @Override
174
    public void setSourceNames(Iterable<String> names) {
175
        this.sourceNames = new SimpleList();
176
        for (String name : names) {
177
            this.sourceNames.add(name);
178
        }
179
        this.build();
180
    }
181

    
182
    @Override
183
    public void setSourceNames(String[] names) {
184
        List<String> asList = Arrays.asList(names);
185
        this.setSourceNames(asList);
186
    }
187

    
188
    @Override
189
    public void setSourceNames(Iterable objs, Function<Object, String> name_getter) {
190
        this.sourceNames = new SimpleList();
191
        for (Object obj : objs) {
192
            String name = name_getter.apply(obj);
193
            this.sourceNames.add(name);
194
        }
195
        this.build();
196
    }
197

    
198
    @Override
199
    public String[] getTranslatedNamesAsArray() {
200
        List<String> l = this.getTranslatedNames();
201
        return l.toArray(new String[l.size()]);
202
    }
203

    
204
    @Override
205
    public String getSource(int index) {
206
        return this.sourceNames.get(index);
207
    }
208

    
209
    @Override
210
    public boolean isValid(String name) {
211
        return StringUtils.equalsIgnoreCase(name, this.getSuggestion(name));
212
    }
213

    
214
    protected abstract void build();
215
    
216

    
217
    public static void registerPersistence() {
218

    
219
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
220
        if (manager.getDefinition("ClassName") == null) {
221
            DynStruct definition = manager.addDefinition(AbstractNamesTranslator.class,
222
                     "AbstractNamesTranslator", "AbstractNamesTranslator persistence definition", null, null);
223
            definition.addDynFieldList("sourceNames").setClassOfItems(String.class
224
            );
225
        }
226
    }
227

    
228
    @Override
229
    public void saveToState(PersistentState state) throws PersistenceException {
230
        state.set("sourceNames", this.sourceNames.toList());
231
    }
232

    
233
    @Override
234
    public void loadFromState(PersistentState state) throws PersistenceException {
235
        List sourceNamesState = state.getList("sourceNames");
236

    
237
        SimpleList sourceNamesList = new SimpleList();
238
        for (Object object : sourceNamesState) {
239
            sourceNamesList.add((String) object);
240
        }
241
        this.sourceNames = sourceNamesList;
242
    }
243
}