001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.format;
032:
033: import java.text.DecimalFormatSymbols;
034:
035: /**
036: * @author Taras Puchko
037: */
038: abstract class NumericConversion extends Conversion {
039:
040: protected static void appendNumber(StringBuilder builder,
041: String number, boolean groupingUsed,
042: DecimalFormatSymbols symbols) {
043: for (int i = 0; i < number.length(); i++) {
044: if (groupingUsed && i > 0 && (number.length() - i) % 3 == 0) {
045: builder.append(symbols.getGroupingSeparator());
046: }
047: builder
048: .append((char) (symbols.getZeroDigit() - '0' + number
049: .charAt(i)));
050: }
051: }
052:
053: protected static boolean printSpecialNumber(FormatContext context,
054: double argument) {
055: if (Double.isNaN(argument)) {
056: context.writePadded("NaN");
057: return true;
058: }
059: if (argument == Double.POSITIVE_INFINITY) {
060: context.writePadded(context.isFlag('+') ? "+Infinity"
061: : "Infinity");
062: return true;
063: }
064: if (argument == Double.NEGATIVE_INFINITY) {
065: context.writePadded(context.isFlag('(') ? "(Infinity)"
066: : "-Infinity");
067: return true;
068: }
069: return false;
070: }
071:
072: protected static void printNumber(FormatContext context,
073: boolean negative, String prefix, StringBuilder argument,
074: DecimalFormatSymbols symbols) {
075: StringBuilder builder = new StringBuilder();
076: if (negative) {
077: if (context.isFlag('(')) {
078: builder.append('(');
079: argument.append(')');
080: } else {
081: builder.append('-');
082: }
083: } else {
084: if (context.isFlag('+')) {
085: builder.append('+');
086: } else if (context.isFlag(' ')) {
087: builder.append(' ');
088: }
089: }
090: if (prefix != null) {
091: builder.append(prefix);
092: }
093: if (context.isFlag('0')) {
094: int count = context.getWidth() - builder.length()
095: - argument.length();
096: for (int i = 0; i < count; i++) {
097: builder.append(symbols.getZeroDigit());
098: }
099: }
100: context.writePadded(builder.append(argument).toString());
101: }
102:
103: }
|