From 63b90353a800fcef99c21b74e5e63bb72f13d434 Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Sat, 6 Jul 2013 11:34:19 +0200 Subject: [PATCH] #26 Adding conversions for BigInteger --- .../BigIntegerToPrimitiveConversion.java | 59 ++++++++ .../BigIntegerToStringConversion.java | 41 +++++ .../BigIntegerToWrapperConversion.java | 63 ++++++++ .../mapstruct/ap/conversion/Conversions.java | 28 ++++ .../bignumbers/BigIntegerConversionTest.java | 73 +++++++++ .../ap/test/conversion/bignumbers/Source.java | 142 ++++++++++++++++++ .../bignumbers/SourceTargetMapper.java | 32 ++++ .../ap/test/conversion/bignumbers/Target.java | 140 +++++++++++++++++ 8 files changed, 578 insertions(+) create mode 100644 processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToPrimitiveConversion.java create mode 100644 processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToStringConversion.java create mode 100644 processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToWrapperConversion.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/conversion/bignumbers/BigIntegerConversionTest.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/conversion/bignumbers/Source.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/conversion/bignumbers/SourceTargetMapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/conversion/bignumbers/Target.java diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToPrimitiveConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToPrimitiveConversion.java new file mode 100644 index 000000000..94fcc34a1 --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToPrimitiveConversion.java @@ -0,0 +1,59 @@ +/** + * 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; + +import org.mapstruct.ap.model.Type; + +/** + * Conversion between {@link BigInteger} and native number types. + * + * @author Gunnar Morling + */ +public class BigIntegerToPrimitiveConversion implements Conversion { + + private final Class targetType; + + public BigIntegerToPrimitiveConversion(Class targetType) { + if ( !targetType.isPrimitive() ) { + throw new IllegalArgumentException( targetType + " is no primitive type." ); + } + + this.targetType = targetType; + } + + @Override + public String to(String sourcePropertyAccessor, Type type) { + return sourcePropertyAccessor + "." + targetType.getName() + "Value()"; + } + + @Override + public String from(String targetPropertyAccessor, Type type) { + StringBuilder conversion = new StringBuilder( "BigInteger.valueOf( " ); + + if ( targetType == float.class || targetType == double.class ) { + conversion.append( "(long) " ); + } + + conversion.append( targetPropertyAccessor ).append( " )" ); + + return conversion.toString(); + } +} diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToStringConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToStringConversion.java new file mode 100644 index 000000000..18c9b37e0 --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToStringConversion.java @@ -0,0 +1,41 @@ +/** + * 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; + +import org.mapstruct.ap.model.Type; + +/** + * Conversion between {@link BigInteger} and {@link String}. + * + * @author Gunnar Morling + */ +public class BigIntegerToStringConversion implements Conversion { + + @Override + public String to(String sourcePropertyAccessor, Type type) { + return sourcePropertyAccessor + ".toString()"; + } + + @Override + public String from(String targetPropertyAccessor, Type type) { + return "new BigInteger( " + targetPropertyAccessor + " )"; + } +} diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToWrapperConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToWrapperConversion.java new file mode 100644 index 000000000..f94f06ccb --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToWrapperConversion.java @@ -0,0 +1,63 @@ +/** + * 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; + +import org.mapstruct.ap.model.Type; +import org.mapstruct.ap.util.NativeTypes; + +/** + * Conversion between {@link BigInteger} and wrappers of native number types. + * + * @author Gunnar Morling + */ +public class BigIntegerToWrapperConversion implements Conversion { + + private final Class targetType; + + public BigIntegerToWrapperConversion(Class targetType) { + if ( targetType.isPrimitive() ) { + throw new IllegalArgumentException( targetType + " is a primitive type." ); + } + + this.targetType = NativeTypes.getPrimitiveType( targetType ); + } + + @Override + public String to(String sourcePropertyAccessor, Type type) { + return sourcePropertyAccessor + "." + targetType.getName() + "Value()"; + } + + @Override + public String from(String targetPropertyAccessor, Type type) { + StringBuilder conversion = new StringBuilder( "BigInteger.valueOf( " ); + + conversion.append( targetPropertyAccessor ); + + if ( targetType == float.class || targetType == double.class ) { + conversion.append( ".longValue()" ); + } + + conversion.append( " )" ); + + + return conversion.toString(); + } +} diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/Conversions.java b/processor/src/main/java/org/mapstruct/ap/conversion/Conversions.java index f5262255f..e1f8eba24 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/Conversions.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/Conversions.java @@ -18,6 +18,7 @@ */ package org.mapstruct.ap.conversion; +import java.math.BigInteger; import java.util.HashMap; import java.util.Map; import javax.lang.model.type.DeclaredType; @@ -47,6 +48,7 @@ public class Conversions { this.enumType = typeUtils.getDeclaredType( elementUtils.getTypeElement( Enum.class.getCanonicalName() ) ); this.stringType = typeUtils.getDeclaredType( elementUtils.getTypeElement( String.class.getCanonicalName() ) ); + //native types <> native types, including wrappers registerNativeTypeConversion( byte.class, Byte.class ); registerNativeTypeConversion( byte.class, short.class ); registerNativeTypeConversion( byte.class, Short.class ); @@ -127,6 +129,21 @@ public class Conversions { registerNativeTypeConversion( boolean.class, Boolean.class ); registerNativeTypeConversion( char.class, Character.class ); + //BigInteger <> native types + registerBigIntegerConversion( byte.class ); + registerBigIntegerConversion( Byte.class ); + registerBigIntegerConversion( short.class ); + registerBigIntegerConversion( Short.class ); + registerBigIntegerConversion( int.class ); + registerBigIntegerConversion( Integer.class ); + registerBigIntegerConversion( long.class ); + registerBigIntegerConversion( Long.class ); + registerBigIntegerConversion( float.class ); + registerBigIntegerConversion( Float.class ); + registerBigIntegerConversion( double.class ); + registerBigIntegerConversion( Double.class ); + + //native types <> String registerToStringConversion( byte.class ); registerToStringConversion( Byte.class ); registerToStringConversion( short.class ); @@ -143,7 +160,9 @@ public class Conversions { registerToStringConversion( Boolean.class ); register( char.class, String.class, new CharToStringConversion() ); register( Character.class, String.class, new CharWrapperToStringConversion() ); + register( BigInteger.class, String.class, new BigIntegerToStringConversion() ); + //misc. register( Enum.class, String.class, new EnumStringConversion() ); } @@ -171,6 +190,15 @@ public class Conversions { } } + private void registerBigIntegerConversion(Class targetType) { + if ( targetType.isPrimitive() ) { + register( BigInteger.class, targetType, new BigIntegerToPrimitiveConversion( targetType ) ); + } + else { + register( BigInteger.class, targetType, new BigIntegerToWrapperConversion( targetType ) ); + } + } + private void register(Class sourceType, Class targetType, Conversion conversion) { conversions.put( Key.forClasses( sourceType, targetType ), conversion ); conversions.put( Key.forClasses( targetType, sourceType ), reverse( conversion ) ); diff --git a/processor/src/test/java/org/mapstruct/ap/test/conversion/bignumbers/BigIntegerConversionTest.java b/processor/src/test/java/org/mapstruct/ap/test/conversion/bignumbers/BigIntegerConversionTest.java new file mode 100644 index 000000000..d798554f2 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/conversion/bignumbers/BigIntegerConversionTest.java @@ -0,0 +1,73 @@ +/** + * 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" ); + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/conversion/bignumbers/Source.java b/processor/src/test/java/org/mapstruct/ap/test/conversion/bignumbers/Source.java new file mode 100644 index 000000000..41908b806 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/conversion/bignumbers/Source.java @@ -0,0 +1,142 @@ +/** + * 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 Source { + + private BigInteger b; + private BigInteger bb; + private BigInteger s; + private BigInteger ss; + private BigInteger i; + private BigInteger ii; + private BigInteger l; + private BigInteger ll; + private BigInteger f; + private BigInteger ff; + private BigInteger d; + private BigInteger dd; + private BigInteger string; + + public BigInteger getB() { + return b; + } + + public void setB(BigInteger b) { + this.b = b; + } + + public BigInteger getBb() { + return bb; + } + + public void setBb(BigInteger bb) { + this.bb = bb; + } + + public BigInteger getS() { + return s; + } + + public void setS(BigInteger s) { + this.s = s; + } + + public BigInteger getSs() { + return ss; + } + + public void setSs(BigInteger ss) { + this.ss = ss; + } + + public BigInteger getI() { + return i; + } + + public void setI(BigInteger i) { + this.i = i; + } + + public BigInteger getIi() { + return ii; + } + + public void setIi(BigInteger ii) { + this.ii = ii; + } + + public BigInteger getL() { + return l; + } + + public void setL(BigInteger l) { + this.l = l; + } + + public BigInteger getLl() { + return ll; + } + + public void setLl(BigInteger ll) { + this.ll = ll; + } + + public BigInteger getF() { + return f; + } + + public void setF(BigInteger f) { + this.f = f; + } + + public BigInteger getFf() { + return ff; + } + + public void setFf(BigInteger ff) { + this.ff = ff; + } + + public BigInteger getD() { + return d; + } + + public void setD(BigInteger d) { + this.d = d; + } + + public BigInteger getDd() { + return dd; + } + + public void setDd(BigInteger dd) { + this.dd = dd; + } + + public BigInteger getString() { + return string; + } + + public void setString(BigInteger string) { + this.string = string; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/conversion/bignumbers/SourceTargetMapper.java b/processor/src/test/java/org/mapstruct/ap/test/conversion/bignumbers/SourceTargetMapper.java new file mode 100644 index 000000000..f19ec9512 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/conversion/bignumbers/SourceTargetMapper.java @@ -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.Mappers; + +@Mapper +public interface SourceTargetMapper { + + SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class ); + + Target sourceToTarget(Source source); + + Source targetToSource(Target target); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/conversion/bignumbers/Target.java b/processor/src/test/java/org/mapstruct/ap/test/conversion/bignumbers/Target.java new file mode 100644 index 000000000..30342fed8 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/conversion/bignumbers/Target.java @@ -0,0 +1,140 @@ +/** + * 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; + +public class Target { + + 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; + + 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; + } +}