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 '.')
This commit is contained in:
uboness 2014-07-12 16:43:35 +02:00
parent f392a99bd9
commit 04b412b597
3 changed files with 64 additions and 0 deletions

View File

@ -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<String> names() {
Set<String> 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();

View File

@ -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<String> names();
/**
* Returns the settings as delimited string.
*/

View File

@ -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.<String, Object>hasEntry("foo.bar", "def"),
Matchers.<String, Object>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<String> 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"));
}
}