-
Notifications
You must be signed in to change notification settings - Fork 230
add BigIntegerConverter #308
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package org.sql2o.converters; | ||
|
|
||
| import java.math.BigInteger; | ||
|
|
||
| public class BigIntegerConverter extends NumberConverter<BigInteger> { | ||
|
|
||
|
|
||
| public BigIntegerConverter() { | ||
| super(false); | ||
| } | ||
|
|
||
| @Override | ||
| protected BigInteger convertNumberValue(Number number) { | ||
| if(null == number){ | ||
| return null; | ||
| } | ||
| else if (number instanceof BigInteger){ | ||
| return (BigInteger)number; | ||
| } | ||
| else{ | ||
| return BigInteger.valueOf(number.intValue()); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| protected BigInteger convertStringValue(String string) { | ||
| if(null != string) { | ||
| return BigInteger.valueOf(Integer.parseInt(string)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
| }else{ | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected String getTypeDescription() { | ||
| return BigInteger.class.toString(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package org.sql2o.converters; | ||
|
|
||
|
|
||
| import org.junit.Test; | ||
|
|
||
|
|
||
| import java.math.BigInteger; | ||
|
|
||
| import static org.hamcrest.core.Is.is; | ||
| import static org.hamcrest.core.IsEqual.equalTo; | ||
| import static org.hamcrest.core.IsNull.nullValue; | ||
| import static org.junit.Assert.assertThat; | ||
|
|
||
| public class BigIntegerConverterTest { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you also add a test where you try to convert a null value? |
||
|
|
||
| private BigIntegerConverter bigIntegerConverter = new BigIntegerConverter(); | ||
|
|
||
| @Test | ||
| public void convertNumberValue(){ | ||
| BigInteger bigInteger = bigIntegerConverter.convertNumberValue(1); | ||
| assertThat(bigInteger,equalTo(BigInteger.ONE)); | ||
| } | ||
|
|
||
| @Test | ||
| public void convertBigIntegerValue(){ | ||
| BigInteger bigInteger = bigIntegerConverter.convertNumberValue(BigInteger.ONE); | ||
| assertThat(bigInteger,equalTo(BigInteger.ONE)); | ||
| } | ||
|
|
||
| @Test | ||
| public void convertStringValue(){ | ||
| BigInteger bigInteger = bigIntegerConverter.convertStringValue("1"); | ||
| assertThat(bigInteger,equalTo(BigInteger.ONE)); | ||
| } | ||
|
|
||
| @Test | ||
| public void convertNullValue(){ | ||
| BigInteger bigInteger; | ||
|
|
||
| bigInteger = bigIntegerConverter.convertNumberValue(null); | ||
| assertThat(bigInteger,is(nullValue())); | ||
|
|
||
| bigInteger = bigIntegerConverter.convertStringValue(null); | ||
| assertThat(bigInteger,is(nullValue())); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void getTypeDescription(){ | ||
| assertThat(bigIntegerConverter.getTypeDescription(),equalTo("class java.math.BigInteger")); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is seems that
number.longValue()would be better thannumber.intValue(). Otherwise you can lost precision.Also, it's more native to use long than int when you want to get BigInteger