Clover coverage report - Maven Clover report
Coverage timestamp: Thu Jan 5 2006 14:51:54 ART
file stats: LOC: 137   Methods: 11
NCLOC: 68   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
FixedFormatWritter.java 83.3% 90.9% 72.7% 86%
coverage coverage
 1    /*
 2    * Created on Oct 31, 2005
 3    *
 4    */
 5    package net.sf.ffo;
 6   
 7    import java.io.IOException;
 8    import java.io.Writer;
 9    import java.text.SimpleDateFormat;
 10    import java.util.Calendar;
 11   
 12    import org.apache.commons.lang.Validate;
 13   
 14    /**
 15    * Simple class to help writing fixed sized fields.
 16    *
 17    * @author mgriffa
 18    * @version $Id: FixedFormatWritter.java,v 1.7 2005/12/26 05:33:26 mikkey Exp $
 19    */
 20    public class FixedFormatWritter {
 21    private final Writer writer;
 22    private String defaultCalendarFormat = "yyyyMMdd";;
 23   
 24  18 public FixedFormatWritter(final Writer writer) {
 25  18 this.writer = writer;
 26    }
 27   
 28  2 public void write(final Integer val, final int len) throws IOException {
 29  2 Validate.notNull(val);
 30  2 write(""+val, len, '0', true);
 31    }
 32   
 33  0 public void write(final int val, final int len) throws IOException {
 34  0 write(""+val, len, '0', true);
 35    }
 36   
 37  2 public void write(final Long val, final int len) throws IOException {
 38  2 Validate.notNull(val);
 39  2 write(""+val, len, '0', true);
 40    }
 41   
 42  1 public void write(final String val, final int len) throws IOException {
 43  1 write(val, len, ' ', false);
 44    }
 45   
 46    /**
 47    * Writes the value with given format parameters.
 48    *
 49    * @param val the value to write
 50    * @param len length to use for the field, must be equal or greater than val.length()
 51    * @param pad the padding character to use
 52    * @param alignRight flag to align the value to the right (otherwise is left)
 53    * @throws IOException
 54    */
 55  17 public void write(final String val, final int len, final char pad, final boolean alignRight) throws IOException {
 56  17 Validate.notNull(val, "String val is null");
 57  17 Validate.isTrue(len>0, "Negative or zero field length");
 58  17 Validate.isTrue(val.length()<=len, "'"+val+"' does not fit a field of "+len+" bytes");
 59   
 60  17 final StringBuffer chunk = new StringBuffer(len);
 61   
 62  17 for (int i=0;i<len;i++)
 63  136 chunk.append(pad);
 64   
 65  17 if (alignRight) {
 66  16 chunk.replace(chunk.length()-val.length(), chunk.length(), val);
 67    } else {
 68  1 chunk.replace(0, val.length(), val);
 69    }
 70   
 71  17 writer.write(chunk.toString());
 72    }
 73   
 74   
 75    /**
 76    * Warning: this method uses intenally SimpleDateFormat with all its limitations
 77    * @param cal
 78    * @throws IOException
 79    */
 80  0 public void write(final Calendar cal) throws IOException {
 81  0 internalWrite(defaultCalendarFormat, cal);
 82    }
 83   
 84   
 85  1 private void internalWrite(final String fmt, final Calendar cal) throws IOException {
 86  1 Validate.notNull(cal, "Calendar es null");
 87  1 final SimpleDateFormat sdf = new SimpleDateFormat(fmt);
 88  1 final String formated = sdf.format(cal.getTime());
 89  1 writer.write(formated);
 90    }
 91   
 92   
 93    /**
 94    * Write the double with specified amount of characters for integer part and decimal part.
 95    * no decimal separator is written.
 96    *
 97    * @param field the value to format
 98    * @param integerCount
 99    * @param decimalCount
 100    * @throws IOException
 101    */
 102  11 public void write(final Double field, final int integerCount, final int decimalCount) throws IOException {
 103  11 Validate.notNull(field, "field to write is null");
 104    // int integerPart = getIntegerPart(field);
 105    // int decimalPart = getDecimalPart(field);
 106    //
 107  11 final String fmt = "%.0"+decimalCount+"f";
 108  11 System.out.println("fmt: "+fmt);
 109    //String s = Formatter2.format(field.doubleValue(), fmt);
 110  11 final PrintfFormat pfmt = new PrintfFormat(fmt);
 111  11 final String formatted = pfmt.sprintf(field.doubleValue());
 112   
 113    //write(integerPart, integerCount);
 114  11 final StringBuffer sb = new StringBuffer(formatted);
 115  11 if (formatted.indexOf('.')>=0) {
 116  11 sb.deleteCharAt(formatted.indexOf('.'));
 117    }
 118  11 write(sb.toString(), decimalCount+integerCount, '0', true);
 119    }
 120   
 121   
 122    /**
 123    * Warning: this method uses intenally SimpleDateFormat with all its limitations
 124    *
 125    * @param fmt the format string to pass to simple date format
 126    * @param cal
 127    * @throws IOException
 128    */
 129  1 public void write(final String fmt, final Calendar cal) throws IOException {
 130  1 internalWrite(fmt, cal);
 131    }
 132   
 133  0 public Writer getWriter() {
 134  0 return this.writer;
 135    }
 136   
 137    }