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