private static String canonicalize(String code, boolean caseSensitive)
    {
        if (code == null || code.length() == 0)
        {
            return null;
        }

        if (caseSensitive)
        {
            return code;
        }

        // A new instance is assigned to this variable
        // if modification is needed.
        StringBuilder sb = null;

        for (int i = 0; i < code.length(); ++i)
        {
            char ch = code.charAt(i);

            // The first letter.
            if (i == 0)
            {
                if (Character.isUpperCase(ch) == false)
                {
                    // Modification is needed.
                    sb = new StringBuilder();
                    sb.append(Character.toUpperCase(ch));
                }
            }
            // The second and subsequent letters.
            else
            {
                if (sb == null)
                {
                    if (Character.isLowerCase(ch) == false)
                    {
                        // Modification is needed.
                        sb = new StringBuilder();

                        // Copy all the previous letters so far.
                        sb.append(code.substring(0, i));

                        // Lower the current letter.
                        sb.append(Character.toLowerCase(ch));
                    }
                }
                else
                {
                    sb.append(Character.toLowerCase(ch));
                }
            }
        }

        if (sb == null)
        {
            return code;
        }
        else
        {
            return sb.toString();
        }
    }