浏览代码

Simplify + Cleanup Dead Code in Settings (#37341)

* Remove dead code
* Simplify some overly complex code, this class is long enough already
Armin Braun 6 年之前
父节点
当前提交
21a88d5505
共有 1 个文件被更改,包括 9 次插入92 次删除
  1. 9 92
      server/src/main/java/org/elasticsearch/common/settings/Settings.java

+ 9 - 92
server/src/main/java/org/elasticsearch/common/settings/Settings.java

@@ -34,8 +34,6 @@ import org.elasticsearch.common.logging.LogConfigurator;
 import org.elasticsearch.common.unit.ByteSizeUnit;
 import org.elasticsearch.common.unit.ByteSizeUnit;
 import org.elasticsearch.common.unit.ByteSizeValue;
 import org.elasticsearch.common.unit.ByteSizeValue;
 import org.elasticsearch.common.unit.MemorySizeValue;
 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.unit.TimeValue;
 import org.elasticsearch.common.xcontent.DeprecationHandler;
 import org.elasticsearch.common.xcontent.DeprecationHandler;
 import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
 import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
@@ -64,6 +62,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.List;
 import java.util.Map;
 import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.NoSuchElementException;
+import java.util.Objects;
 import java.util.Set;
 import java.util.Set;
 import java.util.TreeMap;
 import java.util.TreeMap;
 import java.util.ListIterator;
 import java.util.ListIterator;
@@ -75,7 +74,6 @@ import java.util.stream.Collectors;
 import java.util.stream.Stream;
 import java.util.stream.Stream;
 
 
 import static org.elasticsearch.common.unit.ByteSizeValue.parseBytesSizeValue;
 import static org.elasticsearch.common.unit.ByteSizeValue.parseBytesSizeValue;
-import static org.elasticsearch.common.unit.SizeValue.parseSizeValue;
 import static org.elasticsearch.common.unit.TimeValue.parseTimeValue;
 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<>();
     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()
         // we use a sorted map for consistent serialization when using getAsMap()
         this.settings = Collections.unmodifiableSortedMap(new TreeMap<>(settings));
         this.settings = Collections.unmodifiableSortedMap(new TreeMap<>(settings));
         this.secureSettings = secureSettings;
         this.secureSettings = secureSettings;
@@ -245,30 +243,6 @@ public final class Settings implements ToXContentFragment {
         return retVal == null ? defaultValue : retVal;
         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 setting value (as float) associated with the setting key. If it does not exists,
      * returns the default value provided.
      * returns the default value provided.
@@ -382,23 +356,6 @@ public final class Settings implements ToXContentFragment {
         return MemorySizeValue.parseBytesSizeValueOrHeapRatio(get(setting, defaultValue), setting);
         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.
      * The values associated with a setting key as an immutable list.
      * <p>
      * <p>
@@ -503,11 +460,7 @@ public final class Settings implements ToXContentFragment {
      * Returns group settings for the given setting prefix.
      * Returns group settings for the given setting prefix.
      */
      */
     public Map<String, Settings> getAsGroups() throws SettingsException {
     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;
         if (o == null || getClass() != o.getClass()) return false;
 
 
         Settings that = (Settings) o;
         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
     @Override
     public int hashCode() {
     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 {
     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()
         // we use a sorted map for consistent serialization when using getAsMap()
         private final Map<String, Object> map = new TreeMap<>();
         private final Map<String, Object> map = new TreeMap<>();
 
 
-        private SetOnce<SecureSettings> secureSettings = new SetOnce<>();
+        private final SetOnce<SecureSettings> secureSettings = new SetOnce<>();
 
 
         private Builder() {
         private Builder() {
 
 
@@ -935,18 +886,6 @@ public final class Settings implements ToXContentFragment {
             return put(key, (String) null);
             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.
          * Sets the setting with the provided setting key and the boolean value.
          *
          *
@@ -1061,22 +1000,6 @@ public final class Settings implements ToXContentFragment {
             return this;
             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
          * Sets all the provided settings including secure settings
          */
          */
@@ -1210,18 +1133,12 @@ public final class Settings implements ToXContentFragment {
 
 
                 @Override
                 @Override
                 public boolean shouldIgnoreMissing(String placeholderName) {
                 public boolean shouldIgnoreMissing(String placeholderName) {
-                    if (placeholderName.startsWith("prompt.")) {
-                        return true;
-                    }
-                    return false;
+                    return placeholderName.startsWith("prompt.");
                 }
                 }
 
 
                 @Override
                 @Override
                 public boolean shouldRemoveMissingPlaceholder(String placeholderName) {
                 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
         @Override
         public int size() {
         public int size() {
             if (size == -1) {
             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;
             return size;
         }
         }