--- branches/1.0-gt2-2.6/src/skrueger/AttributeMetadata.java 2009/11/20 10:28:01 534 +++ branches/2.0-RC2/src/skrueger/AttributeMetadata.java 2010/02/09 22:08:26 681 @@ -29,8 +29,9 @@ ******************************************************************************/ package skrueger; -import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.apache.log4j.Logger; import org.geotools.feature.NameImpl; @@ -50,7 +51,7 @@ */ public class AttributeMetadata implements Copyable, Comparable { - + static private final Logger LOGGER = Logger .getLogger(AttributeMetadata.class); @@ -75,9 +76,9 @@ /** * Allows to define general NODATA values for an attribute. e.g. -9999 can * be set and will always be interpreted as NULL internally and will usually - * be ignored. TODO not implemented yet + * be ignored. This overcomes the problem, that **/ - protected List nodataValues = new ArrayList(); + protected HashSet nodataValues = new HashSet(); /** Translation of the attribute's title **/ protected Translation title = new Translation(); @@ -92,8 +93,9 @@ protected boolean visible = true; /** - * When listed, the attributes are listed according to their {@link #weight} (heavier - * => further down) + * When listed, the attributes are listed according to their {@link #weight} + * (heavier => further down) + * * @see #compareTo(AttributeMetadata) **/ protected int weight = 0; @@ -102,13 +104,14 @@ private AttributeMetadata() { } - public AttributeMetadata(final AttributeDescriptor attDesc, final int weight, - final List langs) { + public AttributeMetadata(final AttributeDescriptor attDesc, + final int weight, final List langs) { this(attDesc.getName(), langs); setWeight(weight); } - public AttributeMetadata(final AttributeDescriptor attDesc, final List langs) { + public AttributeMetadata(final AttributeDescriptor attDesc, + final List langs) { this(attDesc.getName(), langs); } @@ -134,13 +137,26 @@ this.setName(name); this.title = title; this.desc = desc; -// -// // The THE_GEOM and shall never be visible! -// if (name.getLocalPart().equalsIgnoreCase("the_geom")) -// this.visible = false; -// else -// this.visible = visible; + this.visible = visible; + this.unit = unit; + } + /** + * Creates an {@link AttributeMetadata} object with the following + * information + * + * @param colIdx + * The column index of this attribute in the underlying + * table/dbf/etc... + * @param visible + * Shall this attribute be displayed or hidden from the user? + * @param unit + * {@link String} of the unit that the information is in + */ + public AttributeMetadata(final Name name, final Boolean visible, + final String unit) { + this.setName(name); + this.visible = visible; this.unit = unit; } @@ -153,7 +169,7 @@ } /** - * Creates a new visible {@link AttributeMetadata} + * Creates a new visible {@link AttributeMetadata} */ public AttributeMetadata(final Name name, final String defaultTitle, final List langs) { @@ -188,7 +204,7 @@ } /** - * Creates a new visible {@link AttributeMetadata} + * Creates a new visible {@link AttributeMetadata} */ public AttributeMetadata(final String localName, final String defaultTitle, final List langs) { @@ -229,13 +245,16 @@ amd.setFunctionX(getFunctionX()); amd.setFunctionA(getFunctionA()); - for (final Object nodataValue : getNodataValues()) { - amd.getNodataValues().add(nodataValue); - } + amd.setNodataValues(getNodataValues()); return amd; } + // only to be used by copyTo() + private void setNodataValues(HashSet nodataValues2) { + nodataValues = nodataValues2; + } + public Translation getDesc() { return desc; } @@ -256,13 +275,14 @@ } /** - * The fully qualified {@link Name} of the attribute, e.g. org.bla.plo:blub + * The fully qualified {@link Name} of the attribute, e.g. + * org.bla.plo:blub */ public Name getName() { return name; } - public List getNodataValues() { + public HashSet getNodataValues() { return nodataValues; } @@ -319,14 +339,19 @@ } /** - * The fully qualified {@link Name} of the attribute, e.g. org.bla.plo:blub + * The fully qualified {@link Name} of the attribute, e.g. + * org.bla.plo:blub */ public void setName(final Name name) { this.name = name; } - public void setNodataValues(final List nodataValues) { - this.nodataValues = nodataValues; + public void addNodataValue(Object nodataValue) { + this.nodataValues.add(nodataValue); + } + + public void removeNodataValue(Object nodataValue) { + this.nodataValues.remove(nodataValue); } public void setTitle(final Translation title) { @@ -343,14 +368,15 @@ /** * Shall the end-user see this attribute? + * * @param visible */ public void setVisible(final Boolean visible) { -// // The THE_GEOM and shall never be visible! -// if (name.getLocalPart().equalsIgnoreCase("the_geom")) -// this.visible = false; -// else -// this.visible = visible; + // // The THE_GEOM and shall never be visible! + // if (name.getLocalPart().equalsIgnoreCase("the_geom")) + // this.visible = false; + // else + // this.visible = visible; this.visible = visible; } @@ -358,16 +384,64 @@ public void setWeight(final int weight) { this.weight = weight; } - + /** * For nicer debugging */ @Override public String toString() { StringBuffer sb = new StringBuffer(); - if (name != null) sb.append(name.toString()+" "); - sb.append("weight="+weight+" "); - sb.append("title="+getTitle().toString()); + if (name != null) + sb.append(name.toString() + " "); + sb.append("weight=" + weight + " "); + sb.append("title=" + getTitle().toString()); return sb.toString(); } + + /** + * Takes any value object and checks it against the NODATA values. If the + * value equals a NODATA value, null is returned. Otherwise the + * same object is returned. + * + * Note: This method is called often. + */ + public Object fiterNodata(final Object value) { + if (nodataValues.contains(value)) + return null; + return value; + } + + /** + * @return a nicely formatted String containing all NODATA values. Strings + * are quoted fo that the empty String can be seen. + */ + public String getNoDataValuesFormatted() { + return formatNoDataValues(getNodataValues()); + } + + /** + * @return a nicely formatted String containing all NODATA values. Strings + * are quoted fo that the empty String can be seen. + */ + public static String formatNoDataValues(Set list) { + String nicelyFormatted = ""; + if (list != null) { + if (list.size() == 0) + nicelyFormatted = ""; + else { + for (Object ndo : list) { + if (ndo instanceof String) + nicelyFormatted += "\"" + ndo + "\""; + else + nicelyFormatted += ndo.toString(); + + nicelyFormatted += ","; + } + // Remove the extra comma + nicelyFormatted = nicelyFormatted.substring(0, nicelyFormatted + .length() - 1); + } + } + return nicelyFormatted; + } }