View Javadoc

1   package net.sf.layoutParser.typeHandler;
2   
3   import net.sf.layoutParser.constants.ConstantCatalog;
4   import net.sf.layoutParser.exception.ExceptionKey;
5   import net.sf.layoutParser.processor.MalformedInputException;
6   import net.sf.layoutParser.processor.MalformedOutputException;
7   
8   /**
9    * TODO: Comentar esta classe
10   *
11   * @author Mário Valentim Junior
12   * @since 1.1
13   * @date 10/06/2008
14   */
15  public class DoubleTypeHandler extends NumberTypeHandler{
16  	
17  	public Double parse(String dados, String format, String filler) throws MalformedInputException {
18  		if(format == null){
19  			throw new MalformedInputException( ExceptionKey.PLUGIN_NO_FORMAT, new Object[]{dados, Double.class});
20  		}
21  		
22  		String value = transform(dados, format);
23  		return super.parse(value, format, filler).doubleValue();
24  	}
25  
26  	private String transform(String dados, String format) {
27  		String[] split = format.split("\\"+ ConstantCatalog.DOT);
28  		int decimalPositions = split.length == 1 ? 0 : split[split.length - 1].length();
29  		int separatorIndex = dados.length() - decimalPositions;
30  		String value = dados.substring(0, separatorIndex) + ConstantCatalog.DOT;
31  		value += dados.substring(separatorIndex);
32  		return value;
33  	}
34  
35  	public String format(Number data, String format, String filler, int size) throws MalformedOutputException {
36  		if(format == null){
37  			throw new MalformedOutputException( ExceptionKey.PLUGIN_NO_FORMAT, new Object[]{data, Double.class});
38  		}
39  		
40  		return super.format(data, format, filler, size);
41  	}
42  
43  }