Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PercentageFormatter |
|
| 6.0;6 |
1 | /* | |
2 | * Copyright 2004 Jonathan M. Lehr | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. | |
5 | * You may obtain a copy of the License at | |
6 | * | |
7 | * http://www.apache.org/licenses/LICENSE-2.0 | |
8 | * | |
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" | |
10 | * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language | |
11 | * governing permissions and limitations under the License. | |
12 | * | |
13 | * MODIFIED BY THE KUALI FOUNDATION | |
14 | */ | |
15 | // begin Kuali Foundation modification | |
16 | package org.kuali.rice.kns.web.format; | |
17 | // end Kuali Foundation modification | |
18 | ||
19 | // begin Kuali Foundation modification | |
20 | import java.math.BigDecimal; | |
21 | import java.text.DecimalFormat; | |
22 | import java.text.NumberFormat; | |
23 | import java.text.ParseException; | |
24 | ||
25 | import org.kuali.rice.kns.util.KualiDecimal; | |
26 | import org.kuali.rice.kns.util.KualiPercent; | |
27 | import org.kuali.rice.kns.util.RiceKeyConstants; | |
28 | ||
29 | /** | |
30 | * begin Kuali Foundation modification | |
31 | * This class is used to format objects as a percent. | |
32 | * end Kuali Foundation modification | |
33 | */ | |
34 | 0 | public class PercentageFormatter extends Formatter { |
35 | // begin Kuali Foundation modification | |
36 | private static final long serialVersionUID = 1323889942436009589L; | |
37 | // end Kuali Foundation modification | |
38 | ||
39 | /** | |
40 | * The default scale for percentage values | |
41 | */ | |
42 | public final static int PERCENTAGE_SCALE = 2; | |
43 | ||
44 | /** | |
45 | * The default format for percentage values | |
46 | */ | |
47 | public final static String PERCENTAGE_FORMAT = "#,##0.00"; | |
48 | ||
49 | // begin Kuali Foundation modification | |
50 | // removed PARSE_MSG | |
51 | // end Kuali Foundation modification | |
52 | ||
53 | /** | |
54 | * Unformats its argument and returns a BigDecimal instance initialized with the resulting string value | |
55 | * | |
56 | * @return a BigDecimal initialized with the provided string | |
57 | */ | |
58 | protected Object convertToObject(String target) { | |
59 | // begin Kuali Foundation modification | |
60 | // using KualiPercent instead of BigDecimal, and exception msg changes | |
61 | try { | |
62 | 0 | DecimalFormat formatter = new DecimalFormat(PERCENTAGE_FORMAT); |
63 | 0 | Number parsedNumber = formatter.parse(target.trim()); |
64 | 0 | return new KualiPercent(parsedNumber.doubleValue()); |
65 | } | |
66 | 0 | catch (NumberFormatException e) { |
67 | 0 | throw new FormatException("parsing", RiceKeyConstants.ERROR_PERCENTAGE, target, e); |
68 | } | |
69 | 0 | catch (ParseException e) { |
70 | 0 | throw new FormatException("parsing", RiceKeyConstants.ERROR_PERCENTAGE, target, e); |
71 | } | |
72 | // end Kuali Foundation modification | |
73 | } | |
74 | ||
75 | /** | |
76 | * Returns a string representation of its argument, formatted as a percentage value. | |
77 | * | |
78 | * @return a formatted String | |
79 | */ | |
80 | public Object format(Object value) { | |
81 | 0 | if (value == null) |
82 | 0 | return "N/A"; |
83 | ||
84 | 0 | String stringValue = ""; |
85 | try { | |
86 | 0 | if (value instanceof KualiDecimal) { |
87 | 0 | value = ((KualiDecimal)value).bigDecimalValue(); |
88 | } | |
89 | 0 | BigDecimal bigDecValue = (BigDecimal) value; |
90 | 0 | bigDecValue = bigDecValue.setScale(PERCENTAGE_SCALE, BigDecimal.ROUND_HALF_UP); |
91 | 0 | stringValue = NumberFormat.getInstance().format(bigDecValue.doubleValue()); |
92 | } | |
93 | 0 | catch (IllegalArgumentException iae) { |
94 | // begin Kuali Foundation modification | |
95 | 0 | throw new FormatException("formatting", RiceKeyConstants.ERROR_PERCENTAGE, value.toString(), iae); |
96 | // end Kuali Foundation modification | |
97 | 0 | } |
98 | ||
99 | 0 | return stringValue + " percent"; |
100 | } | |
101 | } |