gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / namestranslator / AbstractNamesTranslator.java @ 2217
History | View | Annotate | Download (6.92 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 |
public int size() { |
71 |
return this.map.size(); |
72 |
} |
73 |
|
74 |
@Override
|
75 |
public SimpleMap clone() throws CloneNotSupportedException { |
76 |
SimpleMap clone = (SimpleMap) super.clone();
|
77 |
clone.map = new HashMap<>(this.map); |
78 |
return clone;
|
79 |
} |
80 |
|
81 |
} |
82 |
|
83 |
protected static class SimpleList implements Iterable<String>, Cloneable { |
84 |
|
85 |
public List<String> list; |
86 |
|
87 |
public SimpleList() {
|
88 |
this.list = new ArrayList<>(); |
89 |
} |
90 |
|
91 |
@Override
|
92 |
public SimpleList clone() throws CloneNotSupportedException { |
93 |
SimpleList clone = (SimpleList) super.clone();
|
94 |
clone.list = new ArrayList<>(list); |
95 |
return clone;
|
96 |
} |
97 |
|
98 |
public void add(String s) { |
99 |
list.add(s); |
100 |
} |
101 |
|
102 |
public boolean contains(String s) { |
103 |
for (String listElement : list) { |
104 |
if (StringUtils.equalsIgnoreCase(s, listElement)) {
|
105 |
return true; |
106 |
} |
107 |
} |
108 |
return false; |
109 |
} |
110 |
|
111 |
public int indexOf(String s) { |
112 |
for (int i = 0; i < list.size(); i++) { |
113 |
String element = list.get(i);
|
114 |
if (StringUtils.equalsIgnoreCase(s, element)) {
|
115 |
return i;
|
116 |
} |
117 |
} |
118 |
return -1; |
119 |
|
120 |
} |
121 |
|
122 |
public int size() { |
123 |
return list.size();
|
124 |
|
125 |
} |
126 |
|
127 |
public String get(int index) { |
128 |
return list.get(index);
|
129 |
|
130 |
} |
131 |
|
132 |
public void set(int index, String s) { |
133 |
list.set(index, s); |
134 |
} |
135 |
|
136 |
@Override
|
137 |
public Iterator<String> iterator() { |
138 |
return list.iterator();
|
139 |
|
140 |
} |
141 |
|
142 |
public boolean isEmpty() { |
143 |
return this.list.isEmpty(); |
144 |
} |
145 |
|
146 |
public List<String> toList() { |
147 |
return list;
|
148 |
} |
149 |
|
150 |
public boolean hasUniqueValues() { |
151 |
Set<String> sourceNamesSet = new HashSet<>(); |
152 |
for (String sourceName : this.list) { |
153 |
sourceNamesSet.add(sourceName.toLowerCase()); |
154 |
} |
155 |
return sourceNamesSet.size() == this.list.size(); |
156 |
} |
157 |
} |
158 |
|
159 |
protected SimpleList sourceNames;
|
160 |
|
161 |
protected AbstractNamesTranslator() {
|
162 |
|
163 |
} |
164 |
|
165 |
@Override
|
166 |
public NamesTranslator clone() throws CloneNotSupportedException { |
167 |
AbstractNamesTranslator clone = (AbstractNamesTranslator) super.clone();
|
168 |
clone.sourceNames = this.sourceNames.clone();
|
169 |
return clone;
|
170 |
} |
171 |
|
172 |
@Override
|
173 |
public List<String> getSourceNames() { |
174 |
return this.sourceNames.toList(); |
175 |
} |
176 |
|
177 |
@Override
|
178 |
public void setSourceNames(Iterable<String> names) { |
179 |
this.sourceNames = new SimpleList(); |
180 |
for (String name : names) { |
181 |
this.sourceNames.add(name);
|
182 |
} |
183 |
this.build();
|
184 |
} |
185 |
|
186 |
@Override
|
187 |
public void setSourceNames(String[] names) { |
188 |
List<String> asList = Arrays.asList(names); |
189 |
this.setSourceNames(asList);
|
190 |
} |
191 |
|
192 |
@Override
|
193 |
public void setSourceNames(Iterable objs, Function<Object, String> name_getter) { |
194 |
this.sourceNames = new SimpleList(); |
195 |
for (Object obj : objs) { |
196 |
String name = name_getter.apply(obj);
|
197 |
this.sourceNames.add(name);
|
198 |
} |
199 |
this.build();
|
200 |
} |
201 |
|
202 |
@Override
|
203 |
public String[] getTranslatedNamesAsArray() { |
204 |
List<String> l = this.getTranslatedNames(); |
205 |
return l.toArray(new String[l.size()]); |
206 |
} |
207 |
|
208 |
@Override
|
209 |
public String getSource(int index) { |
210 |
return this.sourceNames.get(index); |
211 |
} |
212 |
|
213 |
@Override
|
214 |
public boolean isValid(String name) { |
215 |
return StringUtils.equalsIgnoreCase(name, this.getSuggestion(name)); |
216 |
} |
217 |
|
218 |
protected abstract void build(); |
219 |
|
220 |
|
221 |
public static void registerPersistence() { |
222 |
|
223 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
224 |
if (manager.getDefinition("ClassName") == null) { |
225 |
DynStruct definition = manager.addDefinition(AbstractNamesTranslator.class, |
226 |
"AbstractNamesTranslator", "AbstractNamesTranslator persistence definition", null, null); |
227 |
definition.addDynFieldList("sourceNames").setClassOfItems(String.class |
228 |
); |
229 |
} |
230 |
} |
231 |
|
232 |
@Override
|
233 |
public void saveToState(PersistentState state) throws PersistenceException { |
234 |
state.set("sourceNames", this.sourceNames.toList()); |
235 |
} |
236 |
|
237 |
@Override
|
238 |
public void loadFromState(PersistentState state) throws PersistenceException { |
239 |
List sourceNamesState = state.getList("sourceNames"); |
240 |
|
241 |
SimpleList sourceNamesList = new SimpleList();
|
242 |
for (Object object : sourceNamesState) { |
243 |
sourceNamesList.add((String) object);
|
244 |
} |
245 |
this.sourceNames = sourceNamesList;
|
246 |
} |
247 |
} |