Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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: 8 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,20 @@
<version>${spring-jdbc.version}</version>
</dependency>

<!-- test -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.11.1</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
32 changes: 32 additions & 0 deletions core/src/main/java/org/sql2o/converters/BigIntegerConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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 (number instanceof BigInteger){
return (BigInteger)number;
}
else{
return BigInteger.valueOf(number.intValue());
Copy link

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 than number.intValue(). Otherwise you can lost precision.
Also, it's more native to use long than int when you want to get BigInteger

}

}

@Override
protected BigInteger convertStringValue(String string) {
return BigInteger.valueOf(Integer.parseInt(string));
}

@Override
protected String getTypeDescription() {
return BigInteger.class.toString();
}
}
2 changes: 2 additions & 0 deletions core/src/main/java/org/sql2o/converters/Convert.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;
Expand Down Expand Up @@ -54,6 +55,7 @@ private static void fillDefaults(Map<Class<?>, Converter<?>> mapToFill) {
mapToFill.put(byte.class, new ByteConverter(true));

mapToFill.put(BigDecimal.class, new BigDecimalConverter());
mapToFill.put(BigInteger.class,new BigIntegerConverter());

mapToFill.put(String.class, new StringConverter());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.sql2o.converters;

import org.assertj.core.api.Assertions;
import org.junit.Assert;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

import java.math.BigInteger;

public class BigIntegerConverterTest {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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).isEqualTo(1);
}

@Test
public void convertBigIntegerValue(){
BigInteger bigInteger = bigIntegerConverter.convertNumberValue(BigInteger.valueOf(1L));
assertThat(bigInteger).isEqualTo(1);
}

@Test
public void convertStringValue(){
BigInteger bigInteger = bigIntegerConverter.convertStringValue("1");
assertThat(bigInteger).isEqualTo(1);
}

@Test
public void getTypeDescription(){
assertThat(bigIntegerConverter.getTypeDescription()).isEqualTo("class java.math.BigInteger");
}
}