001 package opendreams.proxy;
002
003
004 /**
005 * A classe <code>TStamp</code> representa um time stamp unico no
006 * sistema.
007 */
008 public class TStamp {
009 private final int NCHARS = 9; /* Numero de caractres da string de tempo */
010 /*
011 * String de conversao <=> Base64 Encoding Table
012 *
013 * Referencia: http://www.faqs.org/rfcs/rfc3548.html
014 *
015 * The "URL and Filename safe" Base 64 Alphabet
016 *
017 * Value Encoding Value Encoding Value Encoding Value Encoding
018 * 0 A 17 R 34 i 51 z
019 * 1 B 18 S 35 j 52 0
020 * 2 C 19 T 36 k 53 1
021 * 3 D 20 U 37 l 54 2
022 * 4 E 21 V 38 m 55 3
023 * 5 F 22 W 39 n 56 4
024 * 6 G 23 X 40 o 57 5
025 * 7 H 24 Y 41 p 58 6
026 * 8 I 25 Z 42 q 59 7
027 * 9 J 26 a 43 r 60 8
028 * 10 K 27 b 44 s 61 9
029 * 11 L 28 c 45 t 62 - (minus)
030 * 12 M 29 d 46 u 63 _ (understrike)
031 * 13 N 30 e 47 v
032 * 14 O 31 f 48 w
033 * 15 P 32 g 49 x
034 * 16 Q 33 h 50 y
035 */
036 private final String str_conv =
037 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
038
039 private static Long lastStamp = 0l;
040
041 private String uniq_id;
042 private long tstamp;
043
044 @Override
045 public String toString() {
046 return uniq_id;
047 }
048
049 public long toLong() {
050 return tstamp;
051 }
052
053 public TStamp() {
054 this.tstamp = System.currentTimeMillis(); // PASSAR PARA us (Java 1.5)
055
056 synchronized (lastStamp) {
057 if (tstamp <= lastStamp) {
058 tstamp = ++lastStamp;
059 }
060 }
061 this.uniq_id = "";
062 /*
063 * Cada caracter corresponde a 6 bits
064 * O primeiro caracter corresponde aos 6 bits mais significativos
065 */
066 long tempo_us = tstamp;
067 for (int i = NCHARS - 1; i >= 0; i--) {
068 /* 0x3F hex = 63 dec = 00111111 bin */
069 uniq_id = str_conv.charAt((int) (tempo_us & 0x3F)) + uniq_id;
070 tempo_us = tempo_us >> 6;
071 }
072 }
073
074 public TStamp(String uniq_id) {
075 /*
076 * Zera o numero que contem o timestamp em usec
077 */
078 this.uniq_id = uniq_id;
079 tstamp = 0;
080
081 /*
082 * Transforma da base64 p/ base10
083 */
084 for (int i = 0; i < NCHARS; i++) {
085 tstamp = tstamp << 6;
086 for (int j = 0; j <= 63; j++)
087 if (uniq_id.charAt(i) == str_conv.charAt(j)) {
088 tstamp = tstamp + j;
089 break;
090 }
091 }
092 }
093 }