KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > id > UUIDHexGenerator


1 //$Id: UUIDHexGenerator.java,v 1.9 2005/02/12 07:19:22 steveebersole Exp $
2
package org.hibernate.id;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.Properties JavaDoc;
6
7 import org.hibernate.Hibernate;
8 import org.hibernate.dialect.Dialect;
9 import org.hibernate.engine.SessionImplementor;
10 import org.hibernate.type.Type;
11 import org.hibernate.util.PropertiesHelper;
12
13 /**
14  * <b>uuid</b><br>
15  * <br>
16  * A <tt>UUIDGenerator</tt> that returns a string of length 32,
17  * This string will consist of only hex digits. Optionally,
18  * the string may be generated with separators between each
19  * component of the UUID.
20  *
21  * @author Gavin King
22  */

23
24 public class UUIDHexGenerator extends AbstractUUIDGenerator implements Configurable {
25
26     private String JavaDoc sep = "";
27
28     protected String JavaDoc format(int intval) {
29         String JavaDoc formatted = Integer.toHexString(intval);
30         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("00000000");
31         buf.replace( 8-formatted.length(), 8, formatted );
32         return buf.toString();
33     }
34
35     protected String JavaDoc format(short shortval) {
36         String JavaDoc formatted = Integer.toHexString(shortval);
37         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("0000");
38         buf.replace( 4-formatted.length(), 4, formatted );
39         return buf.toString();
40     }
41
42     public Serializable JavaDoc generate(SessionImplementor session, Object JavaDoc obj) {
43         return new StringBuffer JavaDoc(36)
44             .append( format( getIP() ) ).append(sep)
45             .append( format( getJVM() ) ).append(sep)
46             .append( format( getHiTime() ) ).append(sep)
47             .append( format( getLoTime() ) ).append(sep)
48             .append( format( getCount() ) )
49             .toString();
50     }
51
52     public void configure(Type type, Properties JavaDoc params, Dialect d) {
53         sep = PropertiesHelper.getString("separator", params, "");
54     }
55
56     public static void main( String JavaDoc[] args ) throws Exception JavaDoc {
57         Properties JavaDoc props = new Properties JavaDoc();
58         props.setProperty("separator", "/");
59         IdentifierGenerator gen = new UUIDHexGenerator();
60         ( (Configurable) gen ).configure(Hibernate.STRING, props, null);
61         IdentifierGenerator gen2 = new UUIDHexGenerator();
62         ( (Configurable) gen2 ).configure(Hibernate.STRING, props, null);
63
64         for ( int i=0; i<10; i++) {
65             String JavaDoc id = (String JavaDoc) gen.generate(null, null);
66             System.out.println(id);
67             String JavaDoc id2 = (String JavaDoc) gen2.generate(null, null);
68             System.out.println(id2);
69         }
70
71     }
72
73 }
74
Popular Tags