@Test(expected = IllegalArgumentException.class) public void testStringToLongFailure() { assignment.stringToLong("12a"); }
public long stringToLong(final String s) { final int asciiDecimalStart = 48; final AtomicInteger index = new AtomicInteger(0); final AtomicLong number = new AtomicLong(0); final int len = s.length(); if (s == null || s.isEmpty()) { throw new IllegalArgumentException(s + " can't be converted to a long."); } s.chars().map(i -> (i - asciiDecimalStart)).forEach(i -> { if (i < 0 || i > 9) { throw new IllegalArgumentException(s + " can't be converted to a long."); } number.set((long) (number.get() + i * Math.pow(10, len - index.addAndGet(1)))); }); return number.get(); }
Assignment { public long stringToLong(final String s) { final int asciiDecimalStart = 48; final AtomicInteger index = new AtomicInteger(0); final AtomicLong number = new AtomicLong(0); final int len = s.length(); if (s == null || s.isEmpty()) { throw new IllegalArgumentException(s + " can't be converted to a long."); } s.chars().map(i -> (i - asciiDecimalStart)).forEach(i -> { if (i < 0 || i > 9) { throw new IllegalArgumentException(s + " can't be converted to a long."); } number.set((long) (number.get() + i * Math.pow(10, len - index.addAndGet(1)))); }); return number.get(); } }
Assignment { public long stringToLong(final String s) { final int asciiDecimalStart = 48; final AtomicInteger index = new AtomicInteger(0); final AtomicLong number = new AtomicLong(0); final int len = s.length(); if (s == null || s.isEmpty()) { throw new IllegalArgumentException(s + " can't be converted to a long."); } s.chars().map(i -> (i - asciiDecimalStart)).forEach(i -> { if (i < 0 || i > 9) { throw new IllegalArgumentException(s + " can't be converted to a long."); } number.set((long) (number.get() + i * Math.pow(10, len - index.addAndGet(1)))); }); return number.get(); }  }
Assignment { public long stringToLong(final String s) { final int asciiDecimalStart = 48; final AtomicInteger index = new AtomicInteger(0); final AtomicLong number = new AtomicLong(0); final int len = s.length(); if (s == null || s.isEmpty()) { throw new IllegalArgumentException(s + " can't be converted to a long."); } s.chars().map(i -> (i - asciiDecimalStart)).forEach(i -> { if (i < 0 || i > 9) { throw new IllegalArgumentException(s + " can't be converted to a long."); } number.set((long) (number.get() + i * Math.pow(10, len - index.addAndGet(1)))); }); return number.get(); }  long stringToLong(final String s); }
Assignment { public long stringToLong(final String s) { final int asciiDecimalStart = 48; final AtomicInteger index = new AtomicInteger(0); final AtomicLong number = new AtomicLong(0); final int len = s.length(); if (s == null || s.isEmpty()) { throw new IllegalArgumentException(s + " can't be converted to a long."); } s.chars().map(i -> (i - asciiDecimalStart)).forEach(i -> { if (i < 0 || i > 9) { throw new IllegalArgumentException(s + " can't be converted to a long."); } number.set((long) (number.get() + i * Math.pow(10, len - index.addAndGet(1)))); }); return number.get(); }  long stringToLong(final String s);  }