/[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 533 by alfonx, Thu Nov 19 17:27:01 2009 UTC trunk/src/skrueger/geotools/AttributeMetadataMap.java revision 769 by alfonx, Sun Mar 21 11:02:34 2010 UTC
# Line 3  package skrueger.geotools; Line 3  package skrueger.geotools;
3  import java.util.ArrayList;  import java.util.ArrayList;
4  import java.util.Collections;  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 cern.colt.Arrays;  import skrueger.AbstractAttributeMetadata;
   
14  import skrueger.AttributeMetadata;  import skrueger.AttributeMetadata;
15  import skrueger.i8n.I8NUtil;  import skrueger.QualityQuantizable;
16  import skrueger.i8n.Translation;  import skrueger.i8n.Translation;
17    
18  /**  /**
19   * 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}
20     * object for {@link Name} keys. It's an extension of a {@link TreeMap}. It's
21     * copyable in the sense of the {@link Copyable} interface.<br>
22     * The {@link #get(Name)} and {@link #get(String)} methods will never return
23     * <code>null</code>, but rather create a default {@link AMD_IMPL} 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.getLogger(AttributeMetadataMap.class);                  Copyable<AttributeMetadataMap>, QualityQuantizable {
28          private List<String> langs;  
29                    private static final long serialVersionUID = 4936966916517396063L;
30          public AttributeMetadataMap(List<String> defLanguages) {  
31                  langs = defLanguages;          protected static final Logger LOGGER = Logger
32          }                          .getLogger(AttributeMetadataMap.class);
33            
34          @Deprecated          /**
35             * A list of default languages used when creating default {@link AMD_IMPL}
36             * on-the-fly. If not initialized by a constructor, it will be
37             * {@link Translation#getActiveLang()} only.
38             **/
39            protected final List<String> langs;
40    
41            /**
42             * AttributeMetadataMap is based on {@link Translation} and would like to
43             * know how many languages are supported, so the needed defaults can be
44             * 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>();
50                  langs.add(Translation.getActiveLang());                  langs.add(Translation.getActiveLang());
51          }          }
           
           
         public AttributeMetadataMap(String[] strings) {  
                 langs = new ArrayList<String>(java.util.Arrays.asList(strings));  
         }  
52    
53          /**          /**
54           * @Deprecated use get(Name name) or get(String localName)           * Creates an {@link AttributeMetadataMap} and sets the list of default
55             * languages.
56           */           */
57          @Deprecated          public AttributeMetadataMap(final List<String> defLanguages) {
58          public AttributeMetadata get(Object key) {                  langs = defLanguages;
                 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!");  
                 return super.get(key);  
         }  
           
         public AttributeMetadata get(Name name) {  
                 final AttributeMetadata attributeMetadata = super.get(name);  
                 if (attributeMetadata == null && name != null && !name.getLocalPart().trim().isEmpty()) {  
                         put(name,new AttributeMetadata(name, langs));  
                         return super.get(name);  
                 }  
                 return attributeMetadata;  
59          }          }
60    
61          public AttributeMetadata get(String localName) {          /**
62                  return this.get(new NameImpl(localName));           * Creates an {@link AttributeMetadataMap} and sets the list of default
63             * languages.
64             */
65            public AttributeMetadataMap(final String[] langs) {
66                    this.langs = new ArrayList<String>(java.util.Arrays.asList(langs));
67          }          }
68            
69            /**
70             * Copies all its values to another {@link AttributeMetadataMap}. @see
71             * {@link Copyable} interface.
72             */
73          @Override          @Override
74          public AttributeMetadataMap copyTo(AttributeMetadataMap amdMap) {          public AttributeMetadataMap<? extends AttributeMetadata> copyTo(AttributeMetadataMap amdMap) {
75                    
76                  amdMap.clear();                  amdMap.clear();
77                    
78                  for (Name key : keySet()) {                  for (final Name key : keySet()) {
79                          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 AMD_IMPL} for a given {@link Name}. May return
87             * <code>null</code> or create a default {@link AttributeMetadata} depending
88             * on whether this method is overwritten. fly.
89             */
90            public AMD_IMPL get(final Name name) {
91                    return super.get(name);
92            }
93    
94            /**
95             * @Deprecated use get(Name name) or get(String localName)
96             */
97          @Override          @Override
98          public AttributeMetadataMap copy() {          @Deprecated
99                  AttributeMetadataMap copy = new AttributeMetadataMap(langs);          public AMD_IMPL get(final Object key) {
100                  return copyTo(copy);                  LOGGER.error("PLEASE DONT USE get(Object) any MORE!");
101                    return super.get(key);
102          }          }
103    
104          public List<AttributeMetadata> sortedValues() {          /**
105                  ArrayList<AttributeMetadata> list = new ArrayList<AttributeMetadata>();           * Returns the {@link AMD_IMPL} for a given {@link Name}. Never returns
106                  list.addAll(values());           * <code>null</code>, but rather creates a default {@link AMD_IMPL} on the
107                  return list;           * fly.
108             */
109            public AMD_IMPL get(final String localName) {
110                    if (localName == null)
111                            return null;
112                    return this.get(new NameImpl(localName));
113            }
114    
115            public List<String> getLanguages() {
116                    return langs;
117          }          }
118    
119          /**          /**
120           * @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
121             *         the amount of translation available in the visible attributes
122           */           */
123          public double getQuality(List<String> languages) {          @Override
124            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    
# Line 107  public class AttributeMetadataMap extend Line 139  public class AttributeMetadataMap extend
139    
140          }          }
141    
142          public List<String> getLanguages() {          /**
143                  return langs;           * @return List of {@link AMD_IMPL} objects ordered by their weight.
144             *         (heavier => further down)
145             */
146            public List<AMD_IMPL> sortedValues() {
147                    final ArrayList<AMD_IMPL> list = new ArrayList<AMD_IMPL>();
148                    list.addAll(values());
149                    Collections.sort(list);
150                    return list;
151            }
152    
153            /**
154             * @return List of only the visible {@link AMD_IMPL} objects ordered by
155             *         their weight. (heavier => further down)
156             */
157            public List<AMD_IMPL> sortedValuesVisibleOnly() {
158                    final ArrayList<AMD_IMPL> list = new ArrayList<AMD_IMPL>();
159                    for (final AMD_IMPL atm : sortedValues()) {
160                            if (atm.isVisible())
161                                    list.add(atm);
162                    }
163                    return list;
164          }          }
165  }  }

Legend:
Removed from v.533  
changed lines
  Added in v.769

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26