| 1 |
/* |
| 2 |
* @(#) IntegerConverter.java |
| 3 |
* |
| 4 |
* Copyright 2002 - 2003 JIDE Software. All rights reserved. |
| 5 |
*/ |
| 6 |
package com.jidesoft.converter; |
| 7 |
|
| 8 |
import com.jidesoft.utils.SystemInfo; |
| 9 |
|
| 10 |
import java.math.RoundingMode; |
| 11 |
import java.text.DecimalFormat; |
| 12 |
import java.text.NumberFormat; |
| 13 |
import java.text.ParseException; |
| 14 |
import java.util.Currency; |
| 15 |
import java.util.Locale; |
| 16 |
|
| 17 |
/** |
| 18 |
* Converter which converts Number to String and converts it back. You can pass in a NumberFormat as UserObject of |
| 19 |
* ConverterContext if you want to control the format of the number such as maximum decimal point etc. |
| 20 |
*/ |
| 21 |
abstract public class NumberConverter implements ObjectConverter { |
| 22 |
private NumberFormat _numberFormat; |
| 23 |
private boolean _avoidNegativeZero; |
| 24 |
|
| 25 |
public static final ConverterContext CONTEXT_FRACTION_NUMBER = new ConverterContext("Fraction Number"); |
| 26 |
|
| 27 |
/** |
| 28 |
* Creates a number converter with no NumberFormat. |
| 29 |
*/ |
| 30 |
public NumberConverter() { |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Creates the number converter with a specified NumberFormat. |
| 35 |
* |
| 36 |
* @param format the number format. |
| 37 |
*/ |
| 38 |
public NumberConverter(NumberFormat format) { |
| 39 |
_numberFormat = format; |
| 40 |
if (_numberFormat != null) { |
| 41 |
_numberFormat.setGroupingUsed(isGroupingUsed()); // apply the grouingUsed flag. |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
private String trimNegativeSign(String text) { |
| 46 |
if (text.indexOf('-') != 0) { |
| 47 |
return text; |
| 48 |
} |
| 49 |
for (char ch = '1'; ch <= '9'; ch++) { |
| 50 |
if (text.indexOf(ch) >= 0) { |
| 51 |
return text; |
| 52 |
} |
| 53 |
} |
| 54 |
for (char ch = 'a'; ch <= 'f'; ch++) { |
| 55 |
if (text.indexOf(ch) >= 0) { |
| 56 |
return text; |
| 57 |
} |
| 58 |
} |
| 59 |
for (char ch = 'A'; ch <= 'F'; ch++) { |
| 60 |
if (text.indexOf(ch) >= 0) { |
| 61 |
return text; |
| 62 |
} |
| 63 |
} |
| 64 |
if (text.indexOf('0') < 0) { |
| 65 |
return text; |
| 66 |
} |
| 67 |
return text.substring(1); |
| 68 |
} |
| 69 |
|
| 70 |
public String toString(Object object, ConverterContext context) { |
| 71 |
// format on userObject has a higher priority. |
| 72 |
try { |
| 73 |
if (context == null || context.getUserObject() == null || !(context.getUserObject() instanceof NumberFormat)) { |
| 74 |
if (object instanceof Number && ((Number) object).doubleValue() == Double.NaN) { |
| 75 |
return ""; |
| 76 |
} |
| 77 |
else { |
| 78 |
String text = getNumberFormat().format(object); |
| 79 |
if (isAvoidNegativeZero()) { |
| 80 |
text = trimNegativeSign(text); |
| 81 |
} |
| 82 |
return text; |
| 83 |
} |
| 84 |
} |
| 85 |
else { |
| 86 |
NumberFormat format = (NumberFormat) context.getUserObject(); |
| 87 |
String text = format.format(object); |
| 88 |
if (isAvoidNegativeZero()) { |
| 89 |
text = trimNegativeSign(text); |
| 90 |
} |
| 91 |
return text; |
| 92 |
} |
| 93 |
} |
| 94 |
catch (IllegalArgumentException e) { |
| 95 |
return ""; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
public boolean supportToString(Object object, ConverterContext context) { |
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
public void setNumberFormat(NumberFormat numberFormat) { |
| 104 |
_numberFormat = numberFormat; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Gets the NumberFormat. Of setNumberFormat is never called, it will return DecimalFormat.getInstance(). |
| 109 |
* |
| 110 |
* @return the NumberFormat. |
| 111 |
*/ |
| 112 |
protected NumberFormat getNumberFormat() { |
| 113 |
if (_numberFormat == null) { |
| 114 |
_numberFormat = DecimalFormat.getInstance(); |
| 115 |
_numberFormat.setGroupingUsed(isGroupingUsed()); |
| 116 |
} |
| 117 |
return _numberFormat; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Gets the NumberFormat for Locale.US. |
| 122 |
* |
| 123 |
* @return the NumberFormat for Locale.US. |
| 124 |
*/ |
| 125 |
protected NumberFormat getDefaultNumberFormat() { |
| 126 |
NumberFormat format = DecimalFormat.getInstance(Locale.US); |
| 127 |
format.setGroupingUsed(isGroupingUsed()); |
| 128 |
return format; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Parse the string as number. It will try using getNumberFormat first then try getDefaultNumberFormat which is the |
| 133 |
* US locale number format. |
| 134 |
* |
| 135 |
* @param string the string |
| 136 |
* @return the Number. Null if the string is not a number. |
| 137 |
*/ |
| 138 |
protected Number parseNumber(String string) { |
| 139 |
Number number; |
| 140 |
try { |
| 141 |
number = getNumberFormat().parse(string); |
| 142 |
} |
| 143 |
catch (ParseException e) { |
| 144 |
try { |
| 145 |
number = getDefaultNumberFormat().parse(string); |
| 146 |
} |
| 147 |
catch (ParseException e1) { |
| 148 |
number = null; |
| 149 |
} |
| 150 |
} |
| 151 |
return number; |
| 152 |
} |
| 153 |
|
| 154 |
private static boolean _groupingUsed = true; |
| 155 |
|
| 156 |
/** |
| 157 |
* Gets flag if the grouping is used for the format. |
| 158 |
* |
| 159 |
* @return if the grouping is used. |
| 160 |
*/ |
| 161 |
public static boolean isGroupingUsed() { |
| 162 |
return _groupingUsed; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Sets if the grouping will be used for the NumberFormat. We will call NumberFormat#setGroupingUsed when we create |
| 167 |
* the NumberFormat. Default is true. If you want to call this method, please make sure calling it before |
| 168 |
* ObjectConverterManager is used. |
| 169 |
* |
| 170 |
* @param groupingUsed true or false. |
| 171 |
*/ |
| 172 |
public static void setGroupingUsed(boolean groupingUsed) { |
| 173 |
_groupingUsed = groupingUsed; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Set the fraction digits of this converter. |
| 178 |
* |
| 179 |
* @param minDigits minimum fraction digits |
| 180 |
* @param maxDigits maximum fraction digits |
| 181 |
*/ |
| 182 |
public void setFractionDigits(int minDigits, int maxDigits) { |
| 183 |
NumberFormat numberFormat = getNumberFormat(); |
| 184 |
numberFormat.setMinimumFractionDigits(minDigits); |
| 185 |
numberFormat.setMaximumFractionDigits(maxDigits); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Set the currency of this converter. |
| 190 |
* |
| 191 |
* @param currency currency |
| 192 |
*/ |
| 193 |
public void setCurrency(Currency currency) { |
| 194 |
NumberFormat numberFormat = getNumberFormat(); |
| 195 |
numberFormat.setCurrency(currency); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Set the integer digits of this converter. |
| 200 |
* |
| 201 |
* @param minDigits minimum integer digits |
| 202 |
* @param maxDigits maximum integer digits |
| 203 |
*/ |
| 204 |
public void setIntegerDigits(int minDigits, int maxDigits) { |
| 205 |
NumberFormat numberFormat = getNumberFormat(); |
| 206 |
numberFormat.setMinimumIntegerDigits(minDigits); |
| 207 |
numberFormat.setMaximumIntegerDigits(maxDigits); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Set the rounding mode of this converter. |
| 212 |
* |
| 213 |
* @param mode rounding mode |
| 214 |
*/ |
| 215 |
public void setRoundingMode(RoundingMode mode) { |
| 216 |
if (SystemInfo.isJdk6Above()) { |
| 217 |
NumberFormat numberFormat = getNumberFormat(); |
| 218 |
numberFormat.setRoundingMode(mode); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Gets the flag indicating if negative zero should be avoided. |
| 224 |
* |
| 225 |
* @return true if negative zero should be avoided. Otherwise false. |
| 226 |
* @see #setAvoidNegativeZero(boolean) |
| 227 |
* @since 3.5.1 |
| 228 |
*/ |
| 229 |
public boolean isAvoidNegativeZero() { |
| 230 |
return _avoidNegativeZero; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Sets the flag indicating if negative zero should be avoided. |
| 235 |
* <p/> |
| 236 |
* By default, the value is false to keep backward compatibility. If you don't like the string like "-0.00", please |
| 237 |
* set this flag to true to remove the negative sign. |
| 238 |
* |
| 239 |
* @param avoidNegativeZero the flag |
| 240 |
* @since 3.5.1 |
| 241 |
*/ |
| 242 |
public void setAvoidNegativeZero(boolean avoidNegativeZero) { |
| 243 |
_avoidNegativeZero = avoidNegativeZero; |
| 244 |
} |
| 245 |
} |