/[schmitzm]/trunk/src/skrueger/geotools/AttributeMetadataMap.java
ViewVC logotype

Diff of /trunk/src/skrueger/geotools/AttributeMetadataMap.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.420  
changed lines
  Added in v.604

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26