Simplify + Cleanup Dead Code in Settings (#37341)

* Remove dead code
* Simplify some overly complex code, this class is long enough already
This commit is contained in:
Armin Braun 2019-01-16 13:57:16 +01:00 committed by GitHub
parent 687978b7d1
commit 21a88d5505
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 92 deletions

View File

@ -34,8 +34,6 @@ import org.elasticsearch.common.logging.LogConfigurator;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.MemorySizeValue;
import org.elasticsearch.common.unit.RatioValue;
import org.elasticsearch.common.unit.SizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
@ -64,6 +62,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import java.util.ListIterator;
@ -75,7 +74,6 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.elasticsearch.common.unit.ByteSizeValue.parseBytesSizeValue;
import static org.elasticsearch.common.unit.SizeValue.parseSizeValue;
import static org.elasticsearch.common.unit.TimeValue.parseTimeValue;
/**
@ -100,7 +98,7 @@ public final class Settings implements ToXContentFragment {
*/
private final SetOnce<Set<String>> keys = new SetOnce<>();
Settings(Map<String, Object> settings, SecureSettings secureSettings) {
private Settings(Map<String, Object> settings, SecureSettings secureSettings) {
// we use a sorted map for consistent serialization when using getAsMap()
this.settings = Collections.unmodifiableSortedMap(new TreeMap<>(settings));
this.secureSettings = secureSettings;
@ -245,30 +243,6 @@ public final class Settings implements ToXContentFragment {
return retVal == null ? defaultValue : retVal;
}
/**
* Returns the setting value associated with the setting key. If it does not exists,
* returns the default value provided.
*/
String get(String setting, String defaultValue, boolean isList) {
Object value = settings.get(setting);
if (value != null) {
if (value instanceof List) {
if (isList == false) {
throw new IllegalArgumentException(
"Found list type value for setting [" + setting + "] but but did not expect a list for it."
);
}
} else if (isList) {
throw new IllegalArgumentException(
"Expected list type value for setting [" + setting + "] but found [" + value.getClass() + ']'
);
}
return toString(value);
} else {
return defaultValue;
}
}
/**
* Returns the setting value (as float) associated with the setting key. If it does not exists,
* returns the default value provided.
@ -382,23 +356,6 @@ public final class Settings implements ToXContentFragment {
return MemorySizeValue.parseBytesSizeValueOrHeapRatio(get(setting, defaultValue), setting);
}
/**
* Returns the setting value (as a RatioValue) associated with the setting key. Provided values can
* either be a percentage value (eg. 23%), or expressed as a floating point number (eg. 0.23). If
* it does not exist, parses the default value provided.
*/
public RatioValue getAsRatio(String setting, String defaultValue) throws SettingsException {
return RatioValue.parseRatioValue(get(setting, defaultValue));
}
/**
* Returns the setting value (as size) associated with the setting key. If it does not exists,
* returns the default value provided.
*/
public SizeValue getAsSize(String setting, SizeValue defaultValue) throws SettingsException {
return parseSizeValue(get(setting), defaultValue);
}
/**
* The values associated with a setting key as an immutable list.
* <p>
@ -503,11 +460,7 @@ public final class Settings implements ToXContentFragment {
* Returns group settings for the given setting prefix.
*/
public Map<String, Settings> getAsGroups() throws SettingsException {
return getAsGroups(false);
}
public Map<String, Settings> getAsGroups(boolean ignoreNonGrouped) throws SettingsException {
return getGroupsInternal("", ignoreNonGrouped);
return getGroupsInternal("", false);
}
/**
@ -566,14 +519,12 @@ public final class Settings implements ToXContentFragment {
if (o == null || getClass() != o.getClass()) return false;
Settings that = (Settings) o;
if (settings != null ? !settings.equals(that.settings) : that.settings != null) return false;
return true;
return Objects.equals(settings, that.settings);
}
@Override
public int hashCode() {
int result = settings != null ? settings.hashCode() : 0;
return result;
return settings != null ? settings.hashCode() : 0;
}
public static Settings readSettingsFromStream(StreamInput in) throws IOException {
@ -791,7 +742,7 @@ public final class Settings implements ToXContentFragment {
// we use a sorted map for consistent serialization when using getAsMap()
private final Map<String, Object> map = new TreeMap<>();
private SetOnce<SecureSettings> secureSettings = new SetOnce<>();
private final SetOnce<SecureSettings> secureSettings = new SetOnce<>();
private Builder() {
@ -935,18 +886,6 @@ public final class Settings implements ToXContentFragment {
return put(key, (String) null);
}
/**
* Sets a setting with the provided setting key and class as value.
*
* @param key The setting key
* @param clazz The setting class value
* @return The builder
*/
public Builder put(String key, Class clazz) {
map.put(key, clazz.getName());
return this;
}
/**
* Sets the setting with the provided setting key and the boolean value.
*
@ -1061,22 +1000,6 @@ public final class Settings implements ToXContentFragment {
return this;
}
/**
* Sets the setting group.
*/
public Builder put(String settingPrefix, String groupName, String[] settings, String[] values) throws SettingsException {
if (settings.length != values.length) {
throw new SettingsException("The settings length must match the value length");
}
for (int i = 0; i < settings.length; i++) {
if (values[i] == null) {
continue;
}
put(settingPrefix + "." + groupName + "." + settings[i], values[i]);
}
return this;
}
/**
* Sets all the provided settings including secure settings
*/
@ -1210,18 +1133,12 @@ public final class Settings implements ToXContentFragment {
@Override
public boolean shouldIgnoreMissing(String placeholderName) {
if (placeholderName.startsWith("prompt.")) {
return true;
}
return false;
return placeholderName.startsWith("prompt.");
}
@Override
public boolean shouldRemoveMissingPlaceholder(String placeholderName) {
if (placeholderName.startsWith("prompt.")) {
return false;
}
return true;
return !placeholderName.startsWith("prompt.");
}
};
@ -1395,7 +1312,7 @@ public final class Settings implements ToXContentFragment {
@Override
public int size() {
if (size == -1) {
size = Math.toIntExact(delegate.keySet().stream().filter((e) -> filter.test(e)).count());
size = Math.toIntExact(delegate.keySet().stream().filter(filter).count());
}
return size;
}