Parses the specified string and returns a Long instance if the string can be decoded into a long value. The string may be an optional minus sign "-" followed by a hexadecimal ("0x..." or "#..."), octal ("0..."), or decimal ("...") representation of a long. @param string a string
(String string)
| 124 | * if {@code string} can not be parsed as a long value. |
| 125 | */ |
| 126 | public static Long decode(String string) throws NumberFormatException { |
| 127 | int length = string.length(), i = 0; |
| 128 | if (length == 0) { |
| 129 | throw new NumberFormatException(); |
| 130 | } |
| 131 | char firstDigit = string.charAt(i); |
| 132 | boolean negative = firstDigit == '-'; |
| 133 | if (negative) { |
| 134 | if (length == 1) { |
| 135 | throw new NumberFormatException(string); |
| 136 | } |
| 137 | firstDigit = string.charAt(++i); |
| 138 | } |
| 139 | |
| 140 | int base = 10; |
| 141 | if (firstDigit == '0') { |
| 142 | if (++i == length) { |
| 143 | return valueOf(0L); |
| 144 | } |
| 145 | if ((firstDigit = string.charAt(i)) == 'x' || firstDigit == 'X') { |
| 146 | if (i == length) { |
| 147 | throw new NumberFormatException(string); |
| 148 | } |
| 149 | i++; |
| 150 | base = 16; |
| 151 | } else { |
| 152 | base = 8; |
| 153 | } |
| 154 | } else if (firstDigit == '#') { |
| 155 | if (i == length) { |
| 156 | throw new NumberFormatException(string); |
| 157 | } |
| 158 | i++; |
| 159 | base = 16; |
| 160 | } |
| 161 | |
| 162 | long result = parse(string, i, base, negative); |
| 163 | return valueOf(result); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public double doubleValue() { |