Java ID Value Create toIdentifierString(long val)

Here you can find the source of toIdentifierString(long val)

Description

creates a shortest possible string representation of the given long number that qualifies as an identifier in common programming languages (and HTML-id's :-) That is, it must start with a letter.

License

Open Source License

Parameter

Parameter Description
val the long value to be encoded

Return

a string represantation of the given value that qualifies as an identifier.

Declaration

public static String toIdentifierString(long val) 

Method Source Code

//package com.java2s;
/*/*from ww w .j  a  va 2  s .  co m*/
 * $Id$
 * Copyright 2000,2005 wingS development team.
 *
 * This file is part of wingS (http://www.j-wings.org).
 *
 * wingS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * Please see COPYING for the complete licence.
 */

public class Main {
    private final static char[] ALPHAS = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
            'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', };
    /**
     * All possible digits for representing a number as a String
     * This is conservative and does not include 'special'
     * characters since some browsers don't handle them right.
     * The IE for instance seems to be case insensitive in class
     * names for CSSs. Grrr.
     */
    private final static char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
            'z',
            /* This %@&!-IE is case insensitive for certain
             * URLs and IDs
             * 'A' , 'B' ,
             * 'C' , 'D' , 'E' , 'F' , 'G' , 'H' ,
             * 'I' , 'J' , 'K' , 'L' , 'M' , 'N' ,
             * 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' ,
             * 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z'
             */
    };

    /**
     * creates a shortest possible string representation of the given
     * long number that qualifies as an identifier in common programming
     * languages (and HTML-id's :-)
     * That is, it must start with a letter.
     *
     * @param val the long value to be encoded
     * @return a string represantation of the given value that qualifies
     *         as an identifier.
     */
    public static String toIdentifierString(long val) {
        char buf[] = new char[14];
        int i = 0;
        if (val < 0) {
            buf[i++] = '_';
            val = -(val + 1);
        }
        buf[i++] = ALPHAS[(int) (val % ALPHAS.length)];
        val /= ALPHAS.length;
        while (val != 0 && i < buf.length) {
            buf[i++] = DIGITS[(int) (val % DIGITS.length)];
            val /= DIGITS.length;
        }
        return new String(buf, 0, i);
    }
}

Related

  1. toIdentifier(String input)
  2. toIdentifier(String name)
  3. toIdentifier(String name, StringBuffer fieldName)
  4. toIdentifier(String str)
  5. toIdentifier(String text)
  6. toIdentityEncodedString(byte[] data)
  7. toIdentityHashCodeInteger(Object value)
  8. toIdentityVariableName(String varName)
  9. toIdSet(boolean includeAlphaCharacters, String nonAlphaCharacters)