Skip to content

Commit

Permalink
feat: Create converters for numeric values from NumberField (#19469)
Browse files Browse the repository at this point in the history
This pull request introduces converter classes which converts between Numeric data types. This change addresses the need for such converters as outlined in issue #5134. With the availability of a NumberField in Vaadin, it is essential to have converters that facilitate the use of various number formats with this component.

Fixes #5134
  • Loading branch information
AlainaFaisal authored Jun 4, 2024
1 parent a463b40 commit 6260b07
Show file tree
Hide file tree
Showing 24 changed files with 1,227 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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<BigDecimal, Double> {

@Override
public Result<Double> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<BigDecimal, Float> {

@Override
public Result<Float> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<BigDecimal, Integer> {

@Override
public Result<Integer> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<BigDecimal, Long> {

@Override
public Result<Long> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <C extends Converter<?, ?>> void registerConverter(
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Double, BigDecimal> {

@Override
public Result<BigDecimal> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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<Float, BigDecimal> {

@Override
public Result<BigDecimal> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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<Float, Double> {

@Override
public Result<Double> 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();
}
}

Loading

0 comments on commit 6260b07

Please sign in to comment.