@Test public void testDecode() { String[][] data = { { "0", "true" }, { "-0", "true" }, { "+0", "true" }, { "00", "true" }, { "-00", "true" }, { "+00", "true" }, { "-10", "true" }, { "10", "true" }, { "+10", "true" }, { "-010", "true", "-10" }, { "010", "true", "10" }, { "+010", "true", "+10" }, { "-80", "true" }, { "80", "true" }, { "+80", "true" }, { "-080", "true", "-80" }, { "080", "true", "80" }, { "+080", "true", "+80" }, { "-0789", "true", "-789" }, { "0789", "true", "789" }, { "+0789", "true", "+789" }, { "-0x01", "true" }, { "0x01", "true" }, { "+0x01", "true" }, { "-0x00", "true" }, { "0x00", "true" }, { "+0x00", "true" }, { "-0x100", "true" }, { "0x100", "true" }, { "+0x100", "true" }, { "-#01", "false" }, { "#01", "false" }, { "+#01", "false" }, { "-#00", "false" }, { "#00", "false" }, { "+#00", "false" }, { "-#100", "false" }, { "#100", "false" }, { "+#100", "false" }, }; int count = 0; for (String[] record : data) { count++; String numberStr = record[0]; boolean expected = Boolean.parseBoolean(record[1]); boolean valid = true; try { Long longValue = PELongUtils.decode(numberStr); String decodeStr = numberStr; if (record.length > 2) { decodeStr = record[2]; } try { Long expectedValue = Long.decode(decodeStr); assertEquals("(Rec: " + count + ") Value '" + numberStr + "' should decode correctly", expectedValue, longValue); } catch (NumberFormatException e) { fail("Possible test error - Long.decode cannot parse '" + decodeStr + "'"); } } catch (NumberFormatException e) { valid = false; } assertEquals("(Rec: " + count + ") Value '" + numberStr + "' should have the right parsability", expected, valid); } }
public static Long decode(String str) throws NumberFormatException { int radix = 10; int index = 0; boolean negative = false; Long result; if (str.length() == 0) throw new NumberFormatException("Zero length string"); char firstChar = str.charAt(0); if (firstChar == '-') { negative = true; index++; } else if (firstChar == '+') index++; if (str.startsWith("0x", index) || str.startsWith("0X", index)) { index += 2; radix = 16; } if (str.startsWith("-", index) || str.startsWith("+", index)) throw new NumberFormatException("Sign character in wrong position"); try { result = Long.valueOf(str.substring(index), radix); result = negative ? Long.valueOf(-result.longValue()) : result; } catch (NumberFormatException e) { String constant = negative ? ("-" + str.substring(index)) : str.substring(index); result = Long.valueOf(constant, radix); } return result; }
PELongUtils { public static Long decode(String str) throws NumberFormatException { int radix = 10; int index = 0; boolean negative = false; Long result; if (str.length() == 0) throw new NumberFormatException("Zero length string"); char firstChar = str.charAt(0); if (firstChar == '-') { negative = true; index++; } else if (firstChar == '+') index++; if (str.startsWith("0x", index) || str.startsWith("0X", index)) { index += 2; radix = 16; } if (str.startsWith("-", index) || str.startsWith("+", index)) throw new NumberFormatException("Sign character in wrong position"); try { result = Long.valueOf(str.substring(index), radix); result = negative ? Long.valueOf(-result.longValue()) : result; } catch (NumberFormatException e) { String constant = negative ? ("-" + str.substring(index)) : str.substring(index); result = Long.valueOf(constant, radix); } return result; } }
PELongUtils { public static Long decode(String str) throws NumberFormatException { int radix = 10; int index = 0; boolean negative = false; Long result; if (str.length() == 0) throw new NumberFormatException("Zero length string"); char firstChar = str.charAt(0); if (firstChar == '-') { negative = true; index++; } else if (firstChar == '+') index++; if (str.startsWith("0x", index) || str.startsWith("0X", index)) { index += 2; radix = 16; } if (str.startsWith("-", index) || str.startsWith("+", index)) throw new NumberFormatException("Sign character in wrong position"); try { result = Long.valueOf(str.substring(index), radix); result = negative ? Long.valueOf(-result.longValue()) : result; } catch (NumberFormatException e) { String constant = negative ? ("-" + str.substring(index)) : str.substring(index); result = Long.valueOf(constant, radix); } return result; } private  PELongUtils(); }
PELongUtils { public static Long decode(String str) throws NumberFormatException { int radix = 10; int index = 0; boolean negative = false; Long result; if (str.length() == 0) throw new NumberFormatException("Zero length string"); char firstChar = str.charAt(0); if (firstChar == '-') { negative = true; index++; } else if (firstChar == '+') index++; if (str.startsWith("0x", index) || str.startsWith("0X", index)) { index += 2; radix = 16; } if (str.startsWith("-", index) || str.startsWith("+", index)) throw new NumberFormatException("Sign character in wrong position"); try { result = Long.valueOf(str.substring(index), radix); result = negative ? Long.valueOf(-result.longValue()) : result; } catch (NumberFormatException e) { String constant = negative ? ("-" + str.substring(index)) : str.substring(index); result = Long.valueOf(constant, radix); } return result; } private  PELongUtils(); static Long decode(String str); }
PELongUtils { public static Long decode(String str) throws NumberFormatException { int radix = 10; int index = 0; boolean negative = false; Long result; if (str.length() == 0) throw new NumberFormatException("Zero length string"); char firstChar = str.charAt(0); if (firstChar == '-') { negative = true; index++; } else if (firstChar == '+') index++; if (str.startsWith("0x", index) || str.startsWith("0X", index)) { index += 2; radix = 16; } if (str.startsWith("-", index) || str.startsWith("+", index)) throw new NumberFormatException("Sign character in wrong position"); try { result = Long.valueOf(str.substring(index), radix); result = negative ? Long.valueOf(-result.longValue()) : result; } catch (NumberFormatException e) { String constant = negative ? ("-" + str.substring(index)) : str.substring(index); result = Long.valueOf(constant, radix); } return result; } private  PELongUtils(); static Long decode(String str);  }