KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > format > DoubleNaNHandler


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.format;
14
15 /**
16  * Parses and formats double numbers, treating Double.NaN as empty string
17  * and vice versa
18  */

19 public class DoubleNaNHandler extends DoubleHandler {
20   /*
21    * if empty - return Double.NaN
22    */

23   public Object JavaDoc parse(String JavaDoc s, String JavaDoc userPattern) throws FormatException {
24     if (s != null && s.length() == 0) {
25       return new Double JavaDoc(Double.NaN);
26     } else {
27       return super.parse(s, userPattern);
28     }
29   }
30
31   /*
32    * if Double.NaN, return empty instead of ?
33    */

34   public String JavaDoc format(Object JavaDoc o, String JavaDoc userPattern) {
35     if (o instanceof Double JavaDoc && Double.isNaN(((Double JavaDoc)o).doubleValue())) {
36       return "";
37     } else {
38       return super.format(o, userPattern);
39     }
40   }
41 }
42
Popular Tags