1 package fr.dyade.aaa.agent; 2 3 public final class StringId { 4 private final static int BUFLEN = 35; 5 6 private static ThreadLocal perThreadBuffer = new ThreadLocal () { 8 protected synchronized Object initialValue() { 9 return new char[BUFLEN]; 10 } 11 }; 12 13 18 public final static String 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 (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 while (i >= 65536) { 68 q = i / 100; 69 r = i - ((q << 6) + (q << 5) + (q << 2)); 71 i = q; 72 buf [--charPos] = DigitOnes[r]; 73 buf [--charPos] = DigitTens[r]; 74 } 75 76 for (;;) { 79 q = (i * 52429) >>> (16+3); 80 r = i - ((q << 3) + (q << 1)); buf [--charPos] = DigitOnes[r]; 82 i = q; 83 if (i == 0) break; 84 } 85 return charPos; 86 } 87 } 88 | Popular Tags |