/[schmitzm]/trunk/src/skrueger/AttributeMetadataImpl.java
ViewVC logotype

Diff of /trunk/src/skrueger/AttributeMetadataImpl.java

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

branches/1.0-gt2-2.6/src/skrueger/AttributeMetadata.java revision 518 by alfonx, Fri Nov 13 18:16:38 2009 UTC branches/2.0-RC2/src/skrueger/AttributeMetadata.java revision 675 by alfonx, Thu Feb 4 19:05:34 2010 UTC
# Line 29  Line 29 
29   ******************************************************************************/   ******************************************************************************/
30  package skrueger;  package skrueger;
31    
32  import java.util.ArrayList;  import java.util.HashSet;
33  import java.util.List;  import java.util.List;
34    
35  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
# Line 50  import skrueger.i8n.Translation; Line 50  import skrueger.i8n.Translation;
50   */   */
51  public class AttributeMetadata implements Copyable<AttributeMetadata>,  public class AttributeMetadata implements Copyable<AttributeMetadata>,
52                  Comparable<AttributeMetadata> {                  Comparable<AttributeMetadata> {
53            
54          static private final Logger LOGGER = Logger          static private final Logger LOGGER = Logger
55                          .getLogger(AttributeMetadata.class);                          .getLogger(AttributeMetadata.class);
56    
         // public class WeightComparator implements Comparator<AttributeDescriptor>  
         // {  
         //  
         // @Override  
         // public int compare(AttributeDescriptor o1, AttributeDescriptor o2) {  
         // // TODO Auto-generated method stub  
         // return 0;  
         // }  
         //                
         // };  
   
         /** Translation of the attribute's title **/  
         protected Translation title = new Translation();  
   
57          /** Translation of the attribute's description **/          /** Translation of the attribute's description **/
58          protected Translation desc = new Translation();          protected Translation desc = new Translation();
59    
60          /** Is the attribute visible to the user or ignored where possible **/          /**
61          protected boolean visible = true;           * For numerical attributes the value can be transformed by VALUE*X+A when
62             * presented on screen. TODO not implemented yet
63             **/
64            protected Double functionA = 0.;
65    
66          /**          /**
67           * The unit append to all visualizations of values of this attribute (is not           * For numerical attributes the value can be transformed by VALUE*X+A when
68           * null)           * presented on screen. TODO not implemented yet
69           **/           **/
70          protected String unit = "";          protected Double functionX = 1.;
71    
72          /** The Name of the attribute **/          /** The Name of the attribute **/
73          private Name name;          private Name name;
74    
75          public int getWeight() {          /**
76                  return weight;           * Allows to define general NODATA values for an attribute. e.g. -9999 can
77          }           * be set and will always be interpreted as NULL internally and will usually
78             * be ignored. This overcomes the problem, that
79          public void setWeight(int weight) {           **/
80                  this.weight = weight;          protected final HashSet<Object> nodataValues = new HashSet<Object>();
         }  
   
         public List<Object> getNodataValues() {  
                 return nodataValues;  
         }  
   
         public void setNodataValues(List<Object> nodataValues) {  
                 this.nodataValues = nodataValues;  
         }  
   
         public Double getFunctionX() {  
                 return functionX;  
         }  
   
         public void setFunctionX(Double functionX) {  
                 this.functionX = functionX;  
         }  
81    
82          public Double getFunctionA() {          /** Translation of the attribute's title **/
83                  return functionA;          protected Translation title = new Translation();
         }  
84    
85          public void setFunctionA(Double functionA) {          /**
86                  this.functionA = functionA;           * The unit append to all visualizations of values of this attribute (is not
87          }           * null)
88             **/
89            protected String unit = "";
90    
91          public void setVisible(boolean visible) {          /** Is the attribute visible to the user or ignored where possible **/
92                  this.visible = visible;          protected boolean visible = true;
         }  
93    
94          /**          /**
95           * When listed, the attributes are listed according to their weight (heavier           * When listed, the attributes are listed according to their {@link #weight} (heavier
96           * => further down)           * => further down)
97             * @see #compareTo(AttributeMetadata)
98           **/           **/
99          protected int weight = 0;          protected int weight = 0;
100    
101          /**          /** Only used for {@link Copyable<AttributeMetaData>#copy()} **/
102           * Allows to define general NODATA values for an attribute. e.g. -9999 can          private AttributeMetadata() {
103           * be set and will always be interpreted as NULL internally and will usually          }
          * be ignored.  
          **/  
         protected List<Object> nodataValues = new ArrayList<Object>();  
104    
105          /**          public AttributeMetadata(final AttributeDescriptor attDesc, final int weight,
106           * For numerical attributes the value can be transformed by VALUE*X+A when                          final List<String> langs) {
107           * presented on screen                  this(attDesc.getName(), langs);
108           **/                  setWeight(weight);
109          protected Double functionX = 1.;          }
110    
111          /**          public AttributeMetadata(final AttributeDescriptor attDesc, final List<String> langs) {
112           * For numerical attributes the value can be transformed by VALUE*X+A when                  this(attDesc.getName(), langs);
113           * presented on screen          }
          **/  
         protected Double functionA = 0.;  
114    
115          /**          /**
116           * Creates an {@link AttributeMetadata} object with the following           * Creates an {@link AttributeMetadata} object with the following
# Line 165  public class AttributeMetadata implement Line 134  public class AttributeMetadata implement
134                  this.setName(name);                  this.setName(name);
135                  this.title = title;                  this.title = title;
136                  this.desc = desc;                  this.desc = desc;
137                    this.visible = visible;
138                    this.unit = unit;
139            }
140            
141    
142                  // The THE_GEOM and shall never be visible!          /**
143                  if (name.getLocalPart().equalsIgnoreCase("the_geom"))           * Creates an {@link AttributeMetadata} object with the following
144                          this.visible = false;           * information
145                  else           *
146                          this.visible = visible;           * @param colIdx
147             *            The column index of this attribute in the underlying
148             *            table/dbf/etc...
149             * @param visible
150             *            Shall this attribute be displayed or hidden from the user?
151             * @param unit
152             *            {@link String} of the unit that the information is in
153             */
154            public AttributeMetadata(final Name name, final Boolean visible, final String unit) {
155                    this.setName(name);
156                    this.visible = visible;
157                  this.unit = unit;                  this.unit = unit;
158          }          }
159    
160          /**          /**
161             * Creates a new visible {@link AttributeMetadata}
162             */
163            public AttributeMetadata(final Name name, final List<String> langs) {
164                    this(name, true, new Translation(langs, name.getLocalPart()),
165                                    new Translation(), "");
166            }
167    
168            /**
169             * Creates a new visible {@link AttributeMetadata}
170             */
171            public AttributeMetadata(final Name name, final String defaultTitle,
172                            final List<String> langs) {
173                    this(name, true, new Translation(langs, defaultTitle),
174                                    new Translation(), "");
175            }
176    
177            /**
178           * Creates an {@link AttributeMetadata} object with the following           * Creates an {@link AttributeMetadata} object with the following
179           * information           * information
180           *           *
# Line 196  public class AttributeMetadata implement Line 195  public class AttributeMetadata implement
195          /**          /**
196           * Creates a new visible {@link AttributeMetadata} with default (no) values.           * Creates a new visible {@link AttributeMetadata} with default (no) values.
197           */           */
198          public AttributeMetadata(final String localName, final String defaultTitle) {          public AttributeMetadata(final String localName, final List<String> langs) {
199                  this(localName, true, new Translation(defaultTitle), new Translation(),                  this(localName, true, new Translation(langs, localName),
200                                  "");                                  new Translation(), "");
201          }          }
202    
203          /**          /**
204           * Creates a new visible {@link AttributeMetadata} with default (no) values.           * Creates a new visible {@link AttributeMetadata}
205           */           */
206          public AttributeMetadata(final Name name, final String defaultTitle) {          public AttributeMetadata(final String localName, final String defaultTitle,
207                  this(name, true, new Translation(defaultTitle), new Translation(), "");                          final List<String> langs) {
208                    this(localName, true, new Translation(langs, defaultTitle),
209                                    new Translation(), "");
210          }          }
211    
212          /**          /**
213           * Creates a new visible {@link AttributeMetadata} with default (no) values.           * Orders the attributes according to their {@link #weight}. Heavier =>
214             * further down.
215           */           */
216          public AttributeMetadata(final Name name) {          @Override
217                  this(name, true, new Translation(name.getLocalPart()),          public int compareTo(final AttributeMetadata atm2) {
218                                  new Translation(), "");                  return new Integer(weight).compareTo(atm2.getWeight());
219          }          }
220    
221          /**          /**
222           * Creates a new visible {@link AttributeMetadata} with default (no) values.           * @see Copyable inferface
223           */           */
224          public AttributeMetadata(final String localName) {          @Override
225                  this(localName, true, new Translation(localName), new Translation(), "");          public AttributeMetadata copy() {
226                    return copyTo(new AttributeMetadata());
227          }          }
228    
229          /** Only used for {@link Copyable<AttributeMetaData>#copy()} **/          /**
230          private AttributeMetadata() {           * @see Copyable inferface
231             */
232            @Override
233            public AttributeMetadata copyTo(final AttributeMetadata amd) {
234                    getTitle().copyTo(amd.getTitle());
235                    getDesc().copyTo(amd.getDesc());
236                    amd.setUnit(getUnit());
237                    amd.setVisible(isVisible());
238                    amd.setName(new NameImpl(getName().getNamespaceURI(), getName()
239                                    .getLocalPart()));
240    
241                    amd.setWeight(getWeight());
242                    amd.setFunctionX(getFunctionX());
243                    amd.setFunctionA(getFunctionA());
244    
245                    for (final Object nodataValue : getNodataValues()) {
246                            amd.getNodataValues().add(nodataValue);
247                    }
248    
249                    return amd;
250          }          }
251    
252          public AttributeMetadata(AttributeDescriptor attDesc) {          public Translation getDesc() {
253                  this(attDesc.getName());                  return desc;
254          }          }
255    
256          public boolean isVisible() {          public Double getFunctionA() {
257                  return visible;                  return functionA;
258          }          }
259    
260          public void setVisible(final Boolean visible) {          public Double getFunctionX() {
261                    return functionX;
262            }
263    
264                  // The THE_GEOM and shall never be visible! // TODO MAKE BETTER          /**
265                  if (name.getLocalPart().equalsIgnoreCase("the_geom"))           * The local name. E.g. the name of the DBF column as a {@link String}
266                          this.visible = false;           */
267                  else          public String getLocalName() {
268                          this.visible = visible;                  return getName().getLocalPart();
                   
                 this.visible = visible;  
269          }          }
270    
271          public Translation getTitle() {          /**
272                  return title;           * The fully qualified {@link Name} of the attribute, e.g. <code>org.bla.plo:blub</code>
273             */
274            public Name getName() {
275                    return name;
276          }          }
277    
278          public void setTitle(final Translation title) {          public HashSet<Object> getNodataValues() {
279                  this.title = title;                  return nodataValues;
280          }          }
281    
282          public Translation getDesc() {          /**
283                  return desc;           * @return a number between 0 (bad) and 1 (good) that is calculated from the
284             *         amount of translation available. If this attribute is not
285             *         {@link #visible}, it will return 1.
286             */
287            public double getQuality(final List<String> languages) {
288    
289                    if (!isVisible())
290                            return 1.;
291    
292                    return (I8NUtil.qmTranslation(languages, getTitle()) * 2. + I8NUtil
293                                    .qmTranslation(languages, getDesc()) * 1.) / 3.;
294          }          }
295    
296          public void setDesc(final Translation desc) {          public Translation getTitle() {
297                  this.desc = desc;                  return title;
298          }          }
299    
300          public String getUnit() {          public String getUnit() {
301                  return unit;                  return unit;
302          }          }
303    
304          public void setUnit(final String unit) {          public int getWeight() {
305                  this.unit = unit;                  return weight;
306          }          }
307    
308          @Override          /**
309          public AttributeMetadata copyTo(AttributeMetadata amd) {           * Will the end-user see this attribute?
310                  getTitle().copyTo(amd.getTitle());           */
311                  getDesc().copyTo(amd.getDesc());          public boolean isVisible() {
312                  amd.setUnit(getUnit());                  return visible;
313                  amd.setVisible(isVisible());          }
                 amd.setName(new NameImpl(getName().getNamespaceURI(), getName()  
                                 .getLocalPart()));  
   
                 amd.setWeight(getWeight());  
                 amd.setFunctionX(getFunctionX());  
                 amd.setFunctionA(getFunctionA());  
   
                 for (Object nodataValue : getNodataValues()) {  
                         amd.getNodataValues().add(nodataValue);  
                 }  
314    
315                  return amd;          public void setDesc(final Translation desc) {
316                    this.desc = desc;
317          }          }
318    
319          @Override          public void setFunctionA(final Double functionA) {
320          public AttributeMetadata copy() {                  this.functionA = functionA;
                 return copyTo(new AttributeMetadata());  
321          }          }
322    
323          /**          public void setFunctionX(final Double functionX) {
324           * The local Name. E.g. the name of the DBF column as a String                  this.functionX = functionX;
          */  
         public String getLocalName() {  
                 return getName().getLocalPart();  
325          }          }
326    
327          /**          /**
328           * The fully qualified Name of the attribute, e.g. org.bla.plo:blub           * The fully qualified Name of the attribute, e.g. org.bla.plo:blub
329           */           */
330          public Name getName() {          public void setLocalName(final String localName) {
331                  return name;                  this.name = new NameImpl(localName);
332          }          }
333    
334          /**          /**
335           * The fully qualified Name of the attribute, e.g. org.bla.plo:blub           * The fully qualified {@link Name} of the attribute, e.g. <code>org.bla.plo:blub</code>
336           */           */
337          public void setName(org.opengis.feature.type.Name name) {          public void setName(final Name name) {
338                  this.name = name;                  this.name = name;
339          }          }
340    
341            public void addNodataValue(Object nodataValue) {
342                    this.nodataValues.add(nodataValue);
343            }
344            
345            public void removeNodataValue(Object nodataValue) {
346                    this.nodataValues.remove(nodataValue);
347            }
348    
349            public void setTitle(final Translation title) {
350                    this.title = title;
351            }
352    
353            public void setUnit(final String unit) {
354                    this.unit = unit;
355            }
356    
357            public void setVisible(final boolean visible) {
358                    this.visible = visible;
359            }
360    
361          /**          /**
362           * The fully qualified Name of the attribute, e.g. org.bla.plo:blub           * Shall the end-user see this attribute?
363             * @param visible
364           */           */
365          public void setLocalName(String localName) {          public void setVisible(final Boolean visible) {
366                  this.name = new NameImpl(localName);  //              // The THE_GEOM and shall never be visible!
367    //              if (name.getLocalPart().equalsIgnoreCase("the_geom"))
368    //                      this.visible = false;
369    //              else
370    //                      this.visible = visible;
371    
372                    this.visible = visible;
373          }          }
374    
375            public void setWeight(final int weight) {
376                    this.weight = weight;
377            }
378            
379          /**          /**
380           * Orders the Attributes according to their weight. Heavier => further down.           * For nicer debugging
381           */           */
382          @Override          @Override
383          public int compareTo(AttributeMetadata atm2) {          public String toString() {
384                  // Double double1 = new Double(1./weight);                  StringBuffer sb = new StringBuffer();
385                  // double double2 = 1./atm2.getWeight();                  if (name != null) sb.append(name.toString()+" ");
386                  return new Integer(weight).compareTo(atm2.getWeight());                  sb.append("weight="+weight+" ");
387                    sb.append("title="+getTitle().toString());
388                    return sb.toString();
389          }          }
390    
391          /**          /**
392           * @return a number between 0 (bad) and 1 (good) that is calculated from the amount of translation available in the visible attributes           * Takes any value object and checks it against the NODATA values. If the
393             * value equals a NODATA value, <code>null</code> is returned. Otherwise the
394             * same object is returned.
395             *
396             * Note: This method is called often.
397           */           */
398          public double getQuality(List<String> languages) {          public Object fiterNodata(final Object value) {
399                  return (I8NUtil.qmTranslation(languages, getTitle()) * 2. + I8NUtil                  if (nodataValues.contains(value)) return null;
400                                  .qmTranslation(languages, getDesc()) * 1.) / 3.;                  return value;
401          }          }
402  }  }

Legend:
Removed from v.518  
changed lines
  Added in v.675

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26