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