2733 |
16 Oct 06 |
nicklas |
1 |
/** |
2733 |
16 Oct 06 |
nicklas |
$Id$ |
2733 |
16 Oct 06 |
nicklas |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2006 Nicklas Nordborg |
2733 |
16 Oct 06 |
nicklas |
5 |
|
2733 |
16 Oct 06 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
2733 |
16 Oct 06 |
nicklas |
Available at http://base.thep.lu.se/ |
2733 |
16 Oct 06 |
nicklas |
8 |
|
2733 |
16 Oct 06 |
nicklas |
BASE is free software; you can redistribute it and/or |
2733 |
16 Oct 06 |
nicklas |
modify it under the terms of the GNU General Public License |
4479 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
2733 |
16 Oct 06 |
nicklas |
of the License, or (at your option) any later version. |
2733 |
16 Oct 06 |
nicklas |
13 |
|
2733 |
16 Oct 06 |
nicklas |
BASE is distributed in the hope that it will be useful, |
2733 |
16 Oct 06 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
2733 |
16 Oct 06 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2733 |
16 Oct 06 |
nicklas |
GNU General Public License for more details. |
2733 |
16 Oct 06 |
nicklas |
18 |
|
2733 |
16 Oct 06 |
nicklas |
You should have received a copy of the GNU General Public License |
4515 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
2733 |
16 Oct 06 |
nicklas |
21 |
*/ |
2942 |
22 Nov 06 |
nicklas |
22 |
package net.sf.basedb.util.formatter; |
2733 |
16 Oct 06 |
nicklas |
23 |
|
8144 |
21 Apr 23 |
nicklas |
24 |
import java.text.DecimalFormat; |
8144 |
21 Apr 23 |
nicklas |
25 |
import java.text.DecimalFormatSymbols; |
8144 |
21 Apr 23 |
nicklas |
26 |
import java.text.NumberFormat; |
8144 |
21 Apr 23 |
nicklas |
27 |
|
7647 |
13 Mar 19 |
nicklas |
28 |
import net.sf.basedb.util.excel.ExcelFormatter; |
7647 |
13 Mar 19 |
nicklas |
29 |
import net.sf.basedb.util.excel.ExcelValue; |
2942 |
22 Nov 06 |
nicklas |
30 |
|
2733 |
16 Oct 06 |
nicklas |
31 |
/** |
2733 |
16 Oct 06 |
nicklas |
Formats a number as an integer. |
2733 |
16 Oct 06 |
nicklas |
33 |
|
2733 |
16 Oct 06 |
nicklas |
@author nicklas |
3028 |
14 Dec 06 |
nicklas |
@version 2.2 |
2733 |
16 Oct 06 |
nicklas |
@base.modified $Date$ |
2733 |
16 Oct 06 |
nicklas |
37 |
*/ |
2733 |
16 Oct 06 |
nicklas |
38 |
public class IntegerFormatter |
7647 |
13 Mar 19 |
nicklas |
39 |
implements Formatter<Number>, ExcelFormatter<Number, Number> |
2733 |
16 Oct 06 |
nicklas |
40 |
{ |
2733 |
16 Oct 06 |
nicklas |
41 |
|
3028 |
14 Dec 06 |
nicklas |
42 |
private final boolean longValues; |
8144 |
21 Apr 23 |
nicklas |
43 |
private final NumberFormat format; |
3028 |
14 Dec 06 |
nicklas |
44 |
|
2733 |
16 Oct 06 |
nicklas |
45 |
/** |
2733 |
16 Oct 06 |
nicklas |
Create a new integer formatter. |
3028 |
14 Dec 06 |
nicklas |
@param longValues If the {@link #parseString(String)} method should return long |
3028 |
14 Dec 06 |
nicklas |
or int values |
2733 |
16 Oct 06 |
nicklas |
49 |
*/ |
3028 |
14 Dec 06 |
nicklas |
50 |
public IntegerFormatter(boolean longValues) |
3028 |
14 Dec 06 |
nicklas |
51 |
{ |
3028 |
14 Dec 06 |
nicklas |
52 |
this.longValues = longValues; |
8144 |
21 Apr 23 |
nicklas |
53 |
this.format = null; |
3028 |
14 Dec 06 |
nicklas |
54 |
} |
2733 |
16 Oct 06 |
nicklas |
55 |
|
8144 |
21 Apr 23 |
nicklas |
56 |
/** |
8144 |
21 Apr 23 |
nicklas |
Create a new integer formatter with a thousands separator. |
8144 |
21 Apr 23 |
nicklas |
@param longValues If the {@link #parseString(String)} method should return long |
8144 |
21 Apr 23 |
nicklas |
or int values |
8144 |
21 Apr 23 |
nicklas |
@since 3.19.8 |
8144 |
21 Apr 23 |
nicklas |
61 |
*/ |
8144 |
21 Apr 23 |
nicklas |
62 |
public IntegerFormatter(boolean longValues, Character thousandsSeparator) |
8144 |
21 Apr 23 |
nicklas |
63 |
{ |
8144 |
21 Apr 23 |
nicklas |
64 |
this.longValues = longValues; |
8144 |
21 Apr 23 |
nicklas |
65 |
if (thousandsSeparator == null) |
8144 |
21 Apr 23 |
nicklas |
66 |
{ |
8144 |
21 Apr 23 |
nicklas |
67 |
this.format = null; |
8144 |
21 Apr 23 |
nicklas |
68 |
} |
8144 |
21 Apr 23 |
nicklas |
69 |
else |
8144 |
21 Apr 23 |
nicklas |
70 |
{ |
8144 |
21 Apr 23 |
nicklas |
71 |
DecimalFormatSymbols dfs = new DecimalFormatSymbols(); |
8144 |
21 Apr 23 |
nicklas |
72 |
dfs.setGroupingSeparator(thousandsSeparator); |
8144 |
21 Apr 23 |
nicklas |
73 |
this.format = new DecimalFormat("#,##0", dfs); |
8144 |
21 Apr 23 |
nicklas |
74 |
} |
8144 |
21 Apr 23 |
nicklas |
75 |
} |
8144 |
21 Apr 23 |
nicklas |
76 |
|
2733 |
16 Oct 06 |
nicklas |
77 |
/* |
2733 |
16 Oct 06 |
nicklas |
From the Formatter interface |
2733 |
16 Oct 06 |
nicklas |
79 |
------------------------------------------- |
2733 |
16 Oct 06 |
nicklas |
80 |
*/ |
6127 |
14 Sep 12 |
nicklas |
81 |
@Override |
2733 |
16 Oct 06 |
nicklas |
82 |
public String format(Number value) |
2733 |
16 Oct 06 |
nicklas |
83 |
{ |
8144 |
21 Apr 23 |
nicklas |
84 |
return value == null ? "" : |
8144 |
21 Apr 23 |
nicklas |
85 |
format != null ? format.format(value.longValue()) : String.valueOf(value.longValue()); |
2733 |
16 Oct 06 |
nicklas |
86 |
} |
6127 |
14 Sep 12 |
nicklas |
87 |
@Override |
2942 |
22 Nov 06 |
nicklas |
88 |
public Number parseString(String value) |
2942 |
22 Nov 06 |
nicklas |
89 |
{ |
5432 |
29 Sep 10 |
nicklas |
90 |
if (value == null) return null; |
3028 |
14 Dec 06 |
nicklas |
91 |
Double d = Double.valueOf(value); |
3028 |
14 Dec 06 |
nicklas |
92 |
Number n = null; |
3028 |
14 Dec 06 |
nicklas |
93 |
if (longValues) |
3028 |
14 Dec 06 |
nicklas |
94 |
{ |
3028 |
14 Dec 06 |
nicklas |
95 |
n = Long.valueOf(d.longValue()); |
3028 |
14 Dec 06 |
nicklas |
96 |
} |
3028 |
14 Dec 06 |
nicklas |
97 |
else |
3028 |
14 Dec 06 |
nicklas |
98 |
{ |
3028 |
14 Dec 06 |
nicklas |
99 |
n = Integer.valueOf(d.intValue()); |
3028 |
14 Dec 06 |
nicklas |
100 |
} |
3028 |
14 Dec 06 |
nicklas |
101 |
return n; |
2942 |
22 Nov 06 |
nicklas |
102 |
} |
2733 |
16 Oct 06 |
nicklas |
103 |
// ------------------------------------------- |
2733 |
16 Oct 06 |
nicklas |
104 |
|
7647 |
13 Mar 19 |
nicklas |
105 |
/** |
7647 |
13 Mar 19 |
nicklas |
@since 3.15 |
7647 |
13 Mar 19 |
nicklas |
107 |
*/ |
7647 |
13 Mar 19 |
nicklas |
108 |
@Override |
7647 |
13 Mar 19 |
nicklas |
109 |
public ExcelValue<Number> toExcelValue(Number value) |
7647 |
13 Mar 19 |
nicklas |
110 |
{ |
7647 |
13 Mar 19 |
nicklas |
111 |
return ExcelValue.asInt(value); |
7647 |
13 Mar 19 |
nicklas |
112 |
} |
7647 |
13 Mar 19 |
nicklas |
113 |
|
2733 |
16 Oct 06 |
nicklas |
114 |
} |