1/* 2 * Copyright (C) 2011 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 11 * express or implied. See the License for the specific language governing permissions and 12 * limitations under the License. 13 */ 14 15package com.google.common.primitives; 16 17import com.google.common.annotations.GwtCompatible; 18 19/** 20 * A string to be parsed as a number and the radix to interpret it in. 21 */ 22@GwtCompatible 23final class ParseRequest { 24 final String rawValue; 25 final int radix; 26 27 private ParseRequest(String rawValue, int radix) { 28 this.rawValue = rawValue; 29 this.radix = radix; 30 } 31 32 static ParseRequest fromString(String stringValue) { 33 if (stringValue.length() == 0) { 34 throw new NumberFormatException("empty string"); 35 } 36 37 // Handle radix specifier if present 38 String rawValue; 39 int radix; 40 char firstChar = stringValue.charAt(0); 41 if (stringValue.startsWith("0x") || stringValue.startsWith("0X")) { 42 rawValue = stringValue.substring(2); 43 radix = 16; 44 } else if (firstChar == '#') { 45 rawValue = stringValue.substring(1); 46 radix = 16; 47 } else if (firstChar == '0' && stringValue.length() > 1) { 48 rawValue = stringValue.substring(1); 49 radix = 8; 50 } else { 51 rawValue = stringValue; 52 radix = 10; 53 } 54 55 return new ParseRequest(rawValue, radix); 56 } 57} 58