KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > widget > WidgetUtil


1 /*
2  * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Sam
27  */

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 JavaDoc;
36
37
38 public class WidgetUtil
39 {
40   private static L10N L = new L10N( WidgetUtil.class );
41
42   static protected final Logger JavaDoc log =
43     Logger.getLogger( WidgetUtil.class.getName() );
44
45   /**
46    * Calculate an id based on the hashCode and possibly the value of toString()
47    * of an Object, the calculated id is suitable as a java identifier.
48    *
49    * @param len the number of characters in the returned string, greater than
50    * 8 and o.toString() is used, maximum of 24
51    */

52   static public String JavaDoc calculateId( Object JavaDoc o, int len )
53   {
54     if ( len > 24 )
55       throw new IllegalArgumentException JavaDoc("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 JavaDoc 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