From 4527bb2fed890098ed66f428825eb33c3a8c9f57 Mon Sep 17 00:00:00 2001 From: Davide Angelocola Date: Mon, 9 May 2022 12:34:26 -0700 Subject: [PATCH] Fix conversion issues in MutableOptions (#9194) Summary: Removing unnecessary checks around conversion from int/long to double as it does not lose information (see https://docs.oracle.com/javase/specs/jls/se9/html/jls-5.html#jls-5.1.2). For example, `value > Double.MAX_VALUE` is always false when value is long or int. Can you please have a look adamretter? Also fixed some other minor issues (do you prefer a separate PR?) Pull Request resolved: https://github.com/facebook/rocksdb/pull/9194 Reviewed By: ajkr Differential Revision: D36221694 fbshipit-source-id: bf327c07386560b87ddc0c98039e8d6e8f2f1e82 --- .../main/java/org/rocksdb/AbstractMutableOptions.java | 8 ++++---- java/src/main/java/org/rocksdb/MutableOptionValue.java | 9 +-------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/java/src/main/java/org/rocksdb/AbstractMutableOptions.java b/java/src/main/java/org/rocksdb/AbstractMutableOptions.java index b0e8a043a..243b8040a 100644 --- a/java/src/main/java/org/rocksdb/AbstractMutableOptions.java +++ b/java/src/main/java/org/rocksdb/AbstractMutableOptions.java @@ -81,8 +81,8 @@ public abstract class AbstractMutableOptions { protected abstract T build(final String[] keys, final String[] values); public T build() { - final String keys[] = new String[options.size()]; - final String values[] = new String[options.size()]; + final String[] keys = new String[options.size()]; + final String[] values = new String[options.size()]; int i = 0; for (final Map.Entry> option : options.entrySet()) { @@ -227,7 +227,7 @@ public abstract class AbstractMutableOptions { } catch (NumberFormatException nfe) { final double doubleValue = Double.parseDouble(value); if (doubleValue != Math.round(doubleValue)) - throw new IllegalArgumentException("Unable to parse or round " + value + " to int"); + throw new IllegalArgumentException("Unable to parse or round " + value + " to long"); return Math.round(doubleValue); } } @@ -245,7 +245,7 @@ public abstract class AbstractMutableOptions { } catch (NumberFormatException nfe) { final double doubleValue = Double.parseDouble(value); if (doubleValue != Math.round(doubleValue)) - throw new IllegalArgumentException("Unable to parse or round " + value + " to long"); + throw new IllegalArgumentException("Unable to parse or round " + value + " to int"); return (int) Math.round(doubleValue); } } diff --git a/java/src/main/java/org/rocksdb/MutableOptionValue.java b/java/src/main/java/org/rocksdb/MutableOptionValue.java index 50d733d5c..7f69eeb9e 100644 --- a/java/src/main/java/org/rocksdb/MutableOptionValue.java +++ b/java/src/main/java/org/rocksdb/MutableOptionValue.java @@ -17,7 +17,7 @@ public abstract class MutableOptionValue { extends MutableOptionValue { protected final T value; - private MutableOptionValueObject(final T value) { + protected MutableOptionValueObject(final T value) { this.value = value; } @@ -153,10 +153,6 @@ public abstract class MutableOptionValue { @Override double asDouble() { - if(value > Double.MAX_VALUE || value < Double.MIN_VALUE) { - throw new NumberFormatException( - "long value lies outside the bounds of int"); - } return Long.valueOf(value).doubleValue(); } @@ -210,9 +206,6 @@ public abstract class MutableOptionValue { @Override double asDouble() { - if(value > Double.MAX_VALUE || value < Double.MIN_VALUE) { - throw new NumberFormatException("int value lies outside the bounds of int"); - } return Integer.valueOf(value).doubleValue(); }