1 package org.hibernate.id; 3 4 import java.io.Serializable ; 5 import java.util.Properties ; 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 23 24 public class UUIDHexGenerator extends AbstractUUIDGenerator implements Configurable { 25 26 private String sep = ""; 27 28 protected String format(int intval) { 29 String formatted = Integer.toHexString(intval); 30 StringBuffer buf = new StringBuffer ("00000000"); 31 buf.replace( 8-formatted.length(), 8, formatted ); 32 return buf.toString(); 33 } 34 35 protected String format(short shortval) { 36 String formatted = Integer.toHexString(shortval); 37 StringBuffer buf = new StringBuffer ("0000"); 38 buf.replace( 4-formatted.length(), 4, formatted ); 39 return buf.toString(); 40 } 41 42 public Serializable generate(SessionImplementor session, Object obj) { 43 return new StringBuffer (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 params, Dialect d) { 53 sep = PropertiesHelper.getString("separator", params, ""); 54 } 55 56 public static void main( String [] args ) throws Exception { 57 Properties props = new Properties (); 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 id = (String ) gen.generate(null, null); 66 System.out.println(id); 67 String id2 = (String ) gen2.generate(null, null); 68 System.out.println(id2); 69 } 70 71 } 72 73 } 74 | Popular Tags |