Skip to content

Commit 9d5bbf5

Browse files
authored
MINOR: Add javadoc for ConfigDef.convertToString() (apache#21387)
Also added test case for `convertToString()` with Double values to highlight Java uses scientific notation for large (and small) values. For example if you set `log.cleaner.io.max.bytes.per.second` to `102400000` via the Admin API, when describing it, you get `1.024E8` back, not `102400000`. Reviewers: Chia-Ping Tsai <chia7712@gmail.com>
1 parent 8e3d731 commit 9d5bbf5

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

clients/src/main/java/org/apache/kafka/common/config/ConfigDef.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,17 @@ else if (value instanceof String) {
790790
}
791791
}
792792

793+
/**
794+
* Convert the provided object into a string based on its type.
795+
* <p>
796+
* This method uses Java's {@link #toString()} for {@link Type#BOOLEAN}, {@link Type#SHORT}, {@link Type#INT},
797+
* {@link Type#LONG}, {@link Type#DOUBLE}, {@link Type#STRING} and {@link Type#PASSWORD} objects.
798+
* For {@link Type#LIST} objects, Java's {@link #toString()} is used for each entry and entries are concatenated
799+
* separated by commas. For {@link Type#CLASS} objects, {@link Class#getName()} is used.
800+
* @param parsedValue The object to convert into a string
801+
* @param type The type of the object
802+
* @return The string representation of the provided object and type
803+
*/
793804
public static String convertToString(Object parsedValue, Type type) {
794805
if (parsedValue == null) {
795806
return null;

clients/src/test/java/org/apache/kafka/common/config/ConfigDefTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,10 @@ public void testConvertValueToStringLong() {
616616

617617
@Test
618618
public void testConvertValueToStringDouble() {
619-
assertEquals("3.125", ConfigDef.convertToString(3.125, Type.DOUBLE));
619+
assertEquals("3.125", ConfigDef.convertToString(3.125d, Type.DOUBLE));
620+
assertEquals("1.7976931348623157E308", ConfigDef.convertToString(Double.MAX_VALUE, Type.DOUBLE));
621+
assertEquals("1.024E8", ConfigDef.convertToString(102400000d, Type.DOUBLE));
622+
assertEquals("-1.024E8", ConfigDef.convertToString(-102400000d, Type.DOUBLE));
620623
assertNull(ConfigDef.convertToString(null, Type.DOUBLE));
621624
}
622625

0 commit comments

Comments
 (0)