1 |
package skrueger.geotools; |
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.List; |
5 |
import java.util.TreeMap; |
6 |
|
7 |
import org.apache.log4j.Logger; |
8 |
import org.geotools.feature.NameImpl; |
9 |
import org.opengis.feature.type.Name; |
10 |
|
11 |
import skrueger.AttributeMetadata; |
12 |
import skrueger.i8n.I8NUtil; |
13 |
|
14 |
/** |
15 |
* An extension of TreeMap, that is copyable in the sense of the {@link Copyable} interface |
16 |
*/ |
17 |
public class AttributeMetadataMap extends TreeMap<Name, AttributeMetadata> |
18 |
implements Copyable<AttributeMetadataMap> { |
19 |
static private final Logger LOGGER = Logger.getLogger(AttributeMetadataMap.class); |
20 |
|
21 |
|
22 |
/** |
23 |
* @Deprecated use get(Name name) or get(String localName) |
24 |
*/ |
25 |
@Deprecated |
26 |
public AttributeMetadata get(Object key) { |
27 |
LOGGER.warn("PLEASE DONT USE get(Object) any MORE!"); |
28 |
LOGGER.warn("PLEASE DONT USE get(Object) any MORE!"); |
29 |
LOGGER.warn("PLEASE DONT USE get(Object) any MORE!"); |
30 |
return super.get(key); |
31 |
} |
32 |
|
33 |
public AttributeMetadata get(Name name) { |
34 |
final AttributeMetadata attributeMetadata = super.get(name); |
35 |
if (attributeMetadata == null && name != null && !name.getLocalPart().trim().isEmpty()) { |
36 |
put(name,new AttributeMetadata(name)); |
37 |
return super.get(name); |
38 |
} |
39 |
return attributeMetadata; |
40 |
} |
41 |
|
42 |
public AttributeMetadata get(String localName) { |
43 |
return this.get(new NameImpl(localName)); |
44 |
} |
45 |
|
46 |
@Override |
47 |
public AttributeMetadataMap copyTo(AttributeMetadataMap amdMap) { |
48 |
|
49 |
amdMap.clear(); |
50 |
|
51 |
for (Name key : keySet()) { |
52 |
AttributeMetadata attributeMetaData = get(key); |
53 |
amdMap.put(key, attributeMetaData.copy()); |
54 |
} |
55 |
return amdMap; |
56 |
} |
57 |
|
58 |
@Override |
59 |
public AttributeMetadataMap copy() { |
60 |
AttributeMetadataMap copy = new AttributeMetadataMap(); |
61 |
return copyTo(copy); |
62 |
} |
63 |
|
64 |
public List<AttributeMetadata> sortedValues() { |
65 |
ArrayList<AttributeMetadata> list = new ArrayList<AttributeMetadata>(); |
66 |
list.addAll(values()); |
67 |
return list; |
68 |
} |
69 |
|
70 |
/** |
71 |
* @return a number between 0 (bad) and 1 (good) that is calculated from the amount of translation available in the visible attributes |
72 |
*/ |
73 |
public double getQuality(List<String> languages) { |
74 |
int allVisible = 0; |
75 |
double colQmSum = 0.; |
76 |
for (final AttributeMetadata oneCol : values()) { |
77 |
|
78 |
if (oneCol.isVisible()) { |
79 |
allVisible++; |
80 |
colQmSum += oneCol.getQuality(languages); |
81 |
} |
82 |
} |
83 |
|
84 |
if (allVisible > 0) |
85 |
return colQmSum / allVisible; |
86 |
else |
87 |
return 1.; |
88 |
|
89 |
} |
90 |
} |