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