diff --git a/flow-data/src/main/java/com/vaadin/flow/data/converter/BigDecimalToDoubleConverter.java b/flow-data/src/main/java/com/vaadin/flow/data/converter/BigDecimalToDoubleConverter.java new file mode 100644 index 00000000000..e555617b70e --- /dev/null +++ b/flow-data/src/main/java/com/vaadin/flow/data/converter/BigDecimalToDoubleConverter.java @@ -0,0 +1,51 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * 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 com.vaadin.flow.data.converter; + +import java.math.BigDecimal; + +import com.vaadin.flow.data.binder.Result; +import com.vaadin.flow.data.binder.ValueContext; + +/** + * A converter that converts from {@link BigDecimal} to {@link Double} and back. + * + * @since 24.5 + */ +public class BigDecimalToDoubleConverter + implements Converter { + + @Override + public Result convertToModel(BigDecimal value, + ValueContext context) { + if (value == null) { + return Result.ok(null); + } + + return Result.ok(value.doubleValue()); + } + + @Override + public BigDecimal convertToPresentation(Double value, + ValueContext context) { + if (value == null) { + return null; + } + + return BigDecimal.valueOf(value); + } +} diff --git a/flow-data/src/main/java/com/vaadin/flow/data/converter/BigDecimalToFloatConverter.java b/flow-data/src/main/java/com/vaadin/flow/data/converter/BigDecimalToFloatConverter.java new file mode 100644 index 00000000000..f56563d1b6d --- /dev/null +++ b/flow-data/src/main/java/com/vaadin/flow/data/converter/BigDecimalToFloatConverter.java @@ -0,0 +1,46 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * 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 com.vaadin.flow.data.converter; + +import com.vaadin.flow.data.binder.Result; +import com.vaadin.flow.data.binder.ValueContext; + +import java.math.BigDecimal; + +/** + * A converter that converts from {@link BigDecimal} to {@link Float} and back. + */ +public class BigDecimalToFloatConverter + implements Converter { + + @Override + public Result convertToModel(BigDecimal value, + ValueContext context) { + if (value == null) { + return Result.ok(null); + } + return Result.ok(value.floatValue()); + } + + @Override + public BigDecimal convertToPresentation(Float value, ValueContext context) { + if (value == null) { + return null; + } + return BigDecimal.valueOf(value); + } +} diff --git a/flow-data/src/main/java/com/vaadin/flow/data/converter/BigDecimalToIntegerConverter.java b/flow-data/src/main/java/com/vaadin/flow/data/converter/BigDecimalToIntegerConverter.java new file mode 100644 index 00000000000..c378ef6bf97 --- /dev/null +++ b/flow-data/src/main/java/com/vaadin/flow/data/converter/BigDecimalToIntegerConverter.java @@ -0,0 +1,52 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * 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 com.vaadin.flow.data.converter; + +import java.math.BigDecimal; + +import com.vaadin.flow.data.binder.Result; +import com.vaadin.flow.data.binder.ValueContext; + +/** + * A converter that converts from {@link BigDecimal} to {@link Integer} and + * back. + * + * @since 24.5 + */ +public class BigDecimalToIntegerConverter + implements Converter { + + @Override + public Result convertToModel(BigDecimal value, + ValueContext context) { + if (value == null) { + return Result.ok(null); + } + + return Result.ok(value.intValue()); + } + + @Override + public BigDecimal convertToPresentation(Integer value, + ValueContext context) { + if (value == null) { + return null; + } + + return BigDecimal.valueOf(value); + } +} diff --git a/flow-data/src/main/java/com/vaadin/flow/data/converter/BigDecimalToLongConverter.java b/flow-data/src/main/java/com/vaadin/flow/data/converter/BigDecimalToLongConverter.java new file mode 100644 index 00000000000..0900d8835f2 --- /dev/null +++ b/flow-data/src/main/java/com/vaadin/flow/data/converter/BigDecimalToLongConverter.java @@ -0,0 +1,44 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * 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 com.vaadin.flow.data.converter; + +import com.vaadin.flow.data.binder.Result; +import com.vaadin.flow.data.binder.ValueContext; + +import java.math.BigDecimal; + +/** + * A converter that converts from {@link BigDecimal} to {@link Long} and back. + */ +public class BigDecimalToLongConverter implements Converter { + + @Override + public Result convertToModel(BigDecimal value, ValueContext context) { + if (value == null) { + return Result.ok(null); + } + return Result.ok(value.longValue()); + } + + @Override + public BigDecimal convertToPresentation(Long value, ValueContext context) { + if (value == null) { + return null; + } + return BigDecimal.valueOf(value); + } +} diff --git a/flow-data/src/main/java/com/vaadin/flow/data/converter/DefaultConverterFactory.java b/flow-data/src/main/java/com/vaadin/flow/data/converter/DefaultConverterFactory.java index 10c2c6b04ba..eb0142efb02 100644 --- a/flow-data/src/main/java/com/vaadin/flow/data/converter/DefaultConverterFactory.java +++ b/flow-data/src/main/java/com/vaadin/flow/data/converter/DefaultConverterFactory.java @@ -72,6 +72,28 @@ public enum DefaultConverterFactory implements ConverterFactory { StringToLongConverter::new); registerConverterWithMessageProvider(StringToUuidConverter.class, StringToUuidConverter::new); + registerConverter(BigDecimalToDoubleConverter.class, + BigDecimalToDoubleConverter::new); + registerConverter(BigDecimalToIntegerConverter.class, + BigDecimalToIntegerConverter::new); + registerConverter(DoubleToBigDecimalConverter.class, + DoubleToBigDecimalConverter::new); + registerConverter(IntegerToBigDecimalConverter.class, + IntegerToBigDecimalConverter::new); + registerConverter(IntegerToDoubleConverter.class, + IntegerToDoubleConverter::new); + registerConverter(IntegerToLongConverter.class, + IntegerToLongConverter::new); + registerConverter(BigDecimalToFloatConverter.class, + BigDecimalToFloatConverter::new); + registerConverter(BigDecimalToLongConverter.class, + BigDecimalToLongConverter::new); + registerConverter(LongToBigDecimalConverter.class, + LongToBigDecimalConverter::new); + registerConverter(FloatToBigDecimalConverter.class, + FloatToBigDecimalConverter::new); + registerConverter(FloatToDoubleConverter.class, + FloatToDoubleConverter::new); } private > void registerConverter( @@ -112,7 +134,8 @@ private static final class Key implements Serializable { private Key(Class presentationType, Class modelType) { assert presentationType != null && modelType != null; - this.presentationType = presentationType; + this.presentationType = ReflectTools + .convertPrimitiveType(presentationType); this.modelType = ReflectTools.convertPrimitiveType(modelType); } diff --git a/flow-data/src/main/java/com/vaadin/flow/data/converter/DoubleToBigDecimalConverter.java b/flow-data/src/main/java/com/vaadin/flow/data/converter/DoubleToBigDecimalConverter.java new file mode 100644 index 00000000000..4e878dcee3e --- /dev/null +++ b/flow-data/src/main/java/com/vaadin/flow/data/converter/DoubleToBigDecimalConverter.java @@ -0,0 +1,51 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * 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 com.vaadin.flow.data.converter; + +import java.math.BigDecimal; + +import com.vaadin.flow.data.binder.Result; +import com.vaadin.flow.data.binder.ValueContext; + +/** + * A converter that converts from {@link Double} to {@link BigDecimal} and back. + * + * @since 24.5 + */ +public class DoubleToBigDecimalConverter + implements Converter { + + @Override + public Result convertToModel(Double value, + ValueContext context) { + if (value == null) { + return Result.ok(null); + } + + return Result.ok(BigDecimal.valueOf(value)); + } + + @Override + public Double convertToPresentation(BigDecimal value, + ValueContext context) { + if (value == null) { + return null; + } + + return value.doubleValue(); + } +} diff --git a/flow-data/src/main/java/com/vaadin/flow/data/converter/FloatToBigDecimalConverter.java b/flow-data/src/main/java/com/vaadin/flow/data/converter/FloatToBigDecimalConverter.java new file mode 100644 index 00000000000..13a13a457aa --- /dev/null +++ b/flow-data/src/main/java/com/vaadin/flow/data/converter/FloatToBigDecimalConverter.java @@ -0,0 +1,46 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * 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 com.vaadin.flow.data.converter; + +import com.vaadin.flow.data.binder.Result; +import com.vaadin.flow.data.binder.ValueContext; + +import java.math.BigDecimal; + +/** + * A converter that converts from {@link Float} to {@link BigDecimal} and back. + */ +public class FloatToBigDecimalConverter + implements Converter { + + @Override + public Result convertToModel(Float value, + ValueContext context) { + if (value == null) { + return Result.ok(null); + } + return Result.ok(BigDecimal.valueOf(value)); + } + + @Override + public Float convertToPresentation(BigDecimal value, ValueContext context) { + if (value == null) { + return null; + } + return value.floatValue(); + } +} diff --git a/flow-data/src/main/java/com/vaadin/flow/data/converter/FloatToDoubleConverter.java b/flow-data/src/main/java/com/vaadin/flow/data/converter/FloatToDoubleConverter.java new file mode 100644 index 00000000000..89d1c8cefab --- /dev/null +++ b/flow-data/src/main/java/com/vaadin/flow/data/converter/FloatToDoubleConverter.java @@ -0,0 +1,47 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * 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 com.vaadin.flow.data.converter; + +import com.vaadin.flow.data.binder.Result; +import com.vaadin.flow.data.binder.ValueContext; + +import java.math.BigDecimal; + +/** + * A converter that converts from {@link Float} to {@link Double} and + * back. + * + * @since 24.5 + */ +public class FloatToDoubleConverter implements Converter { + + @Override + public Result convertToModel(Float value, ValueContext context) { + if (value == null) { + return Result.ok(null); + } + return Result.ok(value.doubleValue()); + } + + @Override + public Float convertToPresentation(Double value, ValueContext context) { + if (value == null) { + return null; + } + return value.floatValue(); + } +} + diff --git a/flow-data/src/main/java/com/vaadin/flow/data/converter/IntegerToBigDecimalConverter.java b/flow-data/src/main/java/com/vaadin/flow/data/converter/IntegerToBigDecimalConverter.java new file mode 100644 index 00000000000..aa3fac5d3e7 --- /dev/null +++ b/flow-data/src/main/java/com/vaadin/flow/data/converter/IntegerToBigDecimalConverter.java @@ -0,0 +1,52 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * 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 com.vaadin.flow.data.converter; + +import java.math.BigDecimal; + +import com.vaadin.flow.data.binder.Result; +import com.vaadin.flow.data.binder.ValueContext; + +/** + * A converter that converts from {@link Integer} to {@link BigDecimal} and + * back. + * + * @since 24.5 + */ +public class IntegerToBigDecimalConverter + implements Converter { + + @Override + public Result convertToModel(Integer value, + ValueContext context) { + if (value == null) { + return Result.ok(null); + } + + return Result.ok(BigDecimal.valueOf(value)); + } + + @Override + public Integer convertToPresentation(BigDecimal value, + ValueContext context) { + if (value == null) { + return null; + } + + return value.intValue(); + } +} diff --git a/flow-data/src/main/java/com/vaadin/flow/data/converter/IntegerToDoubleConverter.java b/flow-data/src/main/java/com/vaadin/flow/data/converter/IntegerToDoubleConverter.java new file mode 100644 index 00000000000..2ae9fcbd9ad --- /dev/null +++ b/flow-data/src/main/java/com/vaadin/flow/data/converter/IntegerToDoubleConverter.java @@ -0,0 +1,46 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * 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 com.vaadin.flow.data.converter; + +import com.vaadin.flow.data.binder.Result; +import com.vaadin.flow.data.binder.ValueContext; + +/** + * A converter that converts from {@link Integer} to {@link Double} and back. + * + * @since 24.5 + * @author Vaadin Ltd + */ + +public class IntegerToDoubleConverter implements Converter { + @Override + public Result convertToModel(Integer value, ValueContext context) { + if (value == null) { + return Result.ok(null); + } + + return Result.ok(value.doubleValue()); + } + + @Override + public Integer convertToPresentation(Double value, ValueContext context) { + if (value == null) { + return null; + } + + return value.intValue(); + } +} diff --git a/flow-data/src/main/java/com/vaadin/flow/data/converter/IntegerToLongConverter.java b/flow-data/src/main/java/com/vaadin/flow/data/converter/IntegerToLongConverter.java new file mode 100644 index 00000000000..9374f9ce02c --- /dev/null +++ b/flow-data/src/main/java/com/vaadin/flow/data/converter/IntegerToLongConverter.java @@ -0,0 +1,41 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * 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 com.vaadin.flow.data.converter; + +import com.vaadin.flow.data.binder.Result; +import com.vaadin.flow.data.binder.ValueContext; + +/** + * A converter that converts from {@link Integer} to {@link Long} and back. + */ +public class IntegerToLongConverter implements Converter { + + @Override + public Result convertToModel(Integer value, ValueContext context) { + if (value == null) { + return Result.ok(null); + } + return Result.ok(value.longValue()); + } + + @Override + public Integer convertToPresentation(Long value, ValueContext context) { + if (value == null) { + return null; + } + return value.intValue(); + } +} diff --git a/flow-data/src/main/java/com/vaadin/flow/data/converter/LongToBigDecimalConverter.java b/flow-data/src/main/java/com/vaadin/flow/data/converter/LongToBigDecimalConverter.java new file mode 100644 index 00000000000..dc05bdcae5e --- /dev/null +++ b/flow-data/src/main/java/com/vaadin/flow/data/converter/LongToBigDecimalConverter.java @@ -0,0 +1,43 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * 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 com.vaadin.flow.data.converter; + +import com.vaadin.flow.data.binder.Result; +import com.vaadin.flow.data.binder.ValueContext; + +import java.math.BigDecimal; + +/** + * A converter that converts from {@link Long} to {@link BigDecimal} and back. + */ +public class LongToBigDecimalConverter implements Converter { + + @Override + public Result convertToModel(Long value, ValueContext context) { + if (value == null) { + return Result.ok(null); + } + return Result.ok(BigDecimal.valueOf(value)); + } + + @Override + public Long convertToPresentation(BigDecimal value, ValueContext context) { + if (value == null) { + return null; + } + return value.longValue(); + } +} diff --git a/flow-data/src/test/java/com/vaadin/flow/data/converter/BigDecimalToDoubleConverterTest.java b/flow-data/src/test/java/com/vaadin/flow/data/converter/BigDecimalToDoubleConverterTest.java new file mode 100644 index 00000000000..0f05476bb81 --- /dev/null +++ b/flow-data/src/test/java/com/vaadin/flow/data/converter/BigDecimalToDoubleConverterTest.java @@ -0,0 +1,66 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * 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 com.vaadin.flow.data.converter; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +import com.vaadin.flow.data.binder.Result; + +import java.math.BigDecimal; + +public class BigDecimalToDoubleConverterTest { + + BigDecimalToDoubleConverter converter = new BigDecimalToDoubleConverter(); + + @Test + public void testNullConversionToModel() { + assertEquals(Result.ok(null), converter.convertToModel(null, null)); + } + + @Test + public void testNullConversionToPresentation() { + assertNull(converter.convertToPresentation(null, null)); + } + + @Test + public void testConvertToModel() { + Result result = converter + .convertToModel(BigDecimal.valueOf(42.42), null); + assertEquals(Result.ok(42.42), result); + } + + @Test + public void testConvertToPresentation() { + BigDecimal value = converter.convertToPresentation(42.42, null); + assertEquals(BigDecimal.valueOf(42.42), value); + } + + @Test + public void testConvertToModelWithDifferentScales() { + Result result = converter + .convertToModel(new BigDecimal("42.420"), null); + assertEquals(Result.ok(42.42), result); + } + + @Test + public void testConvertToPresentationWithDifferentScales() { + BigDecimal value = converter.convertToPresentation(42.42, null); + assertEquals(new BigDecimal("42.42"), value); + } +} diff --git a/flow-data/src/test/java/com/vaadin/flow/data/converter/BigDecimalToFloatConverterTest.java b/flow-data/src/test/java/com/vaadin/flow/data/converter/BigDecimalToFloatConverterTest.java new file mode 100644 index 00000000000..c037591f9d4 --- /dev/null +++ b/flow-data/src/test/java/com/vaadin/flow/data/converter/BigDecimalToFloatConverterTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * 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 com.vaadin.flow.data.converter; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +import com.vaadin.flow.data.binder.Result; + +import java.math.BigDecimal; + +public class BigDecimalToFloatConverterTest { + + BigDecimalToFloatConverter converter = new BigDecimalToFloatConverter(); + + @Test + public void testNullConversionToModel() { + assertEquals(Result.ok(null), converter.convertToModel(null, null)); + } + + @Test + public void testNullConversionToPresentation() { + assertNull(converter.convertToPresentation(null, null)); + } + + @Test + public void testConvertToModel() { + Result result = converter + .convertToModel(BigDecimal.valueOf(42.0), null); + assertEquals(Result.ok(42.0f), result); + } + + @Test + public void testConvertToPresentation() { + BigDecimal value = converter.convertToPresentation(42.0f, null); + assertEquals(BigDecimal.valueOf(42.0), value); + } +} diff --git a/flow-data/src/test/java/com/vaadin/flow/data/converter/BigDecimalToIntegerConverterTest.java b/flow-data/src/test/java/com/vaadin/flow/data/converter/BigDecimalToIntegerConverterTest.java new file mode 100644 index 00000000000..d2d611047a9 --- /dev/null +++ b/flow-data/src/test/java/com/vaadin/flow/data/converter/BigDecimalToIntegerConverterTest.java @@ -0,0 +1,66 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * 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 com.vaadin.flow.data.converter; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +import com.vaadin.flow.data.binder.Result; + +import java.math.BigDecimal; + +public class BigDecimalToIntegerConverterTest { + + BigDecimalToIntegerConverter converter = new BigDecimalToIntegerConverter(); + + @Test + public void testNullConversionToModel() { + assertEquals(Result.ok(null), converter.convertToModel(null, null)); + } + + @Test + public void testNullConversionToPresentation() { + assertNull(converter.convertToPresentation(null, null)); + } + + @Test + public void testConvertToModel() { + Result result = converter + .convertToModel(BigDecimal.valueOf(42), null); + assertEquals(Result.ok(42), result); + } + + @Test + public void testConvertToPresentation() { + BigDecimal value = converter.convertToPresentation(42, null); + assertEquals(BigDecimal.valueOf(42), value); + } + + @Test + public void testConvertToModelWithDecimalValue() { + Result result = converter + .convertToModel(BigDecimal.valueOf(42.99), null); + assertEquals(Result.ok(42), result); + } + + @Test + public void testConvertToPresentationWithDecimalValue() { + BigDecimal value = converter.convertToPresentation(42, null); + assertEquals(BigDecimal.valueOf(42), value); + } +} diff --git a/flow-data/src/test/java/com/vaadin/flow/data/converter/BigDecimalToLongConverterTest.java b/flow-data/src/test/java/com/vaadin/flow/data/converter/BigDecimalToLongConverterTest.java new file mode 100644 index 00000000000..6f359f7bb10 --- /dev/null +++ b/flow-data/src/test/java/com/vaadin/flow/data/converter/BigDecimalToLongConverterTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * 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 com.vaadin.flow.data.converter; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +import com.vaadin.flow.data.binder.Result; + +import java.math.BigDecimal; + +public class BigDecimalToLongConverterTest { + + BigDecimalToLongConverter converter = new BigDecimalToLongConverter(); + + @Test + public void testNullConversionToModel() { + assertEquals(Result.ok(null), converter.convertToModel(null, null)); + } + + @Test + public void testNullConversionToPresentation() { + assertNull(converter.convertToPresentation(null, null)); + } + + @Test + public void testConvertToModel() { + Result result = converter.convertToModel(BigDecimal.valueOf(42), + null); + assertEquals(Result.ok(42L), result); + } + + @Test + public void testConvertToPresentation() { + BigDecimal value = converter.convertToPresentation(42L, null); + assertEquals(BigDecimal.valueOf(42), value); + } +} diff --git a/flow-data/src/test/java/com/vaadin/flow/data/converter/DefaultConverterFactoryTest.java b/flow-data/src/test/java/com/vaadin/flow/data/converter/DefaultConverterFactoryTest.java index 74801cbf331..d62944c1e2a 100644 --- a/flow-data/src/test/java/com/vaadin/flow/data/converter/DefaultConverterFactoryTest.java +++ b/flow-data/src/test/java/com/vaadin/flow/data/converter/DefaultConverterFactoryTest.java @@ -116,24 +116,26 @@ private void assertThatConversionIsNotSupported( private Class toPrimitiveTypeIfExist(Class type) { if (!type.isPrimitive()) { - if (type.equals(Boolean.class)) { - type = Boolean.TYPE; - } else if (type.equals(Integer.class)) { - type = Integer.TYPE; - } else if (type.equals(Float.class)) { - type = Float.TYPE; - } else if (type.equals(Double.class)) { - type = Double.TYPE; - } else if (type.equals(Byte.class)) { - type = Byte.TYPE; - } else if (type.equals(Character.class)) { - type = Character.TYPE; - } else if (type.equals(Short.class)) { - type = Short.TYPE; - } else if (type.equals(Long.class)) { - type = Long.TYPE; - } + return type; + } + if (type.equals(Boolean.class)) { + type = Boolean.TYPE; + } else if (type.equals(Integer.class)) { + type = Integer.TYPE; + } else if (type.equals(Float.class)) { + type = Float.TYPE; + } else if (type.equals(Double.class)) { + type = Double.TYPE; + } else if (type.equals(Byte.class)) { + type = Byte.TYPE; + } else if (type.equals(Character.class)) { + type = Character.TYPE; + } else if (type.equals(Short.class)) { + type = Short.TYPE; + } else if (type.equals(Long.class)) { + type = Long.TYPE; } + return type; } diff --git a/flow-data/src/test/java/com/vaadin/flow/data/converter/DoubleToBigDecimalConverterTest.java b/flow-data/src/test/java/com/vaadin/flow/data/converter/DoubleToBigDecimalConverterTest.java new file mode 100644 index 00000000000..a85a7549d0e --- /dev/null +++ b/flow-data/src/test/java/com/vaadin/flow/data/converter/DoubleToBigDecimalConverterTest.java @@ -0,0 +1,66 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * 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 com.vaadin.flow.data.converter; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +import com.vaadin.flow.data.binder.Result; + +import java.math.BigDecimal; + +public class DoubleToBigDecimalConverterTest { + + DoubleToBigDecimalConverter converter = new DoubleToBigDecimalConverter(); + + @Test + public void testNullConversionToModel() { + assertEquals(Result.ok(null), converter.convertToModel(null, null)); + } + + @Test + public void testNullConversionToPresentation() { + assertNull(converter.convertToPresentation(null, null)); + } + + @Test + public void testConvertToModel() { + Result result = converter.convertToModel(42.42, null); + assertEquals(Result.ok(BigDecimal.valueOf(42.42)), result); + } + + @Test + public void testConvertToPresentation() { + Double value = converter.convertToPresentation(new BigDecimal("42.42"), + null); + assertEquals(Double.valueOf(42.42), value); + } + + @Test + public void testConvertToModelWithDifferentScales() { + Result result = converter.convertToModel(42.420, null); + assertEquals(Result.ok(new BigDecimal("42.42")), result); + } + + @Test + public void testConvertToPresentationWithDifferentScales() { + Double value = converter.convertToPresentation(new BigDecimal("42.420"), + null); + assertEquals(Double.valueOf(42.42), value); + } +} diff --git a/flow-data/src/test/java/com/vaadin/flow/data/converter/FloatToBigDecimalConverterTest.java b/flow-data/src/test/java/com/vaadin/flow/data/converter/FloatToBigDecimalConverterTest.java new file mode 100644 index 00000000000..a125d941de6 --- /dev/null +++ b/flow-data/src/test/java/com/vaadin/flow/data/converter/FloatToBigDecimalConverterTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * 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 com.vaadin.flow.data.converter; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +import com.vaadin.flow.data.binder.Result; + +import java.math.BigDecimal; + +public class FloatToBigDecimalConverterTest { + + FloatToBigDecimalConverter converter = new FloatToBigDecimalConverter(); + + @Test + public void testNullConversionToModel() { + assertEquals(Result.ok(null), converter.convertToModel(null, null)); + } + + @Test + public void testNullConversionToPresentation() { + assertNull(converter.convertToPresentation(null, null)); + } + + @Test + public void testConvertToModel() { + Result result = converter.convertToModel(42.0f, null); + assertEquals(Result.ok(BigDecimal.valueOf(42.0f)), result); + } + + @Test + public void testConvertToPresentation() { + Float value = converter.convertToPresentation(BigDecimal.valueOf(42.0f), + null); + assertEquals(Float.valueOf(42.0f), value); + } +} diff --git a/flow-data/src/test/java/com/vaadin/flow/data/converter/FloatToDoubleConverterTest.java b/flow-data/src/test/java/com/vaadin/flow/data/converter/FloatToDoubleConverterTest.java new file mode 100644 index 00000000000..74a3fb392c6 --- /dev/null +++ b/flow-data/src/test/java/com/vaadin/flow/data/converter/FloatToDoubleConverterTest.java @@ -0,0 +1,89 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * 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 com.vaadin.flow.data.converter; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import com.vaadin.flow.data.binder.Result; +import org.junit.Test; + +public class FloatToDoubleConverterTest { + + FloatToDoubleConverter converter = new FloatToDoubleConverter(); + + @Test + public void testNullConversionToModel() { + assertEquals(Result.ok(null), converter.convertToModel(null, null)); + } + + @Test + public void testNullConversionToPresentation() { + assertNull(converter.convertToPresentation(null, null)); + } + + @Test + public void testConvertToModel() { + assertEquals(Result.ok(42.0), converter.convertToModel(42.0f, null)); + } + + @Test + public void testConvertToPresentation() { + assertEquals(Float.valueOf(42.0f), converter.convertToPresentation(42.0, null)); + } + + // Test conversion of extreme values + @Test + public void testConvertToModelMaxFloat() { + assertEquals(Result.ok((double) Float.MAX_VALUE), converter.convertToModel(Float.MAX_VALUE, null)); + } + @Test + public void testConvertToPresentationMaxFloat() { + assertEquals(Float.valueOf(Float.MAX_VALUE), converter.convertToPresentation((double) Float.MAX_VALUE, null)); + } + + @Test + public void testConvertToModelMinFloat() { + assertEquals(Result.ok((double) Float.MIN_VALUE), converter.convertToModel(Float.MIN_VALUE, null)); + } + + @Test + public void testConvertToPresentationMinFloat() { + assertEquals(Float.valueOf(Float.MIN_VALUE), converter.convertToPresentation((double) Float.MIN_VALUE, null)); + } + + + @Test + public void testConvertToModelPositiveInfinity() { + assertEquals(Result.ok(Double.POSITIVE_INFINITY), converter.convertToModel(Float.POSITIVE_INFINITY, null)); + } + + @Test + public void testConvertToPresentationPositiveInfinity() { + assertEquals(Float.valueOf(Float.POSITIVE_INFINITY), converter.convertToPresentation(Double.POSITIVE_INFINITY, null)); + } + + @Test + public void testConvertToModelNegativeInfinity() { + assertEquals(Result.ok(Double.NEGATIVE_INFINITY), converter.convertToModel(Float.NEGATIVE_INFINITY, null)); + } + + @Test + public void testConvertToPresentationNegativeInfinity() { + assertEquals(Float.valueOf(Float.NEGATIVE_INFINITY), converter.convertToPresentation(Double.NEGATIVE_INFINITY, null)); + } + + +} diff --git a/flow-data/src/test/java/com/vaadin/flow/data/converter/IntegerToBigDecimalConverterTest.java b/flow-data/src/test/java/com/vaadin/flow/data/converter/IntegerToBigDecimalConverterTest.java new file mode 100644 index 00000000000..37260727c73 --- /dev/null +++ b/flow-data/src/test/java/com/vaadin/flow/data/converter/IntegerToBigDecimalConverterTest.java @@ -0,0 +1,52 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * 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 com.vaadin.flow.data.converter; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +import com.vaadin.flow.data.binder.Result; + +import java.math.BigDecimal; + +public class IntegerToBigDecimalConverterTest { + + IntegerToBigDecimalConverter converter = new IntegerToBigDecimalConverter(); + + @Test + public void testNullConversionToModel() { + assertEquals(converter.convertToModel(null, null), Result.ok(null)); + } + + @Test + public void testNullConversionToPresentation() { + assertNull(converter.convertToPresentation(null, null)); + } + + @Test + public void testConvertToModel() { + assertEquals(Result.ok(BigDecimal.valueOf(42)), + converter.convertToModel(42, null)); + } + + @Test + public void testConvertToPresentation() { + assertEquals(Integer.valueOf(42), + converter.convertToPresentation(BigDecimal.valueOf(42), null)); + } +} diff --git a/flow-data/src/test/java/com/vaadin/flow/data/converter/IntegerToDoubleConverterTest.java b/flow-data/src/test/java/com/vaadin/flow/data/converter/IntegerToDoubleConverterTest.java new file mode 100644 index 00000000000..4b54702da2b --- /dev/null +++ b/flow-data/src/test/java/com/vaadin/flow/data/converter/IntegerToDoubleConverterTest.java @@ -0,0 +1,49 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * 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 com.vaadin.flow.data.converter; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +import com.vaadin.flow.data.binder.Result; + +public class IntegerToDoubleConverterTest { + + IntegerToDoubleConverter converter = new IntegerToDoubleConverter(); + + @Test + public void testNullConversionToModel() { + assertEquals(converter.convertToModel(null, null), Result.ok(null)); + } + + @Test + public void testNullConversionToPresentation() { + assertNull(converter.convertToPresentation(null, null)); + } + + @Test + public void testConvertToModel() { + assertEquals(Result.ok(42.0), converter.convertToModel(42, null)); + } + + @Test + public void testConvertToPresentation() { + assertEquals(Integer.valueOf(42), + converter.convertToPresentation(42.0, null)); + } +} diff --git a/flow-data/src/test/java/com/vaadin/flow/data/converter/IntegerToLongConverterTest.java b/flow-data/src/test/java/com/vaadin/flow/data/converter/IntegerToLongConverterTest.java new file mode 100644 index 00000000000..c4f32698883 --- /dev/null +++ b/flow-data/src/test/java/com/vaadin/flow/data/converter/IntegerToLongConverterTest.java @@ -0,0 +1,50 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * 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 com.vaadin.flow.data.converter; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +import com.vaadin.flow.data.binder.Result; + +public class IntegerToLongConverterTest { + + IntegerToLongConverter converter = new IntegerToLongConverter(); + + @Test + public void testNullConversionToModel() { + assertEquals(Result.ok(null), converter.convertToModel(null, null)); + } + + @Test + public void testNullConversionToPresentation() { + assertNull(converter.convertToPresentation(null, null)); + } + + @Test + public void testConvertToModel() { + Result result = converter.convertToModel(42, null); + assertEquals(Result.ok(42L), result); + } + + @Test + public void testConvertToPresentation() { + Integer value = converter.convertToPresentation(42L, null); + assertEquals(Integer.valueOf(42), value); + } +} diff --git a/flow-data/src/test/java/com/vaadin/flow/data/converter/LongToBigDecimalConverterTest.java b/flow-data/src/test/java/com/vaadin/flow/data/converter/LongToBigDecimalConverterTest.java new file mode 100644 index 00000000000..9321b44b3dd --- /dev/null +++ b/flow-data/src/test/java/com/vaadin/flow/data/converter/LongToBigDecimalConverterTest.java @@ -0,0 +1,68 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * 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 com.vaadin.flow.data.converter; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +import com.vaadin.flow.data.binder.Result; + +import java.math.BigDecimal; + +public class LongToBigDecimalConverterTest { + + LongToBigDecimalConverter converter = new LongToBigDecimalConverter(); + + @Test + public void testNullConversionToModel() { + assertEquals(Result.ok(null), converter.convertToModel(null, null)); + } + + @Test + public void testNullConversionToPresentation() { + assertNull(converter.convertToPresentation(null, null)); + } + + @Test + public void testConvertToModel() { + Result result = converter.convertToModel(42L, null); + assertEquals(Result.ok(BigDecimal.valueOf(42)), result); + } + + @Test + public void testConvertToPresentation() { + Long value = converter.convertToPresentation(BigDecimal.valueOf(42), + null); + assertEquals(Long.valueOf(42), value); + } + + @Test + public void testConvertToModelWithLargeValue() { + Result result = converter.convertToModel(2147483648L, null); // large + // value + assertEquals(Result.ok(BigDecimal.valueOf(2147483648L)), result); + } + + @Test + public void testConvertToPresentationWithLargeValue() { + Long value = converter + .convertToPresentation(BigDecimal.valueOf(2147483648L), null); // large + // value + assertEquals(Long.valueOf(2147483648L), value); + } +}