From 04b412b5972a08a79989d82cc9d17842fe3c3e5a Mon Sep 17 00:00:00 2001 From: uboness Date: Sat, 12 Jul 2014 16:43:35 +0200 Subject: [PATCH] Added more utility methods to Settings - names() to return the direct settings names - getAsSettings(String) to return the settings mapped to the given name (like getByPrefix(...) except no need to provide a tailing '.') --- .../common/settings/ImmutableSettings.java | 19 +++++++++++ .../common/settings/Settings.java | 11 ++++++ .../settings/ImmutableSettingsTests.java | 34 +++++++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/src/main/java/org/elasticsearch/common/settings/ImmutableSettings.java b/src/main/java/org/elasticsearch/common/settings/ImmutableSettings.java index 09bb3a06537..28dbaf952b8 100644 --- a/src/main/java/org/elasticsearch/common/settings/ImmutableSettings.java +++ b/src/main/java/org/elasticsearch/common/settings/ImmutableSettings.java @@ -218,6 +218,11 @@ public class ImmutableSettings implements Settings { return builder.build(); } + @Override + public Settings getAsSettings(String setting) { + return getByPrefix(setting + "."); + } + @Override public String get(String setting) { String retVal = settings.get(setting); @@ -563,6 +568,20 @@ public class ImmutableSettings implements Settings { } } + @Override + public Set names() { + Set names = new HashSet<>(); + for (String key : settings.keySet()) { + int i = key.indexOf("."); + if (i < 0) { + names.add(key); + } else { + names.add(key.substring(0, i)); + } + } + return names; + } + @Override public String toDelimitedString(char delimiter) { StringBuilder sb = new StringBuilder(); diff --git a/src/main/java/org/elasticsearch/common/settings/Settings.java b/src/main/java/org/elasticsearch/common/settings/Settings.java index aed56be3038..946c2976e6d 100644 --- a/src/main/java/org/elasticsearch/common/settings/Settings.java +++ b/src/main/java/org/elasticsearch/common/settings/Settings.java @@ -29,6 +29,7 @@ import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.ToXContent; import java.util.Map; +import java.util.Set; /** * Immutable settings allowing to control the configuration. @@ -58,6 +59,11 @@ public interface Settings extends ToXContent { */ Settings getByPrefix(String prefix); + /** + * Returns the settings mapped to the given setting name. + */ + Settings getAsSettings(String setting); + /** * The class loader associated with this settings, or {@link org.elasticsearch.common.Classes#getDefaultClassLoader()} * if not set. @@ -312,6 +318,11 @@ public interface Settings extends ToXContent { */ Version getAsVersion(String setting, Version defaultVersion) throws SettingsException; + /** + * @return The direct keys of this settings + */ + Set names(); + /** * Returns the settings as delimited string. */ diff --git a/src/test/java/org/elasticsearch/common/settings/ImmutableSettingsTests.java b/src/test/java/org/elasticsearch/common/settings/ImmutableSettingsTests.java index aebc7b89e0c..0859179c7c8 100644 --- a/src/test/java/org/elasticsearch/common/settings/ImmutableSettingsTests.java +++ b/src/test/java/org/elasticsearch/common/settings/ImmutableSettingsTests.java @@ -27,6 +27,7 @@ import org.junit.Test; import java.util.List; import java.util.Map; +import java.util.Set; import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; import static org.hamcrest.Matchers.*; @@ -184,4 +185,37 @@ public class ImmutableSettingsTests extends ElasticsearchTestCase { Matchers.hasEntry("foo.bar", "def"), Matchers.hasEntry("foo.baz", "ghi"))); } + + @Test + public void testGetAsSettings() { + Settings settings = settingsBuilder() + .put("foo", "abc") + .put("foo.bar", "def") + .put("foo.baz", "ghi").build(); + + Settings fooSettings = settings.getAsSettings("foo"); + assertThat(fooSettings.get("bar"), equalTo("def")); + assertThat(fooSettings.get("baz"), equalTo("ghi")); + } + + @Test + public void testNames() { + Settings settings = settingsBuilder() + .put("bar", "baz") + .put("foo", "abc") + .put("foo.bar", "def") + .put("foo.baz", "ghi").build(); + + Set names = settings.names(); + assertThat(names.size(), equalTo(2)); + assertTrue(names.contains("bar")); + assertTrue(names.contains("foo")); + + Settings fooSettings = settings.getAsSettings("foo"); + names = fooSettings.names(); + assertThat(names.size(), equalTo(2)); + assertTrue(names.contains("bar")); + assertTrue(names.contains("baz")); + + } }