@Override
public StringBuffer format(Object value, StringBuffer buffer, FieldPosition field) {
-
- if(!(value instanceof Number)) {
+ if (!(value instanceof Number)) {
throw new IllegalArgumentException();
}
- if(buffer == null || field == null) {
+ if (buffer == null || field == null) {
throw new NullPointerException();
}
-
String fieldType = getFieldType(field.getFieldAttribute());
-
Number number = (Number) value;
-
- if(number instanceof BigInteger) {
+ if (number instanceof BigInteger) {
BigInteger valBigInteger = (BigInteger) number;
- String result = NativeDecimalFormat.format(this.addr,
- valBigInteger.toString(10), field, fieldType, null, 0);
+ String result = NativeDecimalFormat.format(this.addr, valBigInteger.toString(10),
+ field, fieldType, null, 0);
return buffer.append(result);
- } else if(number instanceof BigDecimal) {
+ } else if (number instanceof BigDecimal) {
BigDecimal valBigDecimal = (BigDecimal) number;
if (getMultiplier() != 1) {
valBigDecimal = applyMultiplier(valBigDecimal);
val.append(valBigDecimal.unscaledValue().toString(10));
int scale = valBigDecimal.scale();
scale = makeScalePositive(scale, val);
- String result = NativeDecimalFormat.format(this.addr,
- val.toString(), field, fieldType, null, scale);
+ String result = NativeDecimalFormat.format(this.addr, val.toString(),
+ field, fieldType, null, scale);
return buffer.append(result);
- } else {
+ } else if (number instanceof Double || number instanceof Float) {
double dv = number.doubleValue();
+ String result = NativeDecimalFormat.format(this.addr, dv, field, fieldType, null);
+ return buffer.append(result);
+ } else {
long lv = number.longValue();
- if (dv == lv) {
- String result = NativeDecimalFormat.format(this.addr, lv, field,
- fieldType, null);
- return buffer.append(result);
- }
- String result = NativeDecimalFormat.format(this.addr, dv, field,
- fieldType, null);
+ String result = NativeDecimalFormat.format(this.addr, lv, field, fieldType, null);
return buffer.append(result);
}
}
@Override
public StringBuffer format(long value, StringBuffer buffer, FieldPosition field) {
-
- if(buffer == null || field == null) {
+ if (buffer == null || field == null) {
throw new NullPointerException();
}
-
String fieldType = getFieldType(field.getFieldAttribute());
-
- String result = NativeDecimalFormat.format(this.addr, value, field,
- fieldType, null);
-
+ String result = NativeDecimalFormat.format(this.addr, value, field, fieldType, null);
buffer.append(result.toCharArray(), 0, result.length());
-
return buffer;
}
@Override
public StringBuffer format(double value, StringBuffer buffer, FieldPosition field) {
-
- if(buffer == null || field == null) {
+ if (buffer == null || field == null) {
throw new NullPointerException();
}
-
String fieldType = getFieldType(field.getFieldAttribute());
-
- String result = NativeDecimalFormat.format(this.addr, value, field,
- fieldType, null);
-
+ String result = NativeDecimalFormat.format(this.addr, value, field, fieldType, null);
buffer.append(result.toCharArray(), 0, result.length());
-
return buffer;
}
if ('d' == currentConversionType) {
NumberFormat numberFormat = getNumberFormat();
- boolean readableName = formatToken
- .isFlagSet(FormatToken.FLAG_COMMA);
- numberFormat.setGroupingUsed(readableName);
+ numberFormat.setGroupingUsed(formatToken.isFlagSet(FormatToken.FLAG_COMMA));
result.append(numberFormat.format(bigInt));
} else if ('o' == currentConversionType) {
// convert BigInteger to a string presentation using radix 8
return specialNumberResult;
}
- if ('a' != Character.toLowerCase(currentConversionType)) {
- formatToken
- .setPrecision(formatToken.isPrecisionSet() ? formatToken
- .getPrecision()
- : FormatToken.DEFAULT_PRECISION);
+ if (Character.toLowerCase(currentConversionType) != 'a' &&
+ !formatToken.isPrecisionSet()) {
+ formatToken.setPrecision(FormatToken.DEFAULT_PRECISION);
}
+
// output result
FloatUtil floatUtil = new FloatUtil(result, formatToken,
(DecimalFormat) getNumberFormat(), arg);
- floatUtil.transform(formatToken, result);
+ floatUtil.transform();
formatToken.setPrecision(FormatToken.UNSET);
}
private static class FloatUtil {
- private StringBuilder result;
+ private final StringBuilder result;
- private DecimalFormat decimalFormat;
+ private final DecimalFormat decimalFormat;
- private FormatToken formatToken;
+ private final FormatToken formatToken;
- private Object argument;
+ private final Object argument;
- private char minusSign;
+ private final char minusSign;
FloatUtil(StringBuilder result, FormatToken formatToken,
DecimalFormat decimalFormat, Object argument) {
this.formatToken = formatToken;
this.decimalFormat = decimalFormat;
this.argument = argument;
- this.minusSign = decimalFormat.getDecimalFormatSymbols()
- .getMinusSign();
+ this.minusSign = decimalFormat.getDecimalFormatSymbols().getMinusSign();
}
- void transform(FormatToken aFormatToken, StringBuilder aResult) {
- this.result = aResult;
- this.formatToken = aFormatToken;
+ void transform() {
switch (formatToken.getConversionType()) {
case 'e':
case 'E': {