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