mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#21 Adding conversions for BigDecimal
This commit is contained in:
parent
2e6098eab5
commit
ae8e4f4b81
@ -0,0 +1,40 @@
|
||||
/**
|
||||
* Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.conversion;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
|
||||
/**
|
||||
* Conversion between {@link BigDecimal} and {@link BigInteger}.
|
||||
*
|
||||
* @author Gunnar Morling
|
||||
*/
|
||||
public class BigDecimalToBigIntegerConversion extends SimpleConversion {
|
||||
|
||||
@Override
|
||||
public String getToConversionString(String sourceReference, Context conversionContext) {
|
||||
return sourceReference + ".toBigInteger()";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFromConversionString(String targetReference, Context conversionContext) {
|
||||
return "new BigDecimal( " + targetReference + " )";
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
/**
|
||||
* Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.conversion;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
/**
|
||||
* Conversion between {@link BigInteger} and native number types.
|
||||
*
|
||||
* @author Gunnar Morling
|
||||
*/
|
||||
public class BigDecimalToPrimitiveConversion extends SimpleConversion {
|
||||
|
||||
private final Class<?> targetType;
|
||||
|
||||
public BigDecimalToPrimitiveConversion(Class<?> targetType) {
|
||||
if ( !targetType.isPrimitive() ) {
|
||||
throw new IllegalArgumentException( targetType + " is no primitive type." );
|
||||
}
|
||||
|
||||
this.targetType = targetType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getToConversionString(String sourceReference, Context conversionContext) {
|
||||
return sourceReference + "." + targetType.getName() + "Value()";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFromConversionString(String targetReference, Context conversionContext) {
|
||||
StringBuilder conversion = new StringBuilder( "BigDecimal.valueOf( " );
|
||||
conversion.append( targetReference ).append( " )" );
|
||||
|
||||
return conversion.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.conversion;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* Conversion between {@link BigDecimal} and {@link String}.
|
||||
*
|
||||
* @author Gunnar Morling
|
||||
*/
|
||||
public class BigDecimalToStringConversion extends SimpleConversion {
|
||||
|
||||
@Override
|
||||
public String getToConversionString(String sourceReference, Context conversionContext) {
|
||||
return sourceReference + ".toString()";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFromConversionString(String targetReference, Context conversionContext) {
|
||||
return "new BigDecimal( " + targetReference + " )";
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.conversion;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import org.mapstruct.ap.util.NativeTypes;
|
||||
|
||||
/**
|
||||
* Conversion between {@link BigDecimal} and wrappers of native number types.
|
||||
*
|
||||
* @author Gunnar Morling
|
||||
*/
|
||||
public class BigDecimalToWrapperConversion extends SimpleConversion {
|
||||
|
||||
private final Class<?> targetType;
|
||||
|
||||
public BigDecimalToWrapperConversion(Class<?> targetType) {
|
||||
if ( targetType.isPrimitive() ) {
|
||||
throw new IllegalArgumentException( targetType + " is a primitive type." );
|
||||
}
|
||||
|
||||
this.targetType = NativeTypes.getPrimitiveType( targetType );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getToConversionString(String sourceReference, Context conversionContext) {
|
||||
return sourceReference + "." + targetType.getName() + "Value()";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFromConversionString(String targetReference, Context conversionContext) {
|
||||
StringBuilder conversion = new StringBuilder( "BigDecimal.valueOf( " );
|
||||
conversion.append( targetReference );
|
||||
conversion.append( " )" );
|
||||
|
||||
return conversion.toString();
|
||||
}
|
||||
}
|
@ -18,6 +18,7 @@
|
||||
*/
|
||||
package org.mapstruct.ap.conversion;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
@ -142,6 +143,20 @@ public class Conversions {
|
||||
registerBigIntegerConversion( double.class );
|
||||
registerBigIntegerConversion( Double.class );
|
||||
|
||||
//BigDecimal <> native types
|
||||
registerBigDecimalConversion( byte.class );
|
||||
registerBigDecimalConversion( Byte.class );
|
||||
registerBigDecimalConversion( short.class );
|
||||
registerBigDecimalConversion( Short.class );
|
||||
registerBigDecimalConversion( int.class );
|
||||
registerBigDecimalConversion( Integer.class );
|
||||
registerBigDecimalConversion( long.class );
|
||||
registerBigDecimalConversion( Long.class );
|
||||
registerBigDecimalConversion( float.class );
|
||||
registerBigDecimalConversion( Float.class );
|
||||
registerBigDecimalConversion( double.class );
|
||||
registerBigDecimalConversion( Double.class );
|
||||
|
||||
//native types <> String
|
||||
registerToStringConversion( byte.class );
|
||||
registerToStringConversion( Byte.class );
|
||||
@ -160,10 +175,12 @@ public class Conversions {
|
||||
register( char.class, String.class, new CharToStringConversion() );
|
||||
register( Character.class, String.class, new CharWrapperToStringConversion() );
|
||||
register( BigInteger.class, String.class, new BigIntegerToStringConversion() );
|
||||
register( BigDecimal.class, String.class, new BigDecimalToStringConversion() );
|
||||
|
||||
//misc.
|
||||
register( Enum.class, String.class, new EnumStringConversion() );
|
||||
register( Date.class, String.class, new DateToStringConversion() );
|
||||
register( BigDecimal.class, BigInteger.class, new BigDecimalToBigIntegerConversion() );
|
||||
}
|
||||
|
||||
private void registerNativeTypeConversion(Class<?> sourceType, Class<?> targetType) {
|
||||
@ -199,6 +216,15 @@ public class Conversions {
|
||||
}
|
||||
}
|
||||
|
||||
private void registerBigDecimalConversion(Class<?> targetType) {
|
||||
if ( targetType.isPrimitive() ) {
|
||||
register( BigDecimal.class, targetType, new BigDecimalToPrimitiveConversion( targetType ) );
|
||||
}
|
||||
else {
|
||||
register( BigDecimal.class, targetType, new BigDecimalToWrapperConversion( targetType ) );
|
||||
}
|
||||
}
|
||||
|
||||
private void register(Class<?> sourceType, Class<?> targetType, ConversionProvider conversion) {
|
||||
conversions.put( forClasses( sourceType, targetType ), conversion );
|
||||
conversions.put( forClasses( targetType, sourceType ), reverse( conversion ) );
|
||||
|
@ -22,11 +22,11 @@ import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper
|
||||
public interface SourceTargetMapper {
|
||||
public interface BigDecimalMapper {
|
||||
|
||||
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
|
||||
BigDecimalMapper INSTANCE = Mappers.getMapper( BigDecimalMapper.class );
|
||||
|
||||
Target sourceToTarget(Source source);
|
||||
BigDecimalTarget sourceToTarget(BigDecimalSource source);
|
||||
|
||||
Source targetToSource(Target target);
|
||||
BigDecimalSource targetToSource(BigDecimalTarget target);
|
||||
}
|
@ -0,0 +1,151 @@
|
||||
/**
|
||||
* Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.conversion.bignumbers;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class BigDecimalSource {
|
||||
|
||||
private BigDecimal b;
|
||||
private BigDecimal bb;
|
||||
private BigDecimal s;
|
||||
private BigDecimal ss;
|
||||
private BigDecimal i;
|
||||
private BigDecimal ii;
|
||||
private BigDecimal l;
|
||||
private BigDecimal ll;
|
||||
private BigDecimal f;
|
||||
private BigDecimal ff;
|
||||
private BigDecimal d;
|
||||
private BigDecimal dd;
|
||||
private BigDecimal string;
|
||||
private BigDecimal bigInteger;
|
||||
|
||||
public BigDecimal getB() {
|
||||
return b;
|
||||
}
|
||||
|
||||
public void setB(BigDecimal b) {
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
public BigDecimal getBb() {
|
||||
return bb;
|
||||
}
|
||||
|
||||
public void setBb(BigDecimal bb) {
|
||||
this.bb = bb;
|
||||
}
|
||||
|
||||
public BigDecimal getS() {
|
||||
return s;
|
||||
}
|
||||
|
||||
public void setS(BigDecimal s) {
|
||||
this.s = s;
|
||||
}
|
||||
|
||||
public BigDecimal getSs() {
|
||||
return ss;
|
||||
}
|
||||
|
||||
public void setSs(BigDecimal ss) {
|
||||
this.ss = ss;
|
||||
}
|
||||
|
||||
public BigDecimal getI() {
|
||||
return i;
|
||||
}
|
||||
|
||||
public void setI(BigDecimal i) {
|
||||
this.i = i;
|
||||
}
|
||||
|
||||
public BigDecimal getIi() {
|
||||
return ii;
|
||||
}
|
||||
|
||||
public void setIi(BigDecimal ii) {
|
||||
this.ii = ii;
|
||||
}
|
||||
|
||||
public BigDecimal getL() {
|
||||
return l;
|
||||
}
|
||||
|
||||
public void setL(BigDecimal l) {
|
||||
this.l = l;
|
||||
}
|
||||
|
||||
public BigDecimal getLl() {
|
||||
return ll;
|
||||
}
|
||||
|
||||
public void setLl(BigDecimal ll) {
|
||||
this.ll = ll;
|
||||
}
|
||||
|
||||
public BigDecimal getF() {
|
||||
return f;
|
||||
}
|
||||
|
||||
public void setF(BigDecimal f) {
|
||||
this.f = f;
|
||||
}
|
||||
|
||||
public BigDecimal getFf() {
|
||||
return ff;
|
||||
}
|
||||
|
||||
public void setFf(BigDecimal ff) {
|
||||
this.ff = ff;
|
||||
}
|
||||
|
||||
public BigDecimal getD() {
|
||||
return d;
|
||||
}
|
||||
|
||||
public void setD(BigDecimal d) {
|
||||
this.d = d;
|
||||
}
|
||||
|
||||
public BigDecimal getDd() {
|
||||
return dd;
|
||||
}
|
||||
|
||||
public void setDd(BigDecimal dd) {
|
||||
this.dd = dd;
|
||||
}
|
||||
|
||||
public BigDecimal getString() {
|
||||
return string;
|
||||
}
|
||||
|
||||
public void setString(BigDecimal string) {
|
||||
this.string = string;
|
||||
}
|
||||
|
||||
public BigDecimal getBigInteger() {
|
||||
return bigInteger;
|
||||
}
|
||||
|
||||
public void setBigInteger(BigDecimal bigInteger) {
|
||||
this.bigInteger = bigInteger;
|
||||
}
|
||||
}
|
@ -0,0 +1,151 @@
|
||||
/**
|
||||
* Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.conversion.bignumbers;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class BigDecimalTarget {
|
||||
|
||||
private byte b;
|
||||
private Byte bb;
|
||||
private short s;
|
||||
private Short ss;
|
||||
private int i;
|
||||
private Integer ii;
|
||||
private long l;
|
||||
private Long ll;
|
||||
private float f;
|
||||
private Float ff;
|
||||
private double d;
|
||||
private Double dd;
|
||||
private String string;
|
||||
private BigInteger bigInteger;
|
||||
|
||||
public byte getB() {
|
||||
return b;
|
||||
}
|
||||
|
||||
public void setB(byte b) {
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
public Byte getBb() {
|
||||
return bb;
|
||||
}
|
||||
|
||||
public void setBb(Byte bb) {
|
||||
this.bb = bb;
|
||||
}
|
||||
|
||||
public short getS() {
|
||||
return s;
|
||||
}
|
||||
|
||||
public void setS(short s) {
|
||||
this.s = s;
|
||||
}
|
||||
|
||||
public Short getSs() {
|
||||
return ss;
|
||||
}
|
||||
|
||||
public void setSs(Short ss) {
|
||||
this.ss = ss;
|
||||
}
|
||||
|
||||
public int getI() {
|
||||
return i;
|
||||
}
|
||||
|
||||
public void setI(int i) {
|
||||
this.i = i;
|
||||
}
|
||||
|
||||
public Integer getIi() {
|
||||
return ii;
|
||||
}
|
||||
|
||||
public void setIi(Integer ii) {
|
||||
this.ii = ii;
|
||||
}
|
||||
|
||||
public long getL() {
|
||||
return l;
|
||||
}
|
||||
|
||||
public void setL(long l) {
|
||||
this.l = l;
|
||||
}
|
||||
|
||||
public Long getLl() {
|
||||
return ll;
|
||||
}
|
||||
|
||||
public void setLl(Long ll) {
|
||||
this.ll = ll;
|
||||
}
|
||||
|
||||
public float getF() {
|
||||
return f;
|
||||
}
|
||||
|
||||
public void setF(float f) {
|
||||
this.f = f;
|
||||
}
|
||||
|
||||
public Float getFf() {
|
||||
return ff;
|
||||
}
|
||||
|
||||
public void setFf(Float ff) {
|
||||
this.ff = ff;
|
||||
}
|
||||
|
||||
public double getD() {
|
||||
return d;
|
||||
}
|
||||
|
||||
public void setD(double d) {
|
||||
this.d = d;
|
||||
}
|
||||
|
||||
public Double getDd() {
|
||||
return dd;
|
||||
}
|
||||
|
||||
public void setDd(Double dd) {
|
||||
this.dd = dd;
|
||||
}
|
||||
|
||||
public String getString() {
|
||||
return string;
|
||||
}
|
||||
|
||||
public void setString(String string) {
|
||||
this.string = string;
|
||||
}
|
||||
|
||||
public BigInteger getBigInteger() {
|
||||
return bigInteger;
|
||||
}
|
||||
|
||||
public void setBigInteger(BigInteger bigInteger) {
|
||||
this.bigInteger = bigInteger;
|
||||
}
|
||||
}
|
@ -1,73 +0,0 @@
|
||||
/**
|
||||
* Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.conversion.bignumbers;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
import org.mapstruct.ap.testutil.IssueKey;
|
||||
import org.mapstruct.ap.testutil.MapperTestBase;
|
||||
import org.mapstruct.ap.testutil.WithClasses;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests conversions between {@link BigInteger} and numbers as well as String.
|
||||
*
|
||||
* @author Gunnar Morling
|
||||
*/
|
||||
@WithClasses({ Source.class, Target.class, SourceTargetMapper.class })
|
||||
public class BigIntegerConversionTest extends MapperTestBase {
|
||||
|
||||
@Test
|
||||
@IssueKey("21")
|
||||
public void shouldApplyConversions() {
|
||||
Source source = new Source();
|
||||
source.setB( new BigInteger( "1" ) );
|
||||
source.setBb( new BigInteger( "2" ) );
|
||||
source.setS( new BigInteger( "3" ) );
|
||||
source.setSs( new BigInteger( "4" ) );
|
||||
source.setI( new BigInteger( "5" ) );
|
||||
source.setIi( new BigInteger( "6" ) );
|
||||
source.setL( new BigInteger( "7" ) );
|
||||
source.setLl( new BigInteger( "8" ) );
|
||||
source.setF( new BigInteger( "9" ) );
|
||||
source.setFf( new BigInteger( "10" ) );
|
||||
source.setD( new BigInteger( "11" ) );
|
||||
source.setDd( new BigInteger( "12" ) );
|
||||
source.setString( new BigInteger( "13" ) );
|
||||
|
||||
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
|
||||
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getB() ).isEqualTo( (byte) 1 );
|
||||
assertThat( target.getBb() ).isEqualTo( (byte) 2 );
|
||||
assertThat( target.getS() ).isEqualTo( (short) 3 );
|
||||
assertThat( target.getSs() ).isEqualTo( (short) 4 );
|
||||
assertThat( target.getI() ).isEqualTo( 5 );
|
||||
assertThat( target.getIi() ).isEqualTo( 6 );
|
||||
assertThat( target.getL() ).isEqualTo( 7 );
|
||||
assertThat( target.getLl() ).isEqualTo( 8 );
|
||||
assertThat( target.getF() ).isEqualTo( 9.0f );
|
||||
assertThat( target.getFf() ).isEqualTo( 10.0f );
|
||||
assertThat( target.getD() ).isEqualTo( 11.0d );
|
||||
assertThat( target.getDd() ).isEqualTo( 12.0d );
|
||||
assertThat( target.getString() ).isEqualTo( "13" );
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.conversion.bignumbers;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper
|
||||
public interface BigIntegerMapper {
|
||||
|
||||
BigIntegerMapper INSTANCE = Mappers.getMapper( BigIntegerMapper.class );
|
||||
|
||||
BigIntegerTarget sourceToTarget(BigIntegerSource source);
|
||||
|
||||
BigIntegerSource targetToSource(BigIntegerTarget target);
|
||||
}
|
@ -20,7 +20,7 @@ package org.mapstruct.ap.test.conversion.bignumbers;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class Source {
|
||||
public class BigIntegerSource {
|
||||
|
||||
private BigInteger b;
|
||||
private BigInteger bb;
|
@ -18,7 +18,7 @@
|
||||
*/
|
||||
package org.mapstruct.ap.test.conversion.bignumbers;
|
||||
|
||||
public class Target {
|
||||
public class BigIntegerTarget {
|
||||
|
||||
private byte b;
|
||||
private Byte bb;
|
@ -0,0 +1,189 @@
|
||||
/**
|
||||
* Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.conversion.bignumbers;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
|
||||
import org.mapstruct.ap.testutil.IssueKey;
|
||||
import org.mapstruct.ap.testutil.MapperTestBase;
|
||||
import org.mapstruct.ap.testutil.WithClasses;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests conversions between {@link BigInteger} and numbers as well as String.
|
||||
*
|
||||
* @author Gunnar Morling
|
||||
*/
|
||||
public class BigNumbersConversionTest extends MapperTestBase {
|
||||
|
||||
@Test
|
||||
@IssueKey("21")
|
||||
@WithClasses({ BigIntegerSource.class, BigIntegerTarget.class, BigIntegerMapper.class })
|
||||
public void shouldApplyBigIntegerConversions() {
|
||||
BigIntegerSource source = new BigIntegerSource();
|
||||
source.setB( new BigInteger( "1" ) );
|
||||
source.setBb( new BigInteger( "2" ) );
|
||||
source.setS( new BigInteger( "3" ) );
|
||||
source.setSs( new BigInteger( "4" ) );
|
||||
source.setI( new BigInteger( "5" ) );
|
||||
source.setIi( new BigInteger( "6" ) );
|
||||
source.setL( new BigInteger( "7" ) );
|
||||
source.setLl( new BigInteger( "8" ) );
|
||||
source.setF( new BigInteger( "9" ) );
|
||||
source.setFf( new BigInteger( "10" ) );
|
||||
source.setD( new BigInteger( "11" ) );
|
||||
source.setDd( new BigInteger( "12" ) );
|
||||
source.setString( new BigInteger( "13" ) );
|
||||
|
||||
BigIntegerTarget target = BigIntegerMapper.INSTANCE.sourceToTarget( source );
|
||||
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getB() ).isEqualTo( (byte) 1 );
|
||||
assertThat( target.getBb() ).isEqualTo( (byte) 2 );
|
||||
assertThat( target.getS() ).isEqualTo( (short) 3 );
|
||||
assertThat( target.getSs() ).isEqualTo( (short) 4 );
|
||||
assertThat( target.getI() ).isEqualTo( 5 );
|
||||
assertThat( target.getIi() ).isEqualTo( 6 );
|
||||
assertThat( target.getL() ).isEqualTo( 7 );
|
||||
assertThat( target.getLl() ).isEqualTo( 8 );
|
||||
assertThat( target.getF() ).isEqualTo( 9.0f );
|
||||
assertThat( target.getFf() ).isEqualTo( 10.0f );
|
||||
assertThat( target.getD() ).isEqualTo( 11.0d );
|
||||
assertThat( target.getDd() ).isEqualTo( 12.0d );
|
||||
assertThat( target.getString() ).isEqualTo( "13" );
|
||||
}
|
||||
|
||||
@Test
|
||||
@IssueKey("21")
|
||||
@WithClasses({ BigIntegerSource.class, BigIntegerTarget.class, BigIntegerMapper.class })
|
||||
public void shouldApplyReverseBigIntegerConversions() {
|
||||
BigIntegerTarget target = new BigIntegerTarget();
|
||||
target.setB( (byte) 1 );
|
||||
target.setBb( (byte) 2 );
|
||||
target.setS( (short) 3 );
|
||||
target.setSs( (short) 4 );
|
||||
target.setI( 5 );
|
||||
target.setIi( 6 );
|
||||
target.setL( 7 );
|
||||
target.setLl( 8L );
|
||||
target.setF( 9.0f );
|
||||
target.setFf( 10.0f );
|
||||
target.setD( 11.0d );
|
||||
target.setDd( 12.0d );
|
||||
target.setString( "13" );
|
||||
|
||||
BigIntegerSource source = BigIntegerMapper.INSTANCE.targetToSource( target );
|
||||
|
||||
assertThat( source ).isNotNull();
|
||||
assertThat( source.getB() ).isEqualTo( new BigInteger( "1" ) );
|
||||
assertThat( source.getBb() ).isEqualTo( new BigInteger( "2" ) );
|
||||
assertThat( source.getS() ).isEqualTo( new BigInteger( "3" ) );
|
||||
assertThat( source.getSs() ).isEqualTo( new BigInteger( "4" ) );
|
||||
assertThat( source.getI() ).isEqualTo( new BigInteger( "5" ) );
|
||||
assertThat( source.getIi() ).isEqualTo( new BigInteger( "6" ) );
|
||||
assertThat( source.getL() ).isEqualTo( new BigInteger( "7" ) );
|
||||
assertThat( source.getLl() ).isEqualTo( new BigInteger( "8" ) );
|
||||
assertThat( source.getF() ).isEqualTo( new BigInteger( "9" ) );
|
||||
assertThat( source.getFf() ).isEqualTo( new BigInteger( "10" ) );
|
||||
assertThat( source.getD() ).isEqualTo( new BigInteger( "11" ) );
|
||||
assertThat( source.getDd() ).isEqualTo( new BigInteger( "12" ) );
|
||||
assertThat( source.getString() ).isEqualTo( new BigInteger( "13" ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
@IssueKey("21")
|
||||
@WithClasses({ BigDecimalSource.class, BigDecimalTarget.class, BigDecimalMapper.class })
|
||||
public void shouldApplyBigDecimalConversions() {
|
||||
BigDecimalSource source = new BigDecimalSource();
|
||||
source.setB( new BigDecimal( "1.45" ) );
|
||||
source.setBb( new BigDecimal( "2.45" ) );
|
||||
source.setS( new BigDecimal( "3.45" ) );
|
||||
source.setSs( new BigDecimal( "4.45" ) );
|
||||
source.setI( new BigDecimal( "5.45" ) );
|
||||
source.setIi( new BigDecimal( "6.45" ) );
|
||||
source.setL( new BigDecimal( "7.45" ) );
|
||||
source.setLl( new BigDecimal( "8.45" ) );
|
||||
source.setF( new BigDecimal( "9.45" ) );
|
||||
source.setFf( new BigDecimal( "10.45" ) );
|
||||
source.setD( new BigDecimal( "11.45" ) );
|
||||
source.setDd( new BigDecimal( "12.45" ) );
|
||||
source.setString( new BigDecimal( "13.45" ) );
|
||||
source.setBigInteger( new BigDecimal( "14.45" ) );
|
||||
|
||||
BigDecimalTarget target = BigDecimalMapper.INSTANCE.sourceToTarget( source );
|
||||
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getB() ).isEqualTo( (byte) 1 );
|
||||
assertThat( target.getBb() ).isEqualTo( (byte) 2 );
|
||||
assertThat( target.getS() ).isEqualTo( (short) 3 );
|
||||
assertThat( target.getSs() ).isEqualTo( (short) 4 );
|
||||
assertThat( target.getI() ).isEqualTo( 5 );
|
||||
assertThat( target.getIi() ).isEqualTo( 6 );
|
||||
assertThat( target.getL() ).isEqualTo( 7 );
|
||||
assertThat( target.getLl() ).isEqualTo( 8 );
|
||||
assertThat( target.getF() ).isEqualTo( 9.45f );
|
||||
assertThat( target.getFf() ).isEqualTo( 10.45f );
|
||||
assertThat( target.getD() ).isEqualTo( 11.45d );
|
||||
assertThat( target.getDd() ).isEqualTo( 12.45d );
|
||||
assertThat( target.getString() ).isEqualTo( "13.45" );
|
||||
assertThat( target.getBigInteger() ).isEqualTo( new BigInteger( "14" ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
@IssueKey("21")
|
||||
@WithClasses({ BigDecimalSource.class, BigDecimalTarget.class, BigDecimalMapper.class })
|
||||
public void shouldApplyReverseBigDecimalConversions() {
|
||||
BigDecimalTarget target = new BigDecimalTarget();
|
||||
target.setB( (byte) 1 );
|
||||
target.setBb( (byte) 2 );
|
||||
target.setS( (short) 3 );
|
||||
target.setSs( (short) 4 );
|
||||
target.setI( 5 );
|
||||
target.setIi( 6 );
|
||||
target.setL( 7 );
|
||||
target.setLl( 8L );
|
||||
target.setF( 9.0f );
|
||||
target.setFf( 10.0f );
|
||||
target.setD( 11.0d );
|
||||
target.setDd( 12.0d );
|
||||
target.setString( "13.45" );
|
||||
target.setBigInteger( new BigInteger( "14" ) );
|
||||
|
||||
BigDecimalSource source = BigDecimalMapper.INSTANCE.targetToSource( target );
|
||||
|
||||
assertThat( source ).isNotNull();
|
||||
assertThat( source.getB() ).isEqualTo( new BigDecimal( "1" ) );
|
||||
assertThat( source.getBb() ).isEqualTo( new BigDecimal( "2" ) );
|
||||
assertThat( source.getS() ).isEqualTo( new BigDecimal( "3" ) );
|
||||
assertThat( source.getSs() ).isEqualTo( new BigDecimal( "4" ) );
|
||||
assertThat( source.getI() ).isEqualTo( new BigDecimal( "5" ) );
|
||||
assertThat( source.getIi() ).isEqualTo( new BigDecimal( "6" ) );
|
||||
assertThat( source.getL() ).isEqualTo( new BigDecimal( "7" ) );
|
||||
assertThat( source.getLl() ).isEqualTo( new BigDecimal( "8" ) );
|
||||
assertThat( source.getF() ).isEqualTo( new BigDecimal( "9.0" ) );
|
||||
assertThat( source.getFf() ).isEqualTo( new BigDecimal( "10.0" ) );
|
||||
assertThat( source.getD() ).isEqualTo( new BigDecimal( "11.0" ) );
|
||||
assertThat( source.getDd() ).isEqualTo( new BigDecimal( "12.0" ) );
|
||||
assertThat( source.getString() ).isEqualTo( new BigDecimal( "13.45" ) );
|
||||
assertThat( source.getBigInteger() ).isEqualTo( new BigDecimal( "14" ) );
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user