KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > agent > StringId


1 package fr.dyade.aaa.agent;
2
3 public final class StringId {
4   private final static int BUFLEN = 35;
5
6   // Per-thread buffer for string/stringbuffer conversion
7
private static ThreadLocal JavaDoc perThreadBuffer = new ThreadLocal JavaDoc() {
8     protected synchronized Object JavaDoc initialValue() {
9       return new char[BUFLEN];
10     }
11   };
12
13   /**
14    * Returns a string representation of id: label + x + sep + y + sep + z.
15    * At least x must be provided and higher or equal to 0, if y or z are less
16    * than 0 they are ignored.
17    */

18   public final static String JavaDoc toStringId(char label, char sep,
19                                         int x, int y, int z) {
20     int idx = BUFLEN;
21     char[] buf = (char[]) (perThreadBuffer.get());
22     if (z >= 0) {
23       idx = getChars(z, buf, idx);
24       buf[--idx] = sep;
25     }
26     if (y >= 0) {
27       idx = getChars(y, buf, idx);
28       buf[--idx] = sep;
29     }
30     idx = getChars(x, buf, idx);
31     buf[--idx] = label;
32
33     return new String JavaDoc(buf, idx, BUFLEN - idx);
34   }
35
36   final static char [] DigitTens = {
37     '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
38     '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
39     '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
40     '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
41     '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
42     '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
43     '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
44     '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
45     '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
46     '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
47   } ;
48
49   final static char [] DigitOnes = {
50     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
51     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
52     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
53     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
54     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
55     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
56     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
57     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
58     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
59     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
60   } ;
61
62   private final static int getChars(int i, char[] buf, int idx) {
63     int q, r;
64     int charPos = idx;
65
66     // Generate two digits per iteration
67
while (i >= 65536) {
68       q = i / 100;
69       // really: r = i - (q * 100);
70
r = i - ((q << 6) + (q << 5) + (q << 2));
71       i = q;
72       buf [--charPos] = DigitOnes[r];
73       buf [--charPos] = DigitTens[r];
74     }
75
76     // Fall thru to fast mode for smaller numbers
77
// assert(i <= 65536, i);
78
for (;;) {
79       q = (i * 52429) >>> (16+3);
80       r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...
81
buf [--charPos] = DigitOnes[r];
82       i = q;
83       if (i == 0) break;
84     }
85     return charPos;
86   }
87 }
88
Popular Tags