extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/pdf/NumericFormatter.java

Code
Comments
Other
Rev Date Author Line
6027 28 Oct 20 nicklas 1 package net.sf.basedb.reggie.pdf;
6027 28 Oct 20 nicklas 2
6027 28 Oct 20 nicklas 3 import java.text.NumberFormat;
6027 28 Oct 20 nicklas 4
6027 28 Oct 20 nicklas 5 import net.sf.basedb.core.Annotatable;
6027 28 Oct 20 nicklas 6 import net.sf.basedb.core.DbControl;
6027 28 Oct 20 nicklas 7 import net.sf.basedb.reggie.converter.ValueConverter;
6027 28 Oct 20 nicklas 8 import net.sf.basedb.reggie.dao.Annotationtype;
6027 28 Oct 20 nicklas 9 import net.sf.basedb.reggie.dao.ReggieItem;
6027 28 Oct 20 nicklas 10 import net.sf.basedb.util.formatter.Formatter;
6027 28 Oct 20 nicklas 11
6027 28 Oct 20 nicklas 12 /**
6027 28 Oct 20 nicklas 13   Implements formatting of numeric values with optional
6027 28 Oct 20 nicklas 14   conversion of value and unit. This is basically a
6027 28 Oct 20 nicklas 15   ValueConverter implementation with some extra utility
6027 28 Oct 20 nicklas 16   methods for getting annotation values from items.
6027 28 Oct 20 nicklas 17   
6027 28 Oct 20 nicklas 18   @since 4.28
6027 28 Oct 20 nicklas 19 */
6027 28 Oct 20 nicklas 20 public class NumericFormatter
6027 28 Oct 20 nicklas 21   implements ValueConverter<Number, String>, Formatter<Number>
6027 28 Oct 20 nicklas 22 {
6027 28 Oct 20 nicklas 23
6027 28 Oct 20 nicklas 24   private final ValueConverter<? super Number, ? extends Number> converter;
6027 28 Oct 20 nicklas 25   private final NumberFormat formatter;
6027 28 Oct 20 nicklas 26   private final String suffix;
6027 28 Oct 20 nicklas 27   private final String nullValue;
6027 28 Oct 20 nicklas 28   
6027 28 Oct 20 nicklas 29   /**
6027 28 Oct 20 nicklas 30     Create a formatter using the given converter and numeric formatter. No suffix and null
6027 28 Oct 20 nicklas 31     values are returned as empty strings.
6027 28 Oct 20 nicklas 32     
6027 28 Oct 20 nicklas 33     @param converter Converter for the values before they are formatted (optional)
6027 28 Oct 20 nicklas 34     @param formatter Formatter to use for the final value
6027 28 Oct 20 nicklas 35   */
6027 28 Oct 20 nicklas 36   public NumericFormatter(ValueConverter<? super Number, ? extends Number> converter, NumberFormat formatter)
6027 28 Oct 20 nicklas 37   {
6027 28 Oct 20 nicklas 38     this(converter, formatter, "", "");
6027 28 Oct 20 nicklas 39   }
6027 28 Oct 20 nicklas 40   
6027 28 Oct 20 nicklas 41   /**
6027 28 Oct 20 nicklas 42     Create a formatter using the given converter and numeric formatter. Null values
6027 28 Oct 20 nicklas 43     are converted to the specified string and the suffix is added for non-null values.
6027 28 Oct 20 nicklas 44     
6027 28 Oct 20 nicklas 45     @param converter Converter for the values before they are formatted (optional)
6027 28 Oct 20 nicklas 46     @param formatter Formatter to use for the final value
6027 28 Oct 20 nicklas 47   */
6027 28 Oct 20 nicklas 48   public NumericFormatter(ValueConverter<? super Number, ? extends Number> converter, NumberFormat formatter, String suffix, String nullValue)
6027 28 Oct 20 nicklas 49   {
6027 28 Oct 20 nicklas 50     this.converter = converter;
6027 28 Oct 20 nicklas 51     this.formatter = formatter;
6027 28 Oct 20 nicklas 52     this.suffix = suffix;
6027 28 Oct 20 nicklas 53     this.nullValue = nullValue;
6027 28 Oct 20 nicklas 54   }
6027 28 Oct 20 nicklas 55   
6027 28 Oct 20 nicklas 56   /**
6027 28 Oct 20 nicklas 57     Get the specified annotation value from the item and return the formatted string.
6027 28 Oct 20 nicklas 58   */
6027 28 Oct 20 nicklas 59   public String format(DbControl dc, Annotationtype at, ReggieItem<?> item) 
6027 28 Oct 20 nicklas 60   {
6027 28 Oct 20 nicklas 61     return convert((Number)at.getAnnotationValue(dc, item.getItem()));
6027 28 Oct 20 nicklas 62   }
6027 28 Oct 20 nicklas 63   
6027 28 Oct 20 nicklas 64   /**
6027 28 Oct 20 nicklas 65     Get the specified annotation value from the item and return the formatted string.
6027 28 Oct 20 nicklas 66   */
6027 28 Oct 20 nicklas 67   public String format(DbControl dc, Annotationtype at, Annotatable item) 
6027 28 Oct 20 nicklas 68   {
6027 28 Oct 20 nicklas 69     return convert((Number)at.getAnnotationValue(dc, item));
6027 28 Oct 20 nicklas 70   }
6027 28 Oct 20 nicklas 71
6027 28 Oct 20 nicklas 72   /**
6027 28 Oct 20 nicklas 73     Same as convert(value)
6027 28 Oct 20 nicklas 74   */
6027 28 Oct 20 nicklas 75   @Override
6027 28 Oct 20 nicklas 76   public String format(Number value)
6027 28 Oct 20 nicklas 77   {
6027 28 Oct 20 nicklas 78     return convert(value);
6027 28 Oct 20 nicklas 79   }
6027 28 Oct 20 nicklas 80   
6027 28 Oct 20 nicklas 81   @Override
6027 28 Oct 20 nicklas 82   public String convert(Number value) 
6027 28 Oct 20 nicklas 83   {
6027 28 Oct 20 nicklas 84     if (converter != null && value != null) value = converter.convert(value);
6027 28 Oct 20 nicklas 85     return value == null ? nullValue : (formatter.format(value) + suffix);
6027 28 Oct 20 nicklas 86   }
6027 28 Oct 20 nicklas 87
6027 28 Oct 20 nicklas 88   @Override
6027 28 Oct 20 nicklas 89   public Number parseString(String s) 
6027 28 Oct 20 nicklas 90   {
6027 28 Oct 20 nicklas 91     throw new UnsupportedOperationException("parseString");
6027 28 Oct 20 nicklas 92   }
6027 28 Oct 20 nicklas 93
6027 28 Oct 20 nicklas 94 }