Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsciiEncoding::GetDigit to throw a AsciiNumberFormatException. #171

Merged
merged 2 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions agrona/src/main/java/org/agrona/AsciiEncoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ public static int endOffset(final long value)
* @param index within the string the value is encoded.
* @param value of the encoding in ASCII.
* @return the digit value of the encoded ASCII.
* @throws NumberFormatException if the value is not a digit.
* @throws AsciiNumberFormatException if the value is not a digit.
*/
public static int getDigit(final int index, final byte value)
{
if (value < 0x30 || value > 0x39)
{
throw new NumberFormatException("'" + ((char)value) + "' is not a valid digit @ " + index);
throw new AsciiNumberFormatException("'" + ((char)value) + "' is not a valid digit @ " + index);
}

return value - 0x30;
Expand All @@ -98,13 +98,13 @@ public static int getDigit(final int index, final byte value)
* @param index within the string the value is encoded.
* @param value of the encoding in ASCII.
* @return the digit value of the encoded ASCII.
* @throws NumberFormatException if the value is not a digit.
* @throws AsciiNumberFormatException if the value is not a digit.
*/
public static int getDigit(final int index, final char value)
{
if (value < 0x30 || value > 0x39)
{
throw new NumberFormatException("'" + value + "' is not a valid digit @ " + index);
throw new AsciiNumberFormatException("'" + value + "' is not a valid digit @ " + index);
}

return value - 0x30;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.agrona;

public class AsciiNumberFormatException extends NumberFormatException
{
public AsciiNumberFormatException(final String message)
{
super(message);
}
}
14 changes: 13 additions & 1 deletion agrona/src/test/java/org/agrona/AsciiEncodingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.junit.Test;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;

public class AsciiEncodingTest
{
Expand All @@ -41,4 +41,16 @@ public void shouldParseLong()
assertThat(AsciiEncoding.parseLongAscii("-7", 0, 2), is(-7L));
assertThat(AsciiEncoding.parseLongAscii("3333", 1, 2), is(33L));
}

@Test(expected = AsciiNumberFormatException.class)
public void shouldThrowExceptionWhenDecodingCharNonAsciiValue()
{
AsciiEncoding.getDigit(0, 'a');
}

@Test(expected = AsciiNumberFormatException.class)
public void shouldThrowExceptionWhenDecodingByteNonAsciiValue()
{
AsciiEncoding.getDigit(0, (byte)'a');
}
}