allow to load settings from delimited string

This commit is contained in:
Shay Banon 2013-01-24 12:00:03 +01:00
parent 88f68264c7
commit b143822bac
2 changed files with 30 additions and 0 deletions

View File

@ -21,6 +21,7 @@ package org.elasticsearch.common.settings;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.Version;
import org.elasticsearch.common.Booleans;
import org.elasticsearch.common.Classes;
@ -589,6 +590,18 @@ public class ImmutableSettings implements Settings {
return this;
}
public Builder loadFromDelimitedString(String value, char delimiter) {
String[] values = Strings.splitStringToArray(value, delimiter);
for (String s : values) {
int index = s.indexOf('=');
if (index == -1) {
throw new ElasticSearchIllegalArgumentException("value [" + s + "] for settings loaded with delimiter [" + delimiter + "] is malformed, missing =");
}
map.put(s.substring(0, index), s.substring(index + 1));
}
return this;
}
/**
* Loads settings from the actual string content that represents them using the
* {@link SettingsLoaderFactory#loaderFromSource(String)}.

View File

@ -62,4 +62,21 @@ public class ImmutableSettingsTests {
}
@Test
public void testLoadFromDelimitedString() {
Settings settings = settingsBuilder()
.loadFromDelimitedString("key1=value1;key2=value2", ';')
.build();
assertThat(settings.get("key1"), equalTo("value1"));
assertThat(settings.get("key2"), equalTo("value2"));
assertThat(settings.getAsMap().size(), equalTo(2));
settings = settingsBuilder()
.loadFromDelimitedString("key1=value1;key2=value2;", ';')
.build();
assertThat(settings.get("key1"), equalTo("value1"));
assertThat(settings.get("key2"), equalTo("value2"));
assertThat(settings.getAsMap().size(), equalTo(2));
}
}