-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create converters for numeric values from NumberField (#19469)
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
1 parent
a463b40
commit 6260b07
Showing
24 changed files
with
1,227 additions
and
18 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
flow-data/src/main/java/com/vaadin/flow/data/converter/BigDecimalToDoubleConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
flow-data/src/main/java/com/vaadin/flow/data/converter/BigDecimalToFloatConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
flow-data/src/main/java/com/vaadin/flow/data/converter/BigDecimalToIntegerConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
flow-data/src/main/java/com/vaadin/flow/data/converter/BigDecimalToLongConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
flow-data/src/main/java/com/vaadin/flow/data/converter/DoubleToBigDecimalConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
flow-data/src/main/java/com/vaadin/flow/data/converter/FloatToBigDecimalConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
flow-data/src/main/java/com/vaadin/flow/data/converter/FloatToDoubleConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
Oops, something went wrong.