2 |
|
|
3 |
import java.util.ArrayList; |
import java.util.ArrayList; |
4 |
import java.util.Collections; |
import java.util.Collections; |
5 |
|
import java.util.LinkedHashSet; |
6 |
import java.util.List; |
import java.util.List; |
7 |
import java.util.Map; |
import java.util.Map; |
8 |
import java.util.TreeMap; |
import java.util.TreeMap; |
9 |
|
import java.util.TreeSet; |
10 |
|
|
11 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
12 |
import org.geotools.feature.NameImpl; |
import org.geotools.feature.NameImpl; |
13 |
import org.opengis.feature.type.Name; |
import org.opengis.feature.type.Name; |
14 |
|
|
15 |
import skrueger.AttributeMetadata; |
import skrueger.AttributeMetadataInterface; |
16 |
|
import skrueger.QualityQuantizable; |
17 |
import skrueger.i8n.Translation; |
import skrueger.i8n.Translation; |
18 |
|
|
19 |
/** |
/** |
20 |
* The {@link AttributeMetadataMap} is a {@link Map} holding |
* The {@link AttributeMetadataMap} is a {@link Map} holding {@link AMD_IMPL} |
21 |
* {@link AttributeMetadata} object for {@link Name} keys. It's an extension of |
* object for {@link Name} keys. It's an extension of a {@link TreeMap}. It's |
22 |
* a {@link TreeMap}. It's copyable in the sense of the {@link Copyable} |
* copyable in the sense of the {@link Copyable} interface.<br> |
|
* interface.<br> |
|
23 |
* The {@link #get(Name)} and {@link #get(String)} methods will never return |
* The {@link #get(Name)} and {@link #get(String)} methods will never return |
24 |
* <code>null</code>, but rather create a default {@link AttributeMetadata} |
* <code>null</code>, but rather create a default {@link AMD_IMPL} on-the-fly. |
|
* on-the-fly. |
|
25 |
*/ |
*/ |
26 |
public class AttributeMetadataMap extends TreeMap<Name, AttributeMetadata> |
public abstract class AttributeMetadataMap<AMD_IMPL extends AttributeMetadataInterface> |
27 |
implements Copyable<AttributeMetadataMap> { |
extends TreeMap<Name, AMD_IMPL> implements |
28 |
static private final Logger LOGGER = Logger |
Copyable<AttributeMetadataMap>, QualityQuantizable { |
29 |
|
|
30 |
|
private static final long serialVersionUID = 4936966916517396063L; |
31 |
|
|
32 |
|
protected static final Logger LOGGER = Logger |
33 |
.getLogger(AttributeMetadataMap.class); |
.getLogger(AttributeMetadataMap.class); |
34 |
|
|
35 |
/** |
/** |
36 |
* A list of default languages used when creating default |
* A list of default languages used when creating default {@link AMD_IMPL} |
37 |
* {@link AttributeMetadata} on-the-fly. If not initialized by a |
* on-the-fly. If not initialized by a constructor, it will be |
38 |
* constructor, it will be {@link Translation#getActiveLang()} only. |
* {@link Translation#getActiveLang()} only. |
39 |
**/ |
**/ |
40 |
private final List<String> langs; |
protected final List<String> langs; |
41 |
|
|
42 |
@Deprecated |
/** |
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>(); |
langs = new ArrayList<String>(); |
51 |
langs.add(Translation.getActiveLang()); |
langs.add(Translation.getActiveLang()); |
63 |
* Creates an {@link AttributeMetadataMap} and sets the list of default |
* Creates an {@link AttributeMetadataMap} and sets the list of default |
64 |
* languages. |
* languages. |
65 |
*/ |
*/ |
66 |
public AttributeMetadataMap(final String[] strings) { |
public AttributeMetadataMap(final String[] langs) { |
67 |
langs = new ArrayList<String>(java.util.Arrays.asList(strings)); |
this.langs = new ArrayList<String>(java.util.Arrays.asList(langs)); |
|
} |
|
|
|
|
|
/** |
|
|
* Returns a deep-copy. @see {@link Copyable} interface |
|
|
*/ |
|
|
@Override |
|
|
public AttributeMetadataMap copy() { |
|
|
final AttributeMetadataMap copy = new AttributeMetadataMap(langs); |
|
|
return copyTo(copy); |
|
68 |
} |
} |
69 |
|
|
70 |
/** |
/** |
72 |
* {@link Copyable} interface. |
* {@link Copyable} interface. |
73 |
*/ |
*/ |
74 |
@Override |
@Override |
75 |
public AttributeMetadataMap copyTo(final AttributeMetadataMap amdMap) { |
public AttributeMetadataMap<? extends AttributeMetadataInterface> copyTo(AttributeMetadataMap amdMap) { |
76 |
|
|
77 |
amdMap.clear(); |
amdMap.clear(); |
78 |
|
|
79 |
for (final Name key : keySet()) { |
for (final Name key : keySet()) { |
80 |
final 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 AttributeMetadata} for a given {@link Name}. Never |
* Returns the {@link AMD_IMPL} for a given {@link NameImpl}. May return |
88 |
* returns <code>null</code>, but rather creates a default |
* <code>null</code> or create a default {@link AttributeMetadataInterface} depending |
89 |
* {@link AttributeMetadata} on the fly. |
* on whether this method is overwritten. fly. |
90 |
*/ |
*/ |
91 |
public AttributeMetadata get(final Name name) { |
public AMD_IMPL get(final Name name) { |
92 |
final AttributeMetadata attributeMetadata = super.get(name); |
return super.get(name); |
|
if (attributeMetadata == null && name != null |
|
|
&& !name.getLocalPart().trim().isEmpty()) { |
|
|
put(name, new AttributeMetadata(name, langs)); |
|
|
return super.get(name); |
|
|
} |
|
|
return attributeMetadata; |
|
93 |
} |
} |
94 |
|
|
95 |
/** |
/** |
96 |
* @Deprecated use get(Name name) or get(String localName) |
* @Deprecated use get(Name name) or get(String localName) |
97 |
*/ |
*/ |
98 |
|
@Deprecated |
99 |
@Override |
@Override |
100 |
@Deprecated |
public AMD_IMPL get(final Object key) { |
101 |
public AttributeMetadata get(final Object key) { |
LOGGER.error("PLEASE DONT USE get(Object) any MORE!"); |
|
LOGGER.warn("PLEASE DONT USE get(Object) any MORE!"); |
|
|
LOGGER.warn("PLEASE DONT USE get(Object) any MORE!"); |
|
|
LOGGER.warn("PLEASE DONT USE get(Object) any MORE!"); |
|
102 |
return super.get(key); |
return super.get(key); |
103 |
} |
} |
104 |
|
|
105 |
/** |
/** |
106 |
* Returns the {@link AttributeMetadata} for a given {@link Name}. Never |
* Returns the {@link AMD_IMPL} for a given {@link Name}. Never returns |
107 |
* returns <code>null</code>, but rather creates a default |
* <code>null</code>, but rather creates a default {@link AMD_IMPL} on the |
108 |
* {@link AttributeMetadata} on the fly. |
* fly. |
109 |
*/ |
*/ |
110 |
public AttributeMetadata get(final String localName) { |
public AMD_IMPL get(final String localName) { |
111 |
|
if (localName == null) |
112 |
|
return null; |
113 |
return this.get(new NameImpl(localName)); |
return this.get(new NameImpl(localName)); |
114 |
} |
} |
115 |
|
|
121 |
* @return a number between 0. (bad) and 1. (good) that is calculated from |
* @return a number between 0. (bad) and 1. (good) that is calculated from |
122 |
* the amount of translation available in the visible attributes |
* the amount of translation available in the visible attributes |
123 |
*/ |
*/ |
124 |
|
@Override |
125 |
public double getQuality(final List<String> languages) { |
public double getQuality(final List<String> languages) { |
126 |
int allVisible = 0; |
int allVisible = 0; |
127 |
double colQmSum = 0.; |
double colQmSum = 0.; |
128 |
for (final AttributeMetadata oneCol : values()) { |
for (final AMD_IMPL oneCol : values()) { |
129 |
|
|
130 |
if (oneCol.isVisible()) { |
if (oneCol.isVisible() && oneCol instanceof QualityQuantizable) { |
131 |
allVisible++; |
allVisible++; |
132 |
colQmSum += oneCol.getQuality(languages); |
colQmSum += ((QualityQuantizable) oneCol).getQuality(languages); |
133 |
} |
} |
134 |
} |
} |
135 |
|
|
140 |
|
|
141 |
} |
} |
142 |
|
|
|
|
|
143 |
/** |
/** |
144 |
* @return List of {@link AttributeMetadata} objects ordered by their |
* @return List of {@link AMD_IMPL} objects ordered by their weight. |
145 |
* weight. (heavier => further down) |
* (heavier => further down) |
146 |
*/ |
*/ |
147 |
public List<AttributeMetadata> sortedValues() { |
public TreeSet<AMD_IMPL> sortedValues() { |
148 |
final ArrayList<AttributeMetadata> list = new ArrayList<AttributeMetadata>(); |
final TreeSet<AMD_IMPL> list = new TreeSet<AMD_IMPL>(); |
149 |
list.addAll(values()); |
list.addAll(values()); |
150 |
Collections.sort(list); |
// Collections.sort(list); |
151 |
return list; |
return list; |
152 |
} |
} |
153 |
|
|
154 |
/** |
/** |
155 |
* @return List of only the visible {@link AttributeMetadata} objects |
* @return List of only the visible {@link AMD_IMPL} objects ordered by |
156 |
* ordered by their weight. (heavier => further down) |
* their weight. (heavier => further down) |
157 |
*/ |
*/ |
158 |
public List<AttributeMetadata> sortedValuesVisibleOnly() { |
public List<AMD_IMPL> sortedValuesVisibleOnly() { |
159 |
final ArrayList<AttributeMetadata> list = new ArrayList<AttributeMetadata>(); |
final ArrayList<AMD_IMPL> list = new ArrayList<AMD_IMPL>(); |
160 |
for (final AttributeMetadata atm : sortedValues()) { |
for (final AMD_IMPL atm : sortedValues()) { |
161 |
if (atm.isVisible()) |
if (atm.isVisible()) |
162 |
list.add(atm); |
list.add(atm); |
163 |
} |
} |
164 |
return list; |
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 |
} |
} |