1 28 29 30 package com.caucho.widget; 31 32 import com.caucho.util.CharBuffer; 33 import com.caucho.util.L10N; 34 35 import java.util.logging.Logger ; 36 37 38 public class WidgetUtil 39 { 40 private static L10N L = new L10N( WidgetUtil.class ); 41 42 static protected final Logger log = 43 Logger.getLogger( WidgetUtil.class.getName() ); 44 45 52 static public String calculateId( Object o, int len ) 53 { 54 if ( len > 24 ) 55 throw new IllegalArgumentException ("length exceeded"); 56 57 int len1 = len > 8 ? 8 : len; 58 59 CharBuffer cid = CharBuffer.allocate(); 60 61 int hashCode = o.hashCode(); 62 63 for ( int i = 0; i < len1; i++ ) { 64 int c = (hashCode >> (i * 4)) & 0x3f; 65 66 if (c < 26) 67 cid.append( (char) (c + 'A') ); 68 else if (c < 52) 69 cid.append( (char) (c + 'a' - 26) ); 70 else { 71 if ( i == 0 ) { 72 cid.append( '_' ); 73 } 74 else { 75 if (c < 62) 76 cid.append( (char) (c + '0' - 52) ); 77 else 78 cid.append( '_' ); 79 } 80 } 81 } 82 83 if ( len > len1 ) { 84 len1 = len - len1; 85 86 String toString = o.toString(); 87 long longCode = 0; 88 89 if ( toString == null ) 90 longCode = 26010331; 91 else 92 for ( int i = 0; i < toString.length(); i++ ) { 93 longCode = 331 * longCode + toString.charAt( i ); 94 } 95 96 for ( int i = 0; i < len1; i++ ) { 97 long c = (longCode >> (i * 4)) & 0x3f; 98 99 if (c < 26) 100 cid.append( (char) (c + 'A') ); 101 else if (c < 52) 102 cid.append( (char) (c + 'a' - 26) ); 103 else if (c < 62) 104 cid.append( (char) (c + '0' - 52) ); 105 else 106 cid.append( '_' ); 107 } 108 } 109 110 return cid.close(); 111 } 112 } 113 | Popular Tags |