KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > runtime > format > NondecimalIntegralConversion


1 /***
2  * Retrotranslator: a Java bytecode transformer that translates Java classes
3  * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
4  *
5  * Copyright (c) 2005 - 2007 Taras Puchko
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holders nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package net.sf.retrotranslator.runtime.format;
33
34 import java.math.BigInteger JavaDoc;
35
36 /**
37  * @author Taras Puchko
38  */

39 abstract class NondecimalIntegralConversion extends NumericConversion {
40
41     public void format(FormatContext context) {
42         context.checkWidth();
43         context.assertNoPrecision();
44         context.assertNoFlag(',');
45         context.checkFlags();
46         Object JavaDoc argument = context.getArgument();
47         if (argument instanceof Byte JavaDoc) {
48             printf(context, (Byte JavaDoc) argument, Byte.SIZE);
49         } else if (argument instanceof Short JavaDoc) {
50             printf(context, (Short JavaDoc) argument, Short.SIZE);
51         } else if (argument instanceof Integer JavaDoc) {
52             printf(context, (Integer JavaDoc) argument, Integer.SIZE);
53         } else if (argument instanceof Long JavaDoc) {
54             printf(context, (Long JavaDoc) argument, Long.SIZE);
55         } else if (argument instanceof BigInteger JavaDoc) {
56             printf(context, (BigInteger JavaDoc) argument);
57         } else if (argument == null) {
58             context.writePadded(String.valueOf(argument));
59         } else {
60             throw context.getConversionException();
61         }
62     }
63
64     private void printf(FormatContext context, long argument, int size) {
65         context.assertNoFlag('(');
66         context.assertNoFlag(' ');
67         context.assertNoFlag('+');
68         long value = argument >= 0 || size == Long.SIZE ? argument : argument + (1L << size);
69         printf(context, false, toUnsignedString(value));
70     }
71
72     private void printf(FormatContext context, BigInteger JavaDoc argument) {
73         printf(context, argument.signum() < 0, toSignedString(argument.abs()));
74     }
75
76     private void printf(FormatContext context, boolean negative, String JavaDoc value) {
77         String JavaDoc prefix = context.isFlag('#') ? getRadixIndicator() : null;
78         printNumber(context, negative, prefix, new StringBuilder JavaDoc(value), context.getSymbols(false));
79     }
80
81     protected abstract String JavaDoc getRadixIndicator();
82
83     protected abstract String JavaDoc toUnsignedString(long value);
84
85     protected abstract String JavaDoc toSignedString(BigInteger JavaDoc value);
86
87     public static class OctalConversion extends NondecimalIntegralConversion {
88
89         protected String JavaDoc getRadixIndicator() {
90             return "0";
91         }
92
93         protected String JavaDoc toUnsignedString(long value) {
94             return Long.toOctalString(value);
95         }
96
97         protected String JavaDoc toSignedString(BigInteger JavaDoc value) {
98             return value.toString(8);
99         }
100     }
101
102     public static class HexadecimalConversion extends NondecimalIntegralConversion {
103
104         protected String JavaDoc getRadixIndicator() {
105             return "0x";
106         }
107
108         protected String JavaDoc toUnsignedString(long value) {
109             return Long.toHexString(value);
110         }
111
112         protected String JavaDoc toSignedString(BigInteger JavaDoc value) {
113             return value.toString(16);
114         }
115     }
116
117 }
118
Popular Tags