001: /*
002: * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Sam
027: */
028:
029: package com.caucho.widget;
030:
031: import com.caucho.util.CharBuffer;
032: import com.caucho.util.L10N;
033:
034: import java.util.logging.Logger;
035:
036: public class WidgetUtil {
037: private static L10N L = new L10N(WidgetUtil.class);
038:
039: static protected final Logger log = Logger
040: .getLogger(WidgetUtil.class.getName());
041:
042: /**
043: * Calculate an id based on the hashCode and possibly the value of toString()
044: * of an Object, the calculated id is suitable as a java identifier.
045: *
046: * @param len the number of characters in the returned string, greater than
047: * 8 and o.toString() is used, maximum of 24
048: */
049: static public String calculateId(Object o, int len) {
050: if (len > 24)
051: throw new IllegalArgumentException("length exceeded");
052:
053: int len1 = len > 8 ? 8 : len;
054:
055: CharBuffer cid = CharBuffer.allocate();
056:
057: int hashCode = o.hashCode();
058:
059: for (int i = 0; i < len1; i++) {
060: int c = (hashCode >> (i * 4)) & 0x3f;
061:
062: if (c < 26)
063: cid.append((char) (c + 'A'));
064: else if (c < 52)
065: cid.append((char) (c + 'a' - 26));
066: else {
067: if (i == 0) {
068: cid.append('_');
069: } else {
070: if (c < 62)
071: cid.append((char) (c + '0' - 52));
072: else
073: cid.append('_');
074: }
075: }
076: }
077:
078: if (len > len1) {
079: len1 = len - len1;
080:
081: String toString = o.toString();
082: long longCode = 0;
083:
084: if (toString == null)
085: longCode = 26010331;
086: else
087: for (int i = 0; i < toString.length(); i++) {
088: longCode = 331 * longCode + toString.charAt(i);
089: }
090:
091: for (int i = 0; i < len1; i++) {
092: long c = (longCode >> (i * 4)) & 0x3f;
093:
094: if (c < 26)
095: cid.append((char) (c + 'A'));
096: else if (c < 52)
097: cid.append((char) (c + 'a' - 26));
098: else if (c < 62)
099: cid.append((char) (c + '0' - 52));
100: else
101: cid.append('_');
102: }
103: }
104:
105: return cid.close();
106: }
107: }
|