1 |
package skrueger.geotools; |
package skrueger.geotools; |
2 |
|
|
3 |
|
import java.util.ArrayList; |
4 |
|
import java.util.Collections; |
5 |
|
import java.util.LinkedHashSet; |
6 |
|
import java.util.List; |
7 |
|
import java.util.Map; |
8 |
import java.util.TreeMap; |
import java.util.TreeMap; |
9 |
|
import java.util.TreeSet; |
10 |
|
|
11 |
import skrueger.AttributeMetaData; |
import org.apache.log4j.Logger; |
12 |
|
import org.geotools.feature.NameImpl; |
13 |
|
import org.opengis.feature.type.Name; |
14 |
|
|
15 |
|
import skrueger.AttributeMetadataInterface; |
16 |
|
import skrueger.QualityQuantizable; |
17 |
|
import skrueger.i8n.Translation; |
18 |
|
|
19 |
/** |
/** |
20 |
* An extension of TreeMap, that is clonable in the sense of |
* The {@link AttributeMetadataMap} is a {@link Map} holding {@link AMD_IMPL} |
21 |
* |
* object for {@link Name} keys. It's an extension of a {@link TreeMap}. It's |
22 |
* @author stefan |
* copyable in the sense of the {@link Copyable} interface.<br> |
23 |
* |
* The {@link #get(Name)} and {@link #get(String)} methods will never return |
24 |
|
* <code>null</code>, but rather create a default {@link AMD_IMPL} on-the-fly. |
25 |
*/ |
*/ |
26 |
public class AttributeMetadataMap extends TreeMap<Integer, AttributeMetaData> |
public abstract class AttributeMetadataMap<AMD_IMPL extends AttributeMetadataInterface> |
27 |
implements Copyable<AttributeMetadataMap> { |
extends TreeMap<Name, AMD_IMPL> implements |
28 |
|
Copyable<AttributeMetadataMap>, QualityQuantizable { |
29 |
|
|
30 |
|
private static final long serialVersionUID = 4936966916517396063L; |
31 |
|
|
32 |
|
protected static final Logger LOGGER = Logger |
33 |
|
.getLogger(AttributeMetadataMap.class); |
34 |
|
|
35 |
|
/** |
36 |
|
* A list of default languages used when creating default {@link AMD_IMPL} |
37 |
|
* on-the-fly. If not initialized by a constructor, it will be |
38 |
|
* {@link Translation#getActiveLang()} only. |
39 |
|
**/ |
40 |
|
protected final List<String> langs; |
41 |
|
|
42 |
|
/** |
43 |
|
* AttributeMetadataMap is based on {@link Translation} and would like to |
44 |
|
* know how many languages are supported, so the needed defaults can be |
45 |
|
* created.<br/> |
46 |
|
* If you use this constructor, it will be initialized to only use one |
47 |
|
* language, which is {@link Translation.getActiveLang()}. |
48 |
|
*/ |
49 |
public AttributeMetadataMap() { |
public AttributeMetadataMap() { |
50 |
|
langs = new ArrayList<String>(); |
51 |
|
langs.add(Translation.getActiveLang()); |
52 |
|
} |
53 |
|
|
54 |
|
/** |
55 |
|
* Creates an {@link AttributeMetadataMap} and sets the list of default |
56 |
|
* languages. |
57 |
|
*/ |
58 |
|
public AttributeMetadataMap(final List<String> defLanguages) { |
59 |
|
langs = defLanguages; |
60 |
} |
} |
61 |
|
|
62 |
|
/** |
63 |
|
* Creates an {@link AttributeMetadataMap} and sets the list of default |
64 |
|
* languages. |
65 |
|
*/ |
66 |
|
public AttributeMetadataMap(final String[] langs) { |
67 |
|
this.langs = new ArrayList<String>(java.util.Arrays.asList(langs)); |
68 |
|
} |
69 |
|
|
70 |
|
/** |
71 |
|
* Copies all its values to another {@link AttributeMetadataMap}. @see |
72 |
|
* {@link Copyable} interface. |
73 |
|
*/ |
74 |
@Override |
@Override |
75 |
public AttributeMetadataMap copyTo(AttributeMetadataMap amdMap) { |
public AttributeMetadataMap<? extends AttributeMetadataInterface> copyTo(AttributeMetadataMap amdMap) { |
76 |
|
|
77 |
amdMap.clear(); |
amdMap.clear(); |
78 |
|
|
79 |
for (Integer key : keySet()) { |
for (final Name key : keySet()) { |
80 |
AttributeMetaData attributeMetaData = get(key); |
final AMD_IMPL AMD_IMPL = get(key); |
81 |
amdMap.put(key, attributeMetaData.copy()); |
amdMap.put(key, AMD_IMPL.copy()); |
82 |
} |
} |
83 |
return amdMap; |
return amdMap; |
84 |
} |
} |
85 |
|
|
86 |
|
/** |
87 |
|
* Returns the {@link AMD_IMPL} for a given {@link NameImpl}. May return |
88 |
|
* <code>null</code> or create a default {@link AttributeMetadataInterface} depending |
89 |
|
* on whether this method is overwritten. fly. |
90 |
|
*/ |
91 |
|
public AMD_IMPL get(final Name name) { |
92 |
|
return super.get(name); |
93 |
|
} |
94 |
|
|
95 |
|
/** |
96 |
|
* @Deprecated use get(Name name) or get(String localName) |
97 |
|
*/ |
98 |
|
@Deprecated |
99 |
|
@Override |
100 |
|
public AMD_IMPL get(final Object key) { |
101 |
|
LOGGER.error("PLEASE DONT USE get(Object) any MORE!"); |
102 |
|
return super.get(key); |
103 |
|
} |
104 |
|
|
105 |
|
/** |
106 |
|
* Returns the {@link AMD_IMPL} for a given {@link Name}. Never returns |
107 |
|
* <code>null</code>, but rather creates a default {@link AMD_IMPL} on the |
108 |
|
* fly. |
109 |
|
*/ |
110 |
|
public AMD_IMPL get(final String localName) { |
111 |
|
if (localName == null) |
112 |
|
return null; |
113 |
|
return this.get(new NameImpl(localName)); |
114 |
|
} |
115 |
|
|
116 |
|
public List<String> getLanguages() { |
117 |
|
return langs; |
118 |
|
} |
119 |
|
|
120 |
|
/** |
121 |
|
* @return a number between 0. (bad) and 1. (good) that is calculated from |
122 |
|
* the amount of translation available in the visible attributes |
123 |
|
*/ |
124 |
@Override |
@Override |
125 |
public AttributeMetadataMap copy() { |
public double getQuality(final List<String> languages) { |
126 |
AttributeMetadataMap copy = new AttributeMetadataMap(); |
int allVisible = 0; |
127 |
return copyTo(copy); |
double colQmSum = 0.; |
128 |
|
for (final AMD_IMPL oneCol : values()) { |
129 |
|
|
130 |
|
if (oneCol.isVisible() && oneCol instanceof QualityQuantizable) { |
131 |
|
allVisible++; |
132 |
|
colQmSum += ((QualityQuantizable) oneCol).getQuality(languages); |
133 |
|
} |
134 |
|
} |
135 |
|
|
136 |
|
if (allVisible > 0) |
137 |
|
return colQmSum / allVisible; |
138 |
|
else |
139 |
|
return 1.; |
140 |
|
|
141 |
} |
} |
142 |
|
|
143 |
|
/** |
144 |
|
* @return List of {@link AMD_IMPL} objects ordered by their weight. |
145 |
|
* (heavier => further down) |
146 |
|
*/ |
147 |
|
public TreeSet<AMD_IMPL> sortedValues() { |
148 |
|
final TreeSet<AMD_IMPL> list = new TreeSet<AMD_IMPL>(); |
149 |
|
list.addAll(values()); |
150 |
|
// Collections.sort(list); |
151 |
|
return list; |
152 |
|
} |
153 |
|
|
154 |
|
/** |
155 |
|
* @return List of only the visible {@link AMD_IMPL} objects ordered by |
156 |
|
* their weight. (heavier => further down) |
157 |
|
*/ |
158 |
|
public List<AMD_IMPL> sortedValuesVisibleOnly() { |
159 |
|
final ArrayList<AMD_IMPL> list = new ArrayList<AMD_IMPL>(); |
160 |
|
for (final AMD_IMPL atm : sortedValues()) { |
161 |
|
if (atm.isVisible()) |
162 |
|
list.add(atm); |
163 |
|
} |
164 |
|
return list; |
165 |
|
} |
166 |
|
|
167 |
|
/** |
168 |
|
* Just for debuggung. Simply returns {@code super.put(.)}. |
169 |
|
* TODO: remove this method. |
170 |
|
*/ |
171 |
|
@Override |
172 |
|
public AMD_IMPL put(Name key, AMD_IMPL value) { |
173 |
|
return super.put(key, value); |
174 |
|
}; |
175 |
|
|
176 |
} |
} |