KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > util > GUID


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.util;
18
19 import org.doomdark.uuid.UUIDGenerator;
20
21 /**
22  * A wrapper class to serve up GUIDs
23  *
24  * @author kevinr
25  */

26 public final class GUID
27 {
28    /**
29     * Private Constructor for GUID.
30     */

31    private GUID()
32    {
33    }
34
35 // protected static final char[] s_values =
36
// {
37
// '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
38
// 'f'
39
// };
40

41    /**
42     * Generates and returns a new GUID as a string
43     *
44     * @return String GUID
45     */

46    public static String JavaDoc generate()
47    {
48       return UUIDGenerator.getInstance().generateTimeBasedUUID().toString();
49    }
50
51 // == Not sure if we need this functionality again (derekh) ==
52
//
53
// /**
54
// * Convert a string with a guid inside into a byte[16] array
55
// *
56
// * @param str - the guid
57
// * @return - byte[16] containing the GUID
58
// * @throws InvalidGuidFormatException
59
// */
60
// public static byte[] parseFromString(String str) throws InvalidGuidFormatException
61
// {
62
// byte[] data = new byte[16];
63
// int dataPos = 0;
64
//
65
// byte bVal;
66
// int value = 0;
67
// int pos = 0;
68
//
69
// for(int i = 0; i < str.length(); i++)
70
// {
71
// char thisChar = str.charAt(i);
72
//
73
// int idx = 0;
74
//
75
// if(thisChar >= '0' && thisChar <= '9')
76
// {
77
// idx = thisChar - '0';
78
// pos++;
79
// }
80
// else if(thisChar >= 'a' && thisChar <= 'f')
81
// {
82
// idx = thisChar - 'a' + 10;
83
// pos++;
84
// }
85
// else if(thisChar >= 'a' && thisChar <= 'f')
86
// {
87
// idx = thisChar - 'A' + 10;
88
// pos++;
89
// }
90
// else if(thisChar == '-' || thisChar == '{' || thisChar == '}')
91
// {
92
// // Doesn't matter
93
// }
94
// else
95
// {
96
// throw new InvalidGuidFormatException();
97
// }
98
//
99
// try
100
// {
101
// if(pos == 1)
102
// value = idx;
103
// else if(pos == 2)
104
// {
105
// value = (value * 16) + idx;
106
//
107
// byte b = (byte) value;
108
// data[dataPos++] = b;
109
//
110
// pos = 0;
111
// }
112
// }
113
// catch(RuntimeException e)
114
// {
115
// // May occur if we go off the end of the data index
116
// throw new InvalidGuidFormatException();
117
// }
118
// }
119
//
120
// return data;
121
// }
122
//
123
// /**
124
// * Convert a byte[16] containing a guid to a string representation
125
// *
126
// * @param data - the data
127
// * @return - the string
128
// */
129
// public static String convertToString(byte[] data)
130
// {
131
// char[] output = new char[36];
132
// int cPos = 0;
133
//
134
// for(int i = 0; i < 16; i++)
135
// {
136
// int v = data[i];
137
//
138
// int lowVal = v & 0x000F;
139
// int hiVal = (v & 0x00F0) >> 4;
140
//
141
// output[cPos++] = s_values[hiVal];
142
// output[cPos++] = s_values[lowVal];
143
//
144
// if(cPos == 8 || cPos == 13 || cPos == 18 || cPos == 23)
145
// output[cPos++] = '-';
146
// }
147
//
148
// return new String(output);
149
// }
150
}
151
Popular Tags