/[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 474 by alfonx, Thu Oct 15 14:04:16 2009 UTC trunk/src/skrueger/geotools/AttributeMetadataMap.java revision 685 by alfonx, Wed Feb 10 15:04:02 2010 UTC
# Line 1  Line 1 
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;
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.AttributeMetadata;
14    import skrueger.i8n.Translation;
15    
16  /**  /**
17   * An extension of TreeMap, that is copyable in the sense of the {@link Copyable} interface   * The {@link AttributeMetadataMap} is a {@link Map} holding
18     * {@link AttributeMetadata} object for {@link Name} keys. It's an extension of
19     * 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<Name, 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           * @Deprecated use get(Name name) or get(String localName)           * 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            /**
38             @Deprecated Use another constructor. AttributeMetadataMap is based on {@link Translation} and
39             *             would like to know how many languages are supported, so the
40             *             needed defaults can be created.
41           */           */
42          @Deprecated          public AttributeMetadataMap() {
43          public AttributeMetadata get(Object key) {                  langs = new ArrayList<String>();
44                  return super.get(key);                  langs.add(Translation.getActiveLang());
45          }          }
46    
47          public AttributeMetadata get(Name name) {          /**
48                  final AttributeMetadata attributeMetadata = super.get(name);           * Creates an {@link AttributeMetadataMap} and sets the list of default
49                  if (attributeMetadata == null && name != null && !name.getLocalPart().trim().isEmpty()) {           * languages.
50                          put(name,new AttributeMetadata(name));           */
51                          return super.get(name);          public AttributeMetadataMap(final List<String> defLanguages) {
52                  }                  langs = defLanguages;
                 return attributeMetadata;  
53          }          }
54    
55          public AttributeMetadata get(String localName) {          /**
56                  return this.get(new NameImpl(localName));           * Creates an {@link AttributeMetadataMap} and sets the list of default
57             * languages.
58             */
59            public AttributeMetadataMap(final String[] strings) {
60                    langs = new ArrayList<String>(java.util.Arrays.asList(strings));
61            }
62    
63            /**
64             * Returns a deep-copy. @see {@link Copyable} interface
65             */
66            @Override
67            public AttributeMetadataMap copy() {
68                    final AttributeMetadataMap copy = new AttributeMetadataMap(langs);
69                    return copyTo(copy);
70          }          }
71            
72            /**
73             * Copies all its values to another {@link AttributeMetadataMap}. @see
74             * {@link Copyable} interface.
75             */
76          @Override          @Override
77          public AttributeMetadataMap copyTo(AttributeMetadataMap amdMap) {          public AttributeMetadataMap copyTo(final AttributeMetadataMap amdMap) {
78                    
79                  amdMap.clear();                  amdMap.clear();
80                    
81                  for (Name key : keySet()) {                  for (final Name key : keySet()) {
82                          AttributeMetadata attributeMetaData = get(key);                          final AttributeMetadata attributeMetaData = get(key);
83                          amdMap.put(key, attributeMetaData.copy());                          amdMap.put(key, attributeMetaData.copy());
84                  }                  }
85                  return amdMap;                  return amdMap;
86          }          }
87    
88            /**
89             * Returns the {@link AttributeMetadata} for a given {@link Name}. Never
90             * returns <code>null</code>, but rather creates a default
91             * {@link AttributeMetadata} on the fly.
92             */
93            public AttributeMetadata get(final Name name) {
94                    final AttributeMetadata attributeMetadata = super.get(name);
95                    if (attributeMetadata == null && name != null
96                                    && !name.getLocalPart().trim().isEmpty()) {
97                            put(name, new AttributeMetadata(name, langs));
98                            return super.get(name);
99                    }
100                    return attributeMetadata;
101            }
102    
103            /**
104             * @Deprecated use get(Name name) or get(String localName)
105             */
106          @Override          @Override
107          public AttributeMetadataMap copy() {          @Deprecated
108                  AttributeMetadataMap copy = new AttributeMetadataMap();          public AttributeMetadata get(final Object key) {
109                  return copyTo(copy);                  LOGGER.warn("PLEASE DONT USE get(Object) any MORE!");
110                    LOGGER.warn("PLEASE DONT USE get(Object) any MORE!");
111                    LOGGER.warn("PLEASE DONT USE get(Object) any MORE!");
112                    return super.get(key);
113            }
114    
115            /**
116             * Returns the {@link AttributeMetadata} for a given {@link Name}. Never
117             * returns <code>null</code>, but rather creates a default
118             * {@link AttributeMetadata} on the fly.
119             */
120            public AttributeMetadata get(final String localName) {
121                    return this.get(new NameImpl(localName));
122            }
123    
124            public List<String> getLanguages() {
125                    return langs;
126          }          }
127    
128            /**
129             * @return a number between 0. (bad) and 1. (good) that is calculated from
130             *         the amount of translation available in the visible attributes
131             */
132            public double getQuality(final List<String> languages) {
133                    int allVisible = 0;
134                    double colQmSum = 0.;
135                    for (final AttributeMetadata oneCol : values()) {
136    
137                            if (oneCol.isVisible()) {
138                                    allVisible++;
139                                    colQmSum += oneCol.getQuality(languages);
140                            }
141                    }
142    
143                    if (allVisible > 0)
144                            return colQmSum / allVisible;
145                    else
146                            return 1.;
147    
148            }
149    
150    
151            /**
152             * @return List of {@link AttributeMetadata} objects ordered by their
153             *         weight. (heavier => further down)
154             */
155          public List<AttributeMetadata> sortedValues() {          public List<AttributeMetadata> sortedValues() {
156                  ArrayList<AttributeMetadata> list = new ArrayList<AttributeMetadata>();                  final ArrayList<AttributeMetadata> list = new ArrayList<AttributeMetadata>();
157                  list.addAll(values());                  list.addAll(values());
158                    Collections.sort(list);
159                    return list;
160            }
161    
162            /**
163             * @return List of only the visible {@link AttributeMetadata} objects
164             *         ordered by their weight. (heavier => further down)
165             */
166            public List<AttributeMetadata> sortedValuesVisibleOnly() {
167                    final ArrayList<AttributeMetadata> list = new ArrayList<AttributeMetadata>();
168                    for (final AttributeMetadata atm : sortedValues()) {
169                            if (atm.isVisible())
170                                    list.add(atm);
171                    }
172                  return list;                  return list;
173          }          }
174  }  }

Legend:
Removed from v.474  
changed lines
  Added in v.685

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26