-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced BigDecimalConverter (#4557)
* Introduced BigDecimalConverter that users can use as part of convert_entry_type processor that currently exists. Optionally, users can also specify required scaling needed on the converted Signed-off-by: Santhosh Gandhe <gandheaz@amazon.com> * Added Test case for the newly introduced class. Removed * imports as per the review comment Signed-off-by: Santhosh Gandhe <1909520+san81@users.noreply.github.com> * Avoiding using a deprecated method. Added additional test cases Signed-off-by: Santhosh Gandhe <1909520+san81@users.noreply.github.com> * Additional tests to increase the coverage Signed-off-by: Santhosh Gandhe <1909520+san81@users.noreply.github.com> * removed "scale" being the state of BigDecimal converter. We are now passing the scale while converting the instance only when the instance is BigDecimalConverter Signed-off-by: Santhosh Gandhe <1909520+san81@users.noreply.github.com> * test case fix to be inline with the previous commit Signed-off-by: Santhosh Gandhe <1909520+san81@users.noreply.github.com> * test case fix to be inline with the previous commit Signed-off-by: Santhosh Gandhe <1909520+san81@users.noreply.github.com> * addressing review comments Signed-off-by: Santhosh Gandhe <1909520+san81@users.noreply.github.com> * renaming bigdecimal to big_decimal Signed-off-by: Santhosh Gandhe <1909520+san81@users.noreply.github.com> * Introduced ConverterArguments as a way to pass additional arguments to the converter and avoided conditional statement for calling converter methods Signed-off-by: Santhosh Gandhe <1909520+san81@users.noreply.github.com> * Added additional override convert method to reduce the changes across the code Signed-off-by: Santhosh Gandhe <1909520+san81@users.noreply.github.com> * additional Test cases to increase the coverage Signed-off-by: Santhosh Gandhe <1909520+san81@users.noreply.github.com> * added additional tests for converter cases Signed-off-by: Santhosh Gandhe <1909520+san81@users.noreply.github.com> --------- Signed-off-by: Santhosh Gandhe <gandheaz@amazon.com> Signed-off-by: Santhosh Gandhe <1909520+san81@users.noreply.github.com>
- Loading branch information
Showing
27 changed files
with
470 additions
and
75 deletions.
There are no files selected for viewing
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
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
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
58 changes: 58 additions & 0 deletions
58
...epper-api/src/main/java/org/opensearch/dataprepper/typeconverter/BigDecimalConverter.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,58 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.typeconverter; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
|
||
/** | ||
* Converter class for BigDecimal data type. By default, it applies zero scaling keeping the original value as it is. | ||
* If required, the scale can be set using the setScale method. | ||
*/ | ||
public class BigDecimalConverter implements TypeConverter<BigDecimal> { | ||
|
||
public BigDecimal convert(Object source) throws IllegalArgumentException { | ||
return this.convert(source, 0); | ||
} | ||
|
||
public BigDecimal convert(Object source, ConverterArguments arguments) throws IllegalArgumentException { | ||
return this.convert(source, arguments.getScale()); | ||
} | ||
|
||
public BigDecimal convert(Object source, int scale) throws IllegalArgumentException { | ||
BigDecimal result = null; | ||
if (source instanceof String) { | ||
result = new BigDecimal((String)source); | ||
} | ||
else if (source instanceof Float) { | ||
result = BigDecimal.valueOf((Float)source); | ||
} | ||
else if (source instanceof Double) { | ||
result = BigDecimal.valueOf((Double)source); | ||
} | ||
else if (source instanceof Boolean) { | ||
result = ((Boolean)source) ? BigDecimal.valueOf(1L) : BigDecimal.valueOf(0L); | ||
} | ||
else if (source instanceof Integer) { | ||
result = BigDecimal.valueOf((Integer)source); | ||
} | ||
else if (source instanceof Long) { | ||
result = BigDecimal.valueOf((Long)source); | ||
} | ||
else if (source instanceof BigDecimal) { | ||
result = ((BigDecimal)source); | ||
} | ||
|
||
if(result!=null) { | ||
if(scale!=0) { | ||
result = result.setScale(scale, RoundingMode.HALF_EVEN); | ||
} | ||
return result; | ||
} | ||
throw new IllegalArgumentException("Unsupported type conversion. From Source class: " + source.getClass() + " to BigDecimal"); | ||
} | ||
} | ||
|
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
10 changes: 10 additions & 0 deletions
10
...repper-api/src/main/java/org/opensearch/dataprepper/typeconverter/ConverterArguments.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,10 @@ | ||
package org.opensearch.dataprepper.typeconverter; | ||
|
||
/** | ||
* Interface for arguments passed to the {@link TypeConverter} | ||
* | ||
* @since 1.2 | ||
*/ | ||
public interface ConverterArguments { | ||
int getScale(); | ||
} |
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
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
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
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
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
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
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
Oops, something went wrong.