KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com4j > GUID


1 package com4j;
2
3
4
5 /**
6  * Immutable representation of 128-bit COM GUID.
7  *
8  * @author Kohsuke Kawaguchi (kk@kohsuke.org)
9  */

10 public final class GUID {
11     // 2 long value to represent the bit image
12
final long[] v;
13
14     /**
15      * Used internally when GUID is created as a return value from
16      * a native method invocation.
17      */

18     GUID( long[] values ) {
19         this.v = values;
20     }
21
22     /**
23      * Parses the string representation "<tt>{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}</tt>".
24      */

25     public GUID( String JavaDoc str ) {
26         if(str.length()!=32+6)
27             throw new IllegalArgumentException JavaDoc("not a GUID: "+str);
28
29         v = new long[2];
30
31         // {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
32
// (1) (10) (15) (20) (25)
33
v[0] = parse(str,1,8)|(parse(str,10,4)<<32)|(parse(str,15,4)<<48);
34         v[1] = parse(str,20,2)|
35             (parse(str,22,2)<< 8)|
36             (parse(str,25,2)<<16)|
37             (parse(str,27,2)<<24)|
38             (parse(str,29,2)<<32)|
39             (parse(str,31,2)<<40)|
40             (parse(str,33,2)<<48)|
41             (parse(str,35,2)<<56);
42     }
43
44     private long parse( String JavaDoc s, int idx, int len ) {
45         return Long.parseLong(s.substring(idx,idx+len),16);
46     }
47
48     /**
49      * Returns true if two {@link GUID} objects have the same bit representation.
50      */

51     public boolean equals(Object JavaDoc o) {
52         if (this == o) return true;
53         if (!(o instanceof GUID)) return false;
54
55         final GUID guid = (GUID) o;
56
57         if (v[0] != guid.v[0]) return false;
58         if (v[1] != guid.v[1]) return false;
59
60         return true;
61     }
62
63     public int hashCode() {
64         int result;
65         result = (int) (v[0] ^ (v[0] >>> 32));
66         result = 29 * result + (int) (v[1] ^ (v[1] >>> 32));
67         return result;
68     }
69
70     /**
71      * Returns the GUID in the "<tt>{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}</tt>" format.
72      */

73     public String JavaDoc toString() {
74         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(38);
75         buf.append('{');
76         toHex(buf,(v[0]&0x00000000FFFFFFFFL) ,8);
77         buf.append('-');
78         toHex(buf,(v[0]&0x0000FFFF00000000L)>>32,4);
79         buf.append('-');
80         toHex(buf,(v[0]&0xFFFF000000000000L)>>48,4);
81         buf.append('-');
82         toHex(buf,(v[1]&0x00000000000000FFL) ,2);
83         toHex(buf,(v[1]&0x000000000000FF00L)>> 8,2);
84         buf.append('-');
85         toHex(buf,(v[1]&0x0000000000FF0000L)>>16,2);
86         toHex(buf,(v[1]&0x00000000FF000000L)>>24,2);
87         toHex(buf,(v[1]&0x000000FF00000000L)>>32,2);
88         toHex(buf,(v[1]&0x0000FF0000000000L)>>40,2);
89         toHex(buf,(v[1]&0x00FF000000000000L)>>48,2);
90         toHex(buf,(v[1]&0xFF00000000000000L)>>56,2);
91         buf.append('}');
92         return buf.toString();
93     }
94
95     private static final char[] digits = "0123456789ABCDEF".toCharArray();
96
97     private void toHex( StringBuffer JavaDoc buf, long n, int len ) {
98         for( int i=len-1; i>=0; i-- ) {
99             buf.append( digits[((int)(n>>(i*4)))&0xF] );
100         }
101     }
102 }
103
Popular Tags