Standardize int, long, double and float Setting constructors. (#665)

This commit is contained in:
Daniel Doubrovkine (dB.) 2021-05-06 17:17:11 -04:00 committed by dblock
parent b948315633
commit b1b563dc39
No known key found for this signature in database
GPG Key ID: 75BF031B7C94E183
4 changed files with 367 additions and 115 deletions

View File

@ -1160,62 +1160,259 @@ public class Setting<T> implements ToXContentObject {
return new Setting<>(key, s -> Integer.toString(defaultValue.id), s -> Version.fromId(Integer.parseInt(s)), properties);
}
public static Setting<Float> floatSetting(String key, float defaultValue, Property... properties) {
return new Setting<>(key, (s) -> Float.toString(defaultValue), Float::parseFloat, properties);
}
public static Setting<Float> 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<Integer> 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<Float> with defaultValue
public static Setting<Float> floatSetting(String key, float defaultValue, Property... properties) {
return floatSetting(key, defaultValue, Float.MIN_VALUE, Float.MAX_VALUE, properties);
}
public static Setting<Float> floatSetting(String key, float defaultValue, float minValue, Property... properties) {
return floatSetting(key, defaultValue, minValue, Float.MAX_VALUE, properties);
}
public static Setting<Float> floatSetting(String key, float defaultValue, float minValue, float maxValue, Property... properties) {
return floatSetting(key, defaultValue, minValue, maxValue, v -> {}, properties);
}
public static Setting<Float> floatSetting(String key, float defaultValue, float minValue, float maxValue,
Validator<Float> validator, Property... properties) {
return new Setting<>(key, Float.toString(defaultValue), (s) -> parseFloat(s, minValue, maxValue, key, isFiltered(properties)),
validator, properties);
}
// Setting<Float> with fallback
public static Setting<Float> floatSetting(String key, Setting<Float> fallbackSetting, Property... properties) {
return floatSetting(key, fallbackSetting, Float.MIN_VALUE, Float.MAX_VALUE, properties);
}
public static Setting<Float> floatSetting(String key, Setting<Float> fallbackSetting, float minValue, Property... properties) {
return floatSetting(key, fallbackSetting, minValue, Float.MAX_VALUE, properties);
}
public static Setting<Float> floatSetting(String key, Setting<Float> fallbackSetting, float minValue, float maxValue,
Property... properties) {
return floatSetting(key, fallbackSetting, minValue, maxValue, v -> {}, properties);
}
public static Setting<Float> floatSetting(String key, Setting<Float> fallbackSetting, float minValue, float maxValue,
Validator<Float> 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<Integer> with defaultValue
public static Setting<Integer> intSetting(String key, int defaultValue, Property... properties) {
return intSetting(key, defaultValue, Integer.MIN_VALUE, Integer.MAX_VALUE, properties);
}
public static Setting<Integer> 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<Integer> intSetting(String key, int defaultValue, int minValue, int maxValue, Property... properties) {
return intSetting(key, defaultValue, minValue, maxValue, v -> {}, properties);
}
public static Setting<Integer> intSetting(String key, int defaultValue, int minValue, Validator<Integer> validator,
Property... properties) {
return new Setting<>(key, Integer.toString(defaultValue), (s) -> parseInt(s, minValue, key, isFiltered(properties)), validator,
properties);
public static Setting<Integer> intSetting(String key, int defaultValue, int minValue,
Validator<Integer> validator, Property... properties) {
return intSetting(key, defaultValue, minValue, Integer.MAX_VALUE, validator, properties);
}
public static Setting<Integer> intSetting(String key, int defaultValue, int minValue, int maxValue,
Validator<Integer> validator, Property... properties) {
return new Setting<>(key, Integer.toString(defaultValue), (s) -> parseInt(s, minValue, maxValue, key, isFiltered(properties)),
validator, properties);
}
// Setting<Integer> with fallback
public static Setting<Integer> intSetting(String key, Setting<Integer> fallbackSetting, Property... properties) {
return intSetting(key, fallbackSetting, Integer.MIN_VALUE, Integer.MAX_VALUE, properties);
}
public static Setting<Integer> intSetting(String key, Setting<Integer> 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<Integer> intSetting(String key, Setting<Integer> fallbackSetting, int minValue, int maxValue,
Property... properties) {
return new Setting<>(key, fallbackSetting, (s) -> parseInt(s, minValue, maxValue, key, isFiltered(properties)), properties);
public static Setting<Integer> intSetting(String key, Setting<Integer> fallbackSetting, int minValue, int maxValue,
Property... properties) {
return intSetting(key, fallbackSetting, minValue, maxValue, v -> {}, properties);
}
public static Setting<Integer> intSetting(String key, Setting<Integer> fallbackSetting, int minValue, Validator<Integer> validator,
Property... properties) {
return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw,
(s) -> parseInt(s, minValue, key, isFiltered(properties)),validator, properties);
public static Setting<Integer> intSetting(String key, Setting<Integer> fallbackSetting, int minValue,
Validator<Integer> validator, Property... properties) {
return intSetting(key, fallbackSetting, minValue, Integer.MAX_VALUE, validator, properties);
}
public static Setting<Integer> intSetting(String key, Setting<Integer> fallbackSetting, int minValue, int maxValue,
Validator<Integer> 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<Long> with defaultValue
public static Setting<Long> longSetting(String key, long defaultValue, Property... properties) {
return longSetting(key, defaultValue, Long.MIN_VALUE, Long.MAX_VALUE, properties);
}
public static Setting<Long> 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<Long> longSetting(String key, long defaultValue, long minValue, long maxValue, Property... properties) {
return longSetting(key, defaultValue, minValue, maxValue, v -> {}, properties);
}
public static Setting<Long> longSetting(String key, long defaultValue, long minValue, long maxValue,
Validator<Long> validator, Property... properties) {
return new Setting<>(key, Long.toString(defaultValue), (s) -> parseLong(s, minValue, maxValue, key,
isFiltered(properties)), validator, properties);
}
// Setting<Long> with fallback
public static Setting<Long> longSetting(String key, Setting<Long> fallbackSetting, Property... properties) {
return longSetting(key, fallbackSetting, Long.MIN_VALUE, Long.MAX_VALUE, properties);
}
public static Setting<Long> longSetting(String key, Setting<Long> fallbackSetting, long minValue, Property... properties) {
return longSetting(key, fallbackSetting, minValue, Long.MAX_VALUE, properties);
}
public static Setting<Long> longSetting(String key, Setting<Long> fallbackSetting, long minValue, long maxValue,
Property... properties) {
return longSetting(key, fallbackSetting, minValue, maxValue, v -> {}, properties);
}
public static Setting<Long> longSetting(String key, Setting<Long> fallbackSetting, long minValue,
Validator<Long> validator, Property... properties) {
return longSetting(key, fallbackSetting, minValue, Long.MAX_VALUE, validator, properties);
}
public static Setting<Long> longSetting(String key, Setting<Long> fallbackSetting, long minValue, long maxValue,
Validator<Long> 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<Double> with defaultValue
public static Setting<Double> doubleSetting(String key, double defaultValue, Property... properties) {
return doubleSetting(key, defaultValue, Double.MIN_VALUE, Double.MAX_VALUE, properties);
}
public static Setting<Double> doubleSetting(String key, double defaultValue, double minValue, Property... properties) {
return doubleSetting(key, defaultValue, minValue, Double.MAX_VALUE, properties);
}
public static Setting<Double> doubleSetting(String key, double defaultValue, double minValue, double maxValue, Property... properties) {
return doubleSetting(key, defaultValue, minValue, maxValue, v -> {}, properties);
}
public static Setting<Double> doubleSetting(String key, double defaultValue, double minValue, double maxValue,
Validator<Double> validator, Property... properties) {
return new Setting<>(key, Double.toString(defaultValue), (s) -> parseDouble(s, minValue, maxValue, key, isFiltered(properties)),
validator, properties);
}
// Setting<Double> with fallback
public static Setting<Double> doubleSetting(String key, Setting<Double> fallbackSetting, Property... properties) {
return doubleSetting(key, fallbackSetting, Double.MIN_VALUE, Double.MAX_VALUE, properties);
}
public static Setting<Double> doubleSetting(String key, Setting<Double> fallbackSetting, double minValue, Property... properties) {
return doubleSetting(key, fallbackSetting, minValue, Double.MAX_VALUE, properties);
}
public static Setting<Double> doubleSetting(String key, Setting<Double> fallbackSetting, double minValue, double maxValue,
Property... properties) {
return doubleSetting(key, fallbackSetting, minValue, maxValue, v -> {}, properties);
}
public static Setting<Double> doubleSetting(String key, Setting<Double> fallbackSetting, double minValue, double maxValue,
Validator<Double> 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<String> simpleString(String key, Property... properties) {
return new Setting<>(key, s -> "", Function.identity(), properties);
}
@ -1257,44 +1454,6 @@ public class Setting<T> 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<T> implements ToXContentObject {
return timeValue;
}
public static Setting<Integer> intSetting(String key, int defaultValue, Property... properties) {
return intSetting(key, defaultValue, Integer.MIN_VALUE, properties);
}
public static Setting<Boolean> 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<T> implements ToXContentObject {
return new Setting<>(key, fallbackSetting, b -> parseBoolean(b, key, isFiltered(properties)), properties);
}
public static Setting<Boolean> boolSetting(String key, Setting<Boolean> fallbackSetting, Validator<Boolean> validator,
Property... properties) {
public static Setting<Boolean> boolSetting(String key, Setting<Boolean> fallbackSetting,
Validator<Boolean> 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<Boolean> boolSetting(String key, boolean defaultValue, Validator<Boolean> validator, Property... properties) {
return new Setting<>(key, Boolean.toString(defaultValue), b -> parseBoolean(b, key, isFiltered(properties)), validator, properties);
public static Setting<Boolean> boolSetting(String key, boolean defaultValue,
Validator<Boolean> validator, Property... properties) {
return new Setting<>(key, Boolean.toString(defaultValue), b -> parseBoolean(b, key, isFiltered(properties)),
validator, properties);
}
public static Setting<Boolean> boolSetting(String key, Function<Settings, String> defaultValueFn, Property... properties) {
@ -1690,8 +1848,8 @@ public class Setting<T> implements ToXContentObject {
return new Setting<>(key, fallbackSetting, (s) -> TimeValue.parseTimeValue(s, key), properties);
}
public static Setting<TimeValue> timeSetting(String key, Setting<TimeValue> fallBackSetting, Validator<TimeValue> validator,
Property... properties) {
public static Setting<TimeValue> timeSetting(String key, Setting<TimeValue> fallBackSetting,
Validator<TimeValue> 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<T> implements ToXContentObject {
return timeSetting(key, defaultValue, TimeValue.timeValueMillis(0), properties);
}
public static Setting<TimeValue> positiveTimeSetting(
final String key,
final Setting<TimeValue> fallbackSetting,
final TimeValue minValue,
final Property... properties) {
return timeSetting(key, fallbackSetting, minValue, properties);
}
public static Setting<Double> doubleSetting(String key, double defaultValue, double minValue, Property... properties) {
return doubleSetting(key, defaultValue, minValue, Double.POSITIVE_INFINITY, properties);
}
public static Setting<Double> 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<TimeValue> positiveTimeSetting(final String key, final Setting<TimeValue> fallbackSetting,
final Property... properties) {
return timeSetting(key, fallbackSetting, TimeValue.timeValueMillis(0), properties);
}
@Override

View File

@ -106,8 +106,7 @@ public class ScriptService implements Closeable, ClusterStateApplier {
public static final Setting.AffixSetting<TimeValue> 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";

View File

@ -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<String> SEARCH_REMOTE_NODE_ATTRIBUTE =

View File

@ -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<Integer> integerSetting = Setting.intSetting("foo.bar", 42);
assertEquals(integerSetting.get(Settings.EMPTY), Integer.valueOf(42));
}
public void testIntWithFallbackValue() {
Setting<Integer> fallbackSetting = Setting.intSetting("foo.baz", 2);
Setting<Integer> 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<Integer> 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<Long> longSetting = Setting.longSetting("foo.bar", 42);
assertEquals(longSetting.get(Settings.EMPTY), Long.valueOf(42));
}
public void testLongWithFallbackValue() {
Setting<Long> fallbackSetting = Setting.longSetting("foo.baz", 2);
Setting<Long> 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<Long> 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<Float> floatSetting = Setting.floatSetting("foo.bar", (float) 42.1);
assertEquals(floatSetting.get(Settings.EMPTY), Float.valueOf((float) 42.1));
}
public void testFloatWithFallbackValue() {
Setting<Float> fallbackSetting = Setting.floatSetting("foo.baz", (float) 2.1);
Setting<Float> 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<Float> 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<Double> doubleSetting = Setting.doubleSetting("foo.bar", 42.1);
assertEquals(doubleSetting.get(Settings.EMPTY), Double.valueOf(42.1));
}
public void testDoubleWithFallbackValue() {
Setting<Double> fallbackSetting = Setting.doubleSetting("foo.baz", 2.1);
Setting<Double> 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<Double> 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
*/