KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > kawa > functions > IntegerFormat


1 // Copyright (c) 2001 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.kawa.functions;
5 import gnu.text.EnglishIntegerFormat;
6 import gnu.text.RomanIntegerFormat;
7 import gnu.math.*;
8
9 public class IntegerFormat extends gnu.text.IntegerFormat
10 {
11
12   public IntegerFormat ()
13   {
14   }
15
16   private static IntegerFormat plainDecimalFormat;
17
18   public static IntegerFormat getInstance()
19   {
20     if (plainDecimalFormat == null)
21       plainDecimalFormat = new IntegerFormat();
22     return plainDecimalFormat;
23   }
24
25   public static java.text.Format JavaDoc
26   getInstance (int base, int minWidth, int padChar,
27            int commaChar, int commaInterval, int flags)
28   {
29     if (base == PARAM_UNSPECIFIED)
30       {
31     if (padChar == PARAM_UNSPECIFIED
32         && padChar == PARAM_UNSPECIFIED
33         && commaChar == PARAM_UNSPECIFIED
34         && commaInterval == PARAM_UNSPECIFIED)
35       {
36             // Common Lisp ~R format:
37
boolean seenColon = (flags&SHOW_GROUPS) != 0;
38         if ((flags & SHOW_PLUS) != 0)
39           return RomanIntegerFormat.getInstance(seenColon);
40         else
41           return EnglishIntegerFormat.getInstance(seenColon);
42       }
43     base = 10;
44       }
45     if (minWidth == PARAM_UNSPECIFIED) minWidth = 1;
46     if (padChar == PARAM_UNSPECIFIED) padChar = ' ';
47     if (commaChar == PARAM_UNSPECIFIED) commaChar = ',';
48     if (commaInterval == PARAM_UNSPECIFIED) commaInterval = 3;
49     if (base == 10 && minWidth == 1 && padChar == ' '
50     && commaChar == ',' && commaInterval == 3
51     && flags == 0)
52       return getInstance();
53     IntegerFormat fmt = new IntegerFormat();
54     fmt.base = base;
55     fmt.minWidth = minWidth;
56     fmt.padChar = padChar;
57     fmt.commaChar = commaChar;
58     fmt.commaInterval = commaInterval;
59     fmt.flags = flags;
60     return fmt;
61   }
62
63   public String JavaDoc convertToIntegerString(Object JavaDoc arg, int radix)
64   {
65     if (arg instanceof RealNum)
66       return ((RealNum) arg).toExactInt(Numeric.ROUND).toString(radix);
67     else
68       return super.convertToIntegerString(arg, radix);
69   }
70 }
71
Popular Tags