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