Skip to content

Commit

Permalink
allow to specify a list of settings to get a value for
Browse files Browse the repository at this point in the history
  • Loading branch information
kimchy committed Mar 4, 2013
1 parent a8d52b5 commit d2dc672
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@
*/
public class ImmutableSettings implements Settings {

private ImmutableMap<String, String> settings;
public static final Settings EMPTY = new Builder().build();

private ImmutableMap<String, String> settings;
private transient ClassLoader classLoader;

private ImmutableSettings(Map<String, String> settings, ClassLoader classLoader) {
ImmutableSettings(Map<String, String> settings, ClassLoader classLoader) {
this.settings = ImmutableMap.copyOf(settings);
this.classLoader = classLoader;
}
Expand Down Expand Up @@ -124,9 +125,30 @@ public String get(String setting) {
return settings.get(toCamelCase(setting));
}

@Override
public String get(String[] settings) {
for (String setting : settings) {
String retVal = this.settings.get(setting);
if (retVal != null) {
return retVal;
}
retVal = this.settings.get(toCamelCase(setting));
if (retVal != null) {
return retVal;
}
}
return null;
}

@Override
public String get(String setting, String defaultValue) {
String retVal = settings.get(setting);
String retVal = get(setting);
return retVal == null ? defaultValue : retVal;
}

@Override
public String get(String[] settings, String defaultValue) {
String retVal = get(settings);
return retVal == null ? defaultValue : retVal;
}

Expand All @@ -143,6 +165,19 @@ public Float getAsFloat(String setting, Float defaultValue) {
}
}

@Override
public Float getAsFloat(String[] settings, Float defaultValue) throws SettingsException {
String sValue = get(settings);
if (sValue == null) {
return defaultValue;
}
try {
return Float.parseFloat(sValue);
} catch (NumberFormatException e) {
throw new SettingsException("Failed to parse float setting [" + Arrays.toString(settings) + "] with value [" + sValue + "]", e);
}
}

@Override
public Double getAsDouble(String setting, Double defaultValue) {
String sValue = get(setting);
Expand All @@ -156,6 +191,20 @@ public Double getAsDouble(String setting, Double defaultValue) {
}
}

@Override
public Double getAsDouble(String[] settings, Double defaultValue) {
String sValue = get(settings);
if (sValue == null) {
return defaultValue;
}
try {
return Double.parseDouble(sValue);
} catch (NumberFormatException e) {
throw new SettingsException("Failed to parse double setting [" + Arrays.toString(settings) + "] with value [" + sValue + "]", e);
}
}


@Override
public Integer getAsInt(String setting, Integer defaultValue) {
String sValue = get(setting);
Expand All @@ -169,6 +218,19 @@ public Integer getAsInt(String setting, Integer defaultValue) {
}
}

@Override
public Integer getAsInt(String[] settings, Integer defaultValue) {
String sValue = get(settings);
if (sValue == null) {
return defaultValue;
}
try {
return Integer.parseInt(sValue);
} catch (NumberFormatException e) {
throw new SettingsException("Failed to parse int setting [" + Arrays.toString(settings) + "] with value [" + sValue + "]", e);
}
}

@Override
public Long getAsLong(String setting, Long defaultValue) {
String sValue = get(setting);
Expand All @@ -182,26 +244,59 @@ public Long getAsLong(String setting, Long defaultValue) {
}
}

@Override
public Long getAsLong(String[] settings, Long defaultValue) {
String sValue = get(settings);
if (sValue == null) {
return defaultValue;
}
try {
return Long.parseLong(sValue);
} catch (NumberFormatException e) {
throw new SettingsException("Failed to parse long setting [" + Arrays.toString(settings) + "] with value [" + sValue + "]", e);
}
}

@Override
public Boolean getAsBoolean(String setting, Boolean defaultValue) {
return Booleans.parseBoolean(get(setting), defaultValue);
}

@Override
public Boolean getAsBoolean(String[] settings, Boolean defaultValue) {
return Booleans.parseBoolean(get(settings), defaultValue);
}

@Override
public TimeValue getAsTime(String setting, TimeValue defaultValue) {
return parseTimeValue(get(setting), defaultValue);
}

@Override
public TimeValue getAsTime(String[] settings, TimeValue defaultValue) {
return parseTimeValue(get(settings), defaultValue);
}

@Override
public ByteSizeValue getAsBytesSize(String setting, ByteSizeValue defaultValue) throws SettingsException {
return parseBytesSizeValue(get(setting), defaultValue);
}

@Override
public ByteSizeValue getAsBytesSize(String[] settings, ByteSizeValue defaultValue) throws SettingsException {
return parseBytesSizeValue(get(settings), defaultValue);
}

@Override
public SizeValue getAsSize(String setting, SizeValue defaultValue) throws SettingsException {
return parseSizeValue(get(setting), defaultValue);
}

@Override
public SizeValue getAsSize(String[] settings, SizeValue defaultValue) throws SettingsException {
return parseSizeValue(get(settings), defaultValue);
}

@SuppressWarnings({"unchecked"})
@Override
public <T> Class<? extends T> getAsClass(String setting, Class<? extends T> defaultClazz) throws NoClassSettingsException {
Expand Down
106 changes: 59 additions & 47 deletions src/main/java/org/elasticsearch/common/settings/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,23 @@ public interface Settings {
*/
String get(String setting);

/**
* Returns the setting value associated with the first setting key.
*/
String get(String[] settings);

/**
* Returns the setting value associated with the setting key. If it does not exists,
* returns the default value provided.
*
* @param setting The setting key
* @param defaultValue The value to return if no value is associated with the setting
* @return The setting value, or the default value if no value exists
*/
String get(String setting, String defaultValue);

/**
* Returns the setting value associated with the first setting key, if none exists,
* returns the default value provided.
*/
String get(String[] settings, String defaultValue);

/**
* Returns group settings for the given setting prefix.
*/
Expand All @@ -99,94 +106,99 @@ public interface Settings {
/**
* Returns the setting value (as float) associated with the setting key. If it does not exists,
* returns the default value provided.
*
* @param setting The setting key
* @param defaultValue The value to return if no value is associated with the setting
* @return The (float) value, or the default value if no value exists.
* @throws SettingsException Failure to parse the setting
*/
Float getAsFloat(String setting, Float defaultValue) throws SettingsException;

/**
* Returns the setting value (as float) associated with teh first setting key, if none
* exists, returns the default value provided.
*/
Float getAsFloat(String[] settings, Float defaultValue) throws SettingsException;

/**
* Returns the setting value (as double) associated with the setting key. If it does not exists,
* returns the default value provided.
*
* @param setting The setting key
* @param defaultValue The value to return if no value is associated with the setting
* @return The (double) value, or the default value if no value exists.
* @throws SettingsException Failure to parse the setting
*/
Double getAsDouble(String setting, Double defaultValue) throws SettingsException;

/**
* Returns the setting value (as double) associated with teh first setting key, if none
* exists, returns the default value provided.
*/
Double getAsDouble(String[] settings, Double defaultValue) throws SettingsException;

/**
* Returns the setting value (as int) associated with the setting key. If it does not exists,
* returns the default value provided.
*
* @param setting The setting key
* @param defaultValue The value to return if no value is associated with the setting
* @return The (int) value, or the default value if no value exists.
* @throws SettingsException Failure to parse the setting
*/
Integer getAsInt(String setting, Integer defaultValue) throws SettingsException;

/**
* Returns the setting value (as int) associated with the first setting key. If it does not exists,
* returns the default value provided.
*/
Integer getAsInt(String[] settings, Integer defaultValue) throws SettingsException;

/**
* Returns the setting value (as long) associated with the setting key. If it does not exists,
* returns the default value provided.
*
* @param setting The setting key
* @param defaultValue The value to return if no value is associated with the setting
* @return The (long) value, or the default value if no value exists.
* @throws SettingsException Failure to parse the setting
*/
Long getAsLong(String setting, Long defaultValue) throws SettingsException;

/**
* Returns the setting value (as long) associated with the setting key. If it does not exists,
* returns the default value provided.
*/
Long getAsLong(String[] settings, Long defaultValue) throws SettingsException;

/**
* Returns the setting value (as boolean) associated with the setting key. If it does not exists,
* returns the default value provided.
*
* @param setting The setting key
* @param defaultValue The value to return if no value is associated with the setting
* @return The (boolean) value, or the default value if no value exists.
* @throws SettingsException Failure to parse the setting
*/
Boolean getAsBoolean(String setting, Boolean defaultValue) throws SettingsException;

/**
* Returns the setting value (as boolean) associated with the setting key. If it does not exists,
* returns the default value provided.
*/
Boolean getAsBoolean(String[] settings, Boolean defaultValue) throws SettingsException;

/**
* Returns the setting value (as time) associated with the setting key. If it does not exists,
* returns the default value provided.
*
* @param setting The setting key
* @param defaultValue The value to return if no value is associated with the setting
* @return The (time) value, or the default value if no value exists.
* @throws SettingsException Failure to parse the setting
* @see TimeValue#parseTimeValue(String, org.elasticsearch.common.unit.TimeValue)
*/
TimeValue getAsTime(String setting, TimeValue defaultValue) throws SettingsException;

/**
* Returns the setting value (as time) associated with the setting key. If it does not exists,
* returns the default value provided.
*/
TimeValue getAsTime(String[] settings, TimeValue defaultValue) throws SettingsException;

/**
* Returns the setting value (as size) associated with the setting key. If it does not exists,
* returns the default value provided.
*
* @param setting The setting key
* @param defaultValue The value to return if no value is associated with the setting
* @return The (size) value, or the default value if no value exists.
* @throws SettingsException Failure to parse the setting
* @see org.elasticsearch.common.unit.ByteSizeValue#parseBytesSizeValue(String, org.elasticsearch.common.unit.ByteSizeValue)
*/
ByteSizeValue getAsBytesSize(String setting, ByteSizeValue defaultValue) throws SettingsException;

/**
* Returns the setting value (as size) associated with the setting key. If it does not exists,
* returns the default value provided.
*
* @param setting The setting key
* @param defaultValue The value to return if no value is associated with the setting
* @return The (size) value, or the default value if no value exists.
* @throws SettingsException Failure to parse the setting
* @see org.elasticsearch.common.unit.ByteSizeValue#parseBytesSizeValue(String, org.elasticsearch.common.unit.ByteSizeValue)
*/
ByteSizeValue getAsBytesSize(String[] settings, ByteSizeValue defaultValue) throws SettingsException;

/**
* Returns the setting value (as size) associated with the setting key. If it does not exists,
* returns the default value provided.
*/
SizeValue getAsSize(String setting, SizeValue defaultValue) throws SettingsException;

/**
* Returns the setting value (as size) associated with the setting key. If it does not exists,
* returns the default value provided.
*/
SizeValue getAsSize(String[] settings, SizeValue defaultValue) throws SettingsException;

/**
* Returns the setting value (as a class) associated with the setting key. If it does not exists,
* returns the default class provided.
Expand Down

0 comments on commit d2dc672

Please sign in to comment.