Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PercentValidator |
|
| 2.75;2.75 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | package org.apache.commons.validator.routines; | |
18 | ||
19 | import java.text.DecimalFormat; | |
20 | import java.text.Format; | |
21 | import java.math.BigDecimal; | |
22 | ||
23 | /** | |
24 | * <p><b>Percentage Validation</b> and Conversion routines (<code>java.math.BigDecimal</code>).</p> | |
25 | * | |
26 | * <p>This is one implementation of a percent validator that has the following features:</p> | |
27 | * <ul> | |
28 | * <li>It is <i>lenient</i> about the the presence of the <i>percent symbol</i></li> | |
29 | * <li>It converts the percent to a <code>java.math.BigDecimal</code></li> | |
30 | * </ul> | |
31 | * | |
32 | * <p>However any of the <i>number</i> validators can be used for <i>percent</i> validation. | |
33 | * For example, if you wanted a <i>percent</i> validator that converts to a | |
34 | * <code>java.lang.Float</code> then you can simply instantiate an | |
35 | * <code>FloatValidator</code> with the appropriate <i>format type</i>:</p> | |
36 | * | |
37 | * <p><code>... = new FloatValidator(false, FloatValidator.PERCENT_FORMAT);</code></p> | |
38 | * | |
39 | * <p>Pick the appropriate validator, depending on the type (i.e Float, Double or BigDecimal) | |
40 | * you want the percent converted to. Please note, it makes no sense to use | |
41 | * one of the validators that doesn't handle fractions (i.e. byte, short, integer, long | |
42 | * and BigInteger) since percentages are converted to fractions (i.e <code>50%</code> is | |
43 | * converted to <code>0.5</code>).</p> | |
44 | * | |
45 | * @version $Revision: 1739356 $ | |
46 | * @since Validator 1.3.0 | |
47 | */ | |
48 | public class PercentValidator extends BigDecimalValidator { | |
49 | ||
50 | private static final long serialVersionUID = -3508241924961535772L; | |
51 | ||
52 | 1 | private static final PercentValidator VALIDATOR = new PercentValidator(); |
53 | ||
54 | /** DecimalFormat's percent (thousand multiplier) symbol */ | |
55 | private static final char PERCENT_SYMBOL = '%'; | |
56 | ||
57 | 1 | private static final BigDecimal POINT_ZERO_ONE = new BigDecimal("0.01"); |
58 | ||
59 | /** | |
60 | * Return a singleton instance of this validator. | |
61 | * @return A singleton instance of the PercentValidator. | |
62 | */ | |
63 | public static BigDecimalValidator getInstance() { | |
64 | 4 | return VALIDATOR; |
65 | } | |
66 | ||
67 | /** | |
68 | * Construct a <i>strict</i> instance. | |
69 | */ | |
70 | public PercentValidator() { | |
71 | 4 | this(true); |
72 | 4 | } |
73 | ||
74 | /** | |
75 | * Construct an instance with the specified strict setting. | |
76 | * | |
77 | * @param strict <code>true</code> if strict | |
78 | * <code>Format</code> parsing should be used. | |
79 | */ | |
80 | public PercentValidator(boolean strict) { | |
81 | 4 | super(strict, PERCENT_FORMAT, true); |
82 | 4 | } |
83 | ||
84 | /** | |
85 | * <p>Parse the value with the specified <code>Format</code>.</p> | |
86 | * | |
87 | * <p>This implementation is lenient whether the currency symbol | |
88 | * is present or not. The default <code>NumberFormat</code> | |
89 | * behaviour is for the parsing to "fail" if the currency | |
90 | * symbol is missing. This method re-parses with a format | |
91 | * without the currency symbol if it fails initially.</p> | |
92 | * | |
93 | * @param value The value to be parsed. | |
94 | * @param formatter The Format to parse the value with. | |
95 | * @return The parsed value if valid or <code>null</code> if invalid. | |
96 | */ | |
97 | @Override | |
98 | protected Object parse(String value, Format formatter) { | |
99 | ||
100 | // Initial parse of the value | |
101 | 13 | BigDecimal parsedValue = (BigDecimal)super.parse(value, formatter); |
102 | 13 | if (parsedValue != null || !(formatter instanceof DecimalFormat)) { |
103 | 7 | return parsedValue; |
104 | } | |
105 | ||
106 | // Re-parse using a pattern without the percent symbol | |
107 | 6 | DecimalFormat decimalFormat = (DecimalFormat)formatter; |
108 | 6 | String pattern = decimalFormat.toPattern(); |
109 | 6 | if (pattern.indexOf(PERCENT_SYMBOL) >= 0) { |
110 | 6 | StringBuilder buffer = new StringBuilder(pattern.length()); |
111 | 42 | for (int i = 0; i < pattern.length(); i++) { |
112 | 36 | if (pattern.charAt(i) != PERCENT_SYMBOL) { |
113 | 30 | buffer.append(pattern.charAt(i)); |
114 | } | |
115 | } | |
116 | 6 | decimalFormat.applyPattern(buffer.toString()); |
117 | 6 | parsedValue = (BigDecimal)super.parse(value, decimalFormat); |
118 | ||
119 | // If parsed OK, divide by 100 to get percent | |
120 | 6 | if (parsedValue != null) { |
121 | 2 | parsedValue = parsedValue.multiply(POINT_ZERO_ONE); |
122 | } | |
123 | ||
124 | } | |
125 | 6 | return parsedValue; |
126 | } | |
127 | } |