diff --git a/server/src/main/java/org/opensearch/common/settings/Setting.java b/server/src/main/java/org/opensearch/common/settings/Setting.java index 73732a338b5..ef10f9a42c5 100644 --- a/server/src/main/java/org/opensearch/common/settings/Setting.java +++ b/server/src/main/java/org/opensearch/common/settings/Setting.java @@ -1160,62 +1160,259 @@ public class Setting implements ToXContentObject { return new Setting<>(key, s -> Integer.toString(defaultValue.id), s -> Version.fromId(Integer.parseInt(s)), properties); } - public static Setting floatSetting(String key, float defaultValue, Property... properties) { - return new Setting<>(key, (s) -> Float.toString(defaultValue), Float::parseFloat, properties); - } - - public static Setting floatSetting(String key, float defaultValue, float minValue, Property... properties) { - return new Setting<>(key, (s) -> Float.toString(defaultValue), (s) -> { - float value = Float.parseFloat(s); - if (value < minValue) { - String err = "Failed to parse value" + - (isFiltered(properties) ? "" : " [" + s + "]") + " for setting [" + key + "] must be >= " + minValue; - throw new IllegalArgumentException(err); - } - return value; - }, properties); - } - private static boolean isFiltered(Property[] properties) { return properties != null && Arrays.asList(properties).contains(Property.Filtered); } - public static Setting intSetting(String key, int defaultValue, int minValue, int maxValue, Property... properties) { - return new Setting<>(key, (s) -> Integer.toString(defaultValue), - (s) -> parseInt(s, minValue, maxValue, key, isFiltered(properties)), properties); + // Float + + private static float parseFloat(String s, float minValue, float maxValue, String key, boolean isFiltered) { + float value = Float.parseFloat(s); + if (value < minValue) { + String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be >= " + minValue; + throw new IllegalArgumentException(err); + } + if (value > maxValue) { + String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be <= " + maxValue; + throw new IllegalArgumentException(err); + } + return value; + } + + // Setting with defaultValue + + public static Setting floatSetting(String key, float defaultValue, Property... properties) { + return floatSetting(key, defaultValue, Float.MIN_VALUE, Float.MAX_VALUE, properties); + } + + public static Setting floatSetting(String key, float defaultValue, float minValue, Property... properties) { + return floatSetting(key, defaultValue, minValue, Float.MAX_VALUE, properties); + } + + public static Setting floatSetting(String key, float defaultValue, float minValue, float maxValue, Property... properties) { + return floatSetting(key, defaultValue, minValue, maxValue, v -> {}, properties); + } + + public static Setting floatSetting(String key, float defaultValue, float minValue, float maxValue, + Validator validator, Property... properties) { + return new Setting<>(key, Float.toString(defaultValue), (s) -> parseFloat(s, minValue, maxValue, key, isFiltered(properties)), + validator, properties); + } + + // Setting with fallback + + public static Setting floatSetting(String key, Setting fallbackSetting, Property... properties) { + return floatSetting(key, fallbackSetting, Float.MIN_VALUE, Float.MAX_VALUE, properties); + } + + public static Setting floatSetting(String key, Setting fallbackSetting, float minValue, Property... properties) { + return floatSetting(key, fallbackSetting, minValue, Float.MAX_VALUE, properties); + } + + public static Setting floatSetting(String key, Setting fallbackSetting, float minValue, float maxValue, + Property... properties) { + return floatSetting(key, fallbackSetting, minValue, maxValue, v -> {}, properties); + } + + public static Setting floatSetting(String key, Setting fallbackSetting, float minValue, float maxValue, + Validator validator, Property... properties) { + return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw, (s) -> parseFloat(s, minValue, maxValue, key, + isFiltered(properties)), validator, properties); + } + + // Integer + + public static int parseInt(String s, int minValue, String key) { + return parseInt(s, minValue, Integer.MAX_VALUE, key); + } + + public static int parseInt(String s, int minValue, int maxValue, String key) { + return parseInt(s, minValue, maxValue, key, false); + } + + public static int parseInt(String s, int minValue, int maxValue, String key, boolean isFiltered) { + int value = Integer.parseInt(s); + if (value < minValue) { + String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be >= " + minValue; + throw new IllegalArgumentException(err); + } + if (value > maxValue) { + String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be <= " + maxValue; + throw new IllegalArgumentException(err); + } + return value; + } + + // Setting with defaultValue + + public static Setting intSetting(String key, int defaultValue, Property... properties) { + return intSetting(key, defaultValue, Integer.MIN_VALUE, Integer.MAX_VALUE, properties); } public static Setting intSetting(String key, int defaultValue, int minValue, Property... properties) { - return new Setting<>(key, (s) -> Integer.toString(defaultValue), (s) -> parseInt(s, minValue, key, isFiltered(properties)), - properties); + return intSetting(key, defaultValue, minValue, Integer.MAX_VALUE, properties); + } + + public static Setting intSetting(String key, int defaultValue, int minValue, int maxValue, Property... properties) { + return intSetting(key, defaultValue, minValue, maxValue, v -> {}, properties); } - public static Setting intSetting(String key, int defaultValue, int minValue, Validator validator, - Property... properties) { - return new Setting<>(key, Integer.toString(defaultValue), (s) -> parseInt(s, minValue, key, isFiltered(properties)), validator, - properties); + public static Setting intSetting(String key, int defaultValue, int minValue, + Validator validator, Property... properties) { + return intSetting(key, defaultValue, minValue, Integer.MAX_VALUE, validator, properties); + } + + public static Setting intSetting(String key, int defaultValue, int minValue, int maxValue, + Validator validator, Property... properties) { + return new Setting<>(key, Integer.toString(defaultValue), (s) -> parseInt(s, minValue, maxValue, key, isFiltered(properties)), + validator, properties); + } + + // Setting with fallback + + public static Setting intSetting(String key, Setting fallbackSetting, Property... properties) { + return intSetting(key, fallbackSetting, Integer.MIN_VALUE, Integer.MAX_VALUE, properties); } public static Setting intSetting(String key, Setting fallbackSetting, int minValue, Property... properties) { - return new Setting<>(key, fallbackSetting, (s) -> parseInt(s, minValue, key, isFiltered(properties)), properties); + return intSetting(key, fallbackSetting, minValue, Integer.MAX_VALUE, properties); } - public static Setting intSetting(String key, Setting fallbackSetting, int minValue, int maxValue, - Property... properties) { - return new Setting<>(key, fallbackSetting, (s) -> parseInt(s, minValue, maxValue, key, isFiltered(properties)), properties); + public static Setting intSetting(String key, Setting fallbackSetting, int minValue, int maxValue, + Property... properties) { + return intSetting(key, fallbackSetting, minValue, maxValue, v -> {}, properties); } - public static Setting intSetting(String key, Setting fallbackSetting, int minValue, Validator validator, - Property... properties) { - return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw, - (s) -> parseInt(s, minValue, key, isFiltered(properties)),validator, properties); + public static Setting intSetting(String key, Setting fallbackSetting, int minValue, + Validator validator, Property... properties) { + return intSetting(key, fallbackSetting, minValue, Integer.MAX_VALUE, validator, properties); + } + + public static Setting intSetting(String key, Setting fallbackSetting, int minValue, int maxValue, + Validator validator, Property... properties) { + return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw, (s) -> parseInt(s, minValue, maxValue, key, + isFiltered(properties)), validator, properties); + } + + // Long + + private static long parseLong(String s, long minValue, long maxValue, String key, boolean isFiltered) { + long value = Long.parseLong(s); + if (value < minValue) { + String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be >= " + minValue; + throw new IllegalArgumentException(err); + } + if (value > maxValue) { + String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be <= " + maxValue; + throw new IllegalArgumentException(err); + } + return value; + } + + // Setting with defaultValue + + public static Setting longSetting(String key, long defaultValue, Property... properties) { + return longSetting(key, defaultValue, Long.MIN_VALUE, Long.MAX_VALUE, properties); } public static Setting longSetting(String key, long defaultValue, long minValue, Property... properties) { - return new Setting<>(key, (s) -> Long.toString(defaultValue), (s) -> parseLong(s, minValue, key, isFiltered(properties)), - properties); + return longSetting(key, defaultValue, minValue, Long.MAX_VALUE, properties); + } + + public static Setting longSetting(String key, long defaultValue, long minValue, long maxValue, Property... properties) { + return longSetting(key, defaultValue, minValue, maxValue, v -> {}, properties); } + public static Setting longSetting(String key, long defaultValue, long minValue, long maxValue, + Validator validator, Property... properties) { + return new Setting<>(key, Long.toString(defaultValue), (s) -> parseLong(s, minValue, maxValue, key, + isFiltered(properties)), validator, properties); + } + + // Setting with fallback + + public static Setting longSetting(String key, Setting fallbackSetting, Property... properties) { + return longSetting(key, fallbackSetting, Long.MIN_VALUE, Long.MAX_VALUE, properties); + } + + public static Setting longSetting(String key, Setting fallbackSetting, long minValue, Property... properties) { + return longSetting(key, fallbackSetting, minValue, Long.MAX_VALUE, properties); + } + + public static Setting longSetting(String key, Setting fallbackSetting, long minValue, long maxValue, + Property... properties) { + return longSetting(key, fallbackSetting, minValue, maxValue, v -> {}, properties); + } + + public static Setting longSetting(String key, Setting fallbackSetting, long minValue, + Validator validator, Property... properties) { + return longSetting(key, fallbackSetting, minValue, Long.MAX_VALUE, validator, properties); + } + + public static Setting longSetting(String key, Setting fallbackSetting, long minValue, long maxValue, + Validator validator, Property... properties) { + return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw, (s) -> parseLong(s, minValue, maxValue, key, + isFiltered(properties)), validator, properties); + } + + // Double + + private static double parseDouble(String s, double minValue, double maxValue, String key, boolean isFiltered) { + double value = Double.parseDouble(s); + if (value < minValue) { + String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be >= " + minValue; + throw new IllegalArgumentException(err); + } + if (value > maxValue) { + String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be <= " + maxValue; + throw new IllegalArgumentException(err); + } + return value; + } + + // Setting with defaultValue + + public static Setting doubleSetting(String key, double defaultValue, Property... properties) { + return doubleSetting(key, defaultValue, Double.MIN_VALUE, Double.MAX_VALUE, properties); + } + + public static Setting doubleSetting(String key, double defaultValue, double minValue, Property... properties) { + return doubleSetting(key, defaultValue, minValue, Double.MAX_VALUE, properties); + } + + public static Setting doubleSetting(String key, double defaultValue, double minValue, double maxValue, Property... properties) { + return doubleSetting(key, defaultValue, minValue, maxValue, v -> {}, properties); + } + + public static Setting doubleSetting(String key, double defaultValue, double minValue, double maxValue, + Validator validator, Property... properties) { + return new Setting<>(key, Double.toString(defaultValue), (s) -> parseDouble(s, minValue, maxValue, key, isFiltered(properties)), + validator, properties); + } + + // Setting with fallback + + public static Setting doubleSetting(String key, Setting fallbackSetting, Property... properties) { + return doubleSetting(key, fallbackSetting, Double.MIN_VALUE, Double.MAX_VALUE, properties); + } + + public static Setting doubleSetting(String key, Setting fallbackSetting, double minValue, Property... properties) { + return doubleSetting(key, fallbackSetting, minValue, Double.MAX_VALUE, properties); + } + + public static Setting doubleSetting(String key, Setting fallbackSetting, double minValue, double maxValue, + Property... properties) { + return doubleSetting(key, fallbackSetting, minValue, maxValue, v -> {}, properties); + } + + public static Setting doubleSetting(String key, Setting fallbackSetting, double minValue, double maxValue, + Validator validator, Property... properties) { + return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw, (s) -> parseDouble(s, minValue, maxValue, key, + isFiltered(properties)), validator, properties); + } + + /// simpleString + public static Setting simpleString(String key, Property... properties) { return new Setting<>(key, s -> "", Function.identity(), properties); } @@ -1257,44 +1454,6 @@ public class Setting implements ToXContentObject { return new Setting<>(key, s -> defaultValue, Function.identity(), properties); } - public static int parseInt(String s, int minValue, String key) { - return parseInt(s, minValue, Integer.MAX_VALUE, key, false); - } - - public static int parseInt(String s, int minValue, String key, boolean isFiltered) { - return parseInt(s, minValue, Integer.MAX_VALUE, key, isFiltered); - } - - public static int parseInt(String s, int minValue, int maxValue, String key) { - return parseInt(s, minValue, maxValue, key, false); - } - - public static int parseInt(String s, int minValue, int maxValue, String key, boolean isFiltered) { - int value = Integer.parseInt(s); - if (value < minValue) { - String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be >= " + minValue; - throw new IllegalArgumentException(err); - } - if (value > maxValue) { - String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be <= " + maxValue; - throw new IllegalArgumentException(err); - } - return value; - } - - public static long parseLong(String s, long minValue, String key) { - return parseLong(s, minValue, key, false); - } - - static long parseLong(String s, long minValue, String key, boolean isFiltered) { - long value = Long.parseLong(s); - if (value < minValue) { - String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be >= " + minValue; - throw new IllegalArgumentException(err); - } - return value; - } - public static TimeValue parseTimeValue(String s, TimeValue minValue, String key) { TimeValue timeValue = TimeValue.parseTimeValue(s, null, key); if (timeValue.millis() < minValue.millis()) { @@ -1303,10 +1462,6 @@ public class Setting implements ToXContentObject { return timeValue; } - public static Setting intSetting(String key, int defaultValue, Property... properties) { - return intSetting(key, defaultValue, Integer.MIN_VALUE, properties); - } - public static Setting boolSetting(String key, boolean defaultValue, Property... properties) { return new Setting<>(key, (s) -> Boolean.toString(defaultValue), b -> parseBoolean(b, key, isFiltered(properties)), properties); } @@ -1315,14 +1470,17 @@ public class Setting implements ToXContentObject { return new Setting<>(key, fallbackSetting, b -> parseBoolean(b, key, isFiltered(properties)), properties); } - public static Setting boolSetting(String key, Setting fallbackSetting, Validator validator, - Property... properties) { + public static Setting boolSetting(String key, Setting fallbackSetting, + Validator validator, Property... properties) { return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw, b -> parseBoolean(b, key, - isFiltered(properties)), validator, properties); + isFiltered(properties)), + validator, properties); } - public static Setting boolSetting(String key, boolean defaultValue, Validator validator, Property... properties) { - return new Setting<>(key, Boolean.toString(defaultValue), b -> parseBoolean(b, key, isFiltered(properties)), validator, properties); + public static Setting boolSetting(String key, boolean defaultValue, + Validator validator, Property... properties) { + return new Setting<>(key, Boolean.toString(defaultValue), b -> parseBoolean(b, key, isFiltered(properties)), + validator, properties); } public static Setting boolSetting(String key, Function defaultValueFn, Property... properties) { @@ -1690,8 +1848,8 @@ public class Setting implements ToXContentObject { return new Setting<>(key, fallbackSetting, (s) -> TimeValue.parseTimeValue(s, key), properties); } - public static Setting timeSetting(String key, Setting fallBackSetting, Validator validator, - Property... properties) { + public static Setting timeSetting(String key, Setting fallBackSetting, + Validator validator, Property... properties) { return new Setting<>(new SimpleKey(key), fallBackSetting, fallBackSetting::getRaw, (s) -> TimeValue.parseTimeValue(s, key), validator, properties); } @@ -1700,33 +1858,9 @@ public class Setting implements ToXContentObject { return timeSetting(key, defaultValue, TimeValue.timeValueMillis(0), properties); } - public static Setting positiveTimeSetting( - final String key, - final Setting fallbackSetting, - final TimeValue minValue, - final Property... properties) { - return timeSetting(key, fallbackSetting, minValue, properties); - } - - public static Setting doubleSetting(String key, double defaultValue, double minValue, Property... properties) { - return doubleSetting(key, defaultValue, minValue, Double.POSITIVE_INFINITY, properties); - } - - public static Setting doubleSetting(String key, double defaultValue, double minValue, double maxValue, Property... properties) { - return new Setting<>(key, (s) -> Double.toString(defaultValue), (s) -> { - final double d = Double.parseDouble(s); - if (d < minValue) { - String err = "Failed to parse value" + (isFiltered(properties) ? "" : " [" + s + "]") + " for setting [" + key + - "] must be >= " + minValue; - throw new IllegalArgumentException(err); - } - if (d > maxValue) { - String err = "Failed to parse value" + (isFiltered(properties) ? "" : " [" + s + "]") + " for setting [" + key + - "] must be <= " + maxValue; - throw new IllegalArgumentException(err); - } - return d; - }, properties); + public static Setting positiveTimeSetting(final String key, final Setting fallbackSetting, + final Property... properties) { + return timeSetting(key, fallbackSetting, TimeValue.timeValueMillis(0), properties); } @Override diff --git a/server/src/main/java/org/opensearch/script/ScriptService.java b/server/src/main/java/org/opensearch/script/ScriptService.java index 622c23ed76b..af4dae88ed7 100644 --- a/server/src/main/java/org/opensearch/script/ScriptService.java +++ b/server/src/main/java/org/opensearch/script/ScriptService.java @@ -106,8 +106,7 @@ public class ScriptService implements Closeable, ClusterStateApplier { public static final Setting.AffixSetting SCRIPT_CACHE_EXPIRE_SETTING = Setting.affixKeySetting(CONTEXT_PREFIX, "cache_expire", - key -> Setting.positiveTimeSetting(key, SCRIPT_GENERAL_CACHE_EXPIRE_SETTING, TimeValue.timeValueMillis(0), - Property.NodeScope, Property.Dynamic)); + key -> Setting.positiveTimeSetting(key, SCRIPT_GENERAL_CACHE_EXPIRE_SETTING, Property.NodeScope, Property.Dynamic)); // Unlimited compilation rate for context-specific script caches static final String UNLIMITED_COMPILATION_RATE_KEY = "unlimited"; diff --git a/server/src/main/java/org/opensearch/transport/RemoteClusterService.java b/server/src/main/java/org/opensearch/transport/RemoteClusterService.java index e19a8eab391..1b792df8ec4 100644 --- a/server/src/main/java/org/opensearch/transport/RemoteClusterService.java +++ b/server/src/main/java/org/opensearch/transport/RemoteClusterService.java @@ -100,7 +100,6 @@ public final class RemoteClusterService extends RemoteClusterAware implements Cl Setting.positiveTimeSetting( "cluster.remote.initial_connect_timeout", SEARCH_REMOTE_INITIAL_CONNECTION_TIMEOUT_SETTING, // the default needs to be thirty seconds when fallback is removed - TimeValue.timeValueSeconds(30), Setting.Property.NodeScope); public static final Setting SEARCH_REMOTE_NODE_ATTRIBUTE = diff --git a/server/src/test/java/org/opensearch/common/settings/SettingTests.java b/server/src/test/java/org/opensearch/common/settings/SettingTests.java index d104c3d5ef3..a2dc6a51621 100644 --- a/server/src/test/java/org/opensearch/common/settings/SettingTests.java +++ b/server/src/test/java/org/opensearch/common/settings/SettingTests.java @@ -896,7 +896,22 @@ public class SettingTests extends OpenSearchTestCase { assertEquals(fix1.get(Settings.builder().put("abc.qrx", 30).build()), Integer.valueOf(30)); } - public void testMinMaxInt() { + // Integer + + public void testIntWithDefaultValue() { + Setting integerSetting = Setting.intSetting("foo.bar", 42); + assertEquals(integerSetting.get(Settings.EMPTY), Integer.valueOf(42)); + } + + public void testIntWithFallbackValue() { + Setting fallbackSetting = Setting.intSetting("foo.baz", 2); + Setting integerSetting = Setting.intSetting("foo.bar", fallbackSetting); + assertEquals(integerSetting.get(Settings.EMPTY), Integer.valueOf(2)); + assertEquals(integerSetting.get(Settings.builder().put("foo.bar", 3).build()), Integer.valueOf(3)); + assertEquals(integerSetting.get(Settings.builder().put("foo.baz", 3).build()), Integer.valueOf(3)); + } + + public void testIntWithMinMax() { Setting integerSetting = Setting.intSetting("foo.bar", 1, 0, 10, Property.NodeScope); try { integerSetting.get(Settings.builder().put("foo.bar", 11).build()); @@ -916,6 +931,111 @@ public class SettingTests extends OpenSearchTestCase { assertEquals(1, integerSetting.get(Settings.EMPTY).intValue()); } + // Long + + public void testLongWithDefaultValue() { + Setting longSetting = Setting.longSetting("foo.bar", 42); + assertEquals(longSetting.get(Settings.EMPTY), Long.valueOf(42)); + } + + public void testLongWithFallbackValue() { + Setting fallbackSetting = Setting.longSetting("foo.baz", 2); + Setting longSetting = Setting.longSetting("foo.bar", fallbackSetting); + assertEquals(longSetting.get(Settings.EMPTY), Long.valueOf(2)); + assertEquals(longSetting.get(Settings.builder().put("foo.bar", 3).build()), Long.valueOf(3)); + assertEquals(longSetting.get(Settings.builder().put("foo.baz", 3).build()), Long.valueOf(3)); + } + + public void testLongWithMinMax() { + Setting longSetting = Setting.longSetting("foo.bar", 1, 0, 10, Property.NodeScope); + try { + longSetting.get(Settings.builder().put("foo.bar", 11).build()); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals("Failed to parse value [11] for setting [foo.bar] must be <= 10", ex.getMessage()); + } + + try { + longSetting.get(Settings.builder().put("foo.bar", -1).build()); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals("Failed to parse value [-1] for setting [foo.bar] must be >= 0", ex.getMessage()); + } + + assertEquals(5, longSetting.get(Settings.builder().put("foo.bar", 5).build()).longValue()); + assertEquals(1, longSetting.get(Settings.EMPTY).longValue()); + } + + // Float + + public void testFloatWithDefaultValue() { + Setting floatSetting = Setting.floatSetting("foo.bar", (float) 42.1); + assertEquals(floatSetting.get(Settings.EMPTY), Float.valueOf((float) 42.1)); + } + + public void testFloatWithFallbackValue() { + Setting fallbackSetting = Setting.floatSetting("foo.baz", (float) 2.1); + Setting floatSetting = Setting.floatSetting("foo.bar", fallbackSetting); + assertEquals(floatSetting.get(Settings.EMPTY), Float.valueOf((float) 2.1)); + assertEquals(floatSetting.get(Settings.builder().put("foo.bar", 3.2).build()), Float.valueOf((float) 3.2)); + assertEquals(floatSetting.get(Settings.builder().put("foo.baz", 3.2).build()), Float.valueOf((float) 3.2)); + } + + public void testFloatWithMinMax() { + Setting floatSetting = Setting.floatSetting("foo.bar", (float) 1.2, 0, 10, Property.NodeScope); + try { + floatSetting.get(Settings.builder().put("foo.bar", (float) 11.3).build()); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals("Failed to parse value [11.3] for setting [foo.bar] must be <= 10.0", ex.getMessage()); + } + + try { + floatSetting.get(Settings.builder().put("foo.bar", (float) -1.4).build()); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals("Failed to parse value [-1.4] for setting [foo.bar] must be >= 0.0", ex.getMessage()); + } + + assertEquals(5.6, floatSetting.get(Settings.builder().put("foo.bar", (float) 5.6).build()).floatValue(), 0.01); + assertEquals(1.2, floatSetting.get(Settings.EMPTY).floatValue(), 0.01); + } + + // Double + + public void testDoubleWithDefaultValue() { + Setting doubleSetting = Setting.doubleSetting("foo.bar", 42.1); + assertEquals(doubleSetting.get(Settings.EMPTY), Double.valueOf(42.1)); + } + + public void testDoubleWithFallbackValue() { + Setting fallbackSetting = Setting.doubleSetting("foo.baz", 2.1); + Setting doubleSetting = Setting.doubleSetting("foo.bar", fallbackSetting); + assertEquals(doubleSetting.get(Settings.EMPTY), Double.valueOf(2.1)); + assertEquals(doubleSetting.get(Settings.builder().put("foo.bar", 3.2).build()), Double.valueOf(3.2)); + assertEquals(doubleSetting.get(Settings.builder().put("foo.baz", 3.2).build()), Double.valueOf(3.2)); + } + + public void testDoubleWithMinMax() { + Setting doubleSetting = Setting.doubleSetting("foo.bar", 1.2, 0, 10, Property.NodeScope); + try { + doubleSetting.get(Settings.builder().put("foo.bar", 11.3).build()); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals("Failed to parse value [11.3] for setting [foo.bar] must be <= 10.0", ex.getMessage()); + } + + try { + doubleSetting.get(Settings.builder().put("foo.bar", -1.4).build()); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals("Failed to parse value [-1.4] for setting [foo.bar] must be >= 0.0", ex.getMessage()); + } + + assertEquals(5.6, doubleSetting.get(Settings.builder().put("foo.bar", 5.6).build()).doubleValue(), 0.01); + assertEquals(1.2, doubleSetting.get(Settings.EMPTY).doubleValue(), 0.01); + } + /** * Only one single scope can be added to any setting */