Added extra setters with null and empty checks.

This commit is contained in:
Anthony Whitford
2015-10-31 10:25:50 -07:00
parent 0cbecbe3a0
commit e5744dd63f
2 changed files with 74 additions and 0 deletions

View File

@@ -444,6 +444,30 @@ public final class Settings {
LOGGER.debug("Setting: {}='{}'", key, value);
}
/**
* Sets a property value only if the value is not null.
*
* @param key the key for the property
* @param value the value for the property
*/
public static void setStringIfNotNull(String key, String value) {
if (null != value) {
setString(key, value);
}
}
/**
* Sets a property value only if the value is not null and not empty.
*
* @param key the key for the property
* @param value the value for the property
*/
public static void setStringIfNotEmpty(String key, String value) {
if (null != value && !value.isEmpty()) {
setString(key, value);
}
}
/**
* Sets a property value.
*
@@ -454,6 +478,18 @@ public final class Settings {
setString(key, Boolean.toString(value));
}
/**
* Sets a property value only if the value is not null.
*
* @param key the key for the property
* @param value the value for the property
*/
public static void setBooleanIfNotNull(String key, Boolean value) {
if (null != value) {
setBoolean(key, value);
}
}
/**
* Sets a property value.
*
@@ -465,6 +501,18 @@ public final class Settings {
LOGGER.debug("Setting: {}='{}'", key, value);
}
/**
* Sets a property value only if the value is not null.
*
* @param key the key for the property
* @param value the value for the property
*/
public static void setIntIfNotNull(String key, Integer value) {
if (null != value) {
setInt(key, value);
}
}
/**
* Merges a new properties file into the current properties. This method allows for the loading of a user provided properties
* file.<br/><br/>

View File

@@ -77,6 +77,32 @@ public class SettingsTest extends BaseTest {
Assert.assertEquals(expResults, value);
}
/**
* Test of setStringIfNotNull method, of class Settings.
*/
@Test
public void testSetStringIfNotNull() {
String key = "nullableProperty";
String value = "someValue";
Settings.setString(key, value);
Settings.setStringIfNotNull(key, null); // NO-OP
String expResults = Settings.getString(key);
Assert.assertEquals(expResults, value);
}
/**
* Test of setStringIfNotNull method, of class Settings.
*/
@Test
public void testSetStringIfNotEmpty() {
String key = "optionalProperty";
String value = "someValue";
Settings.setString(key, value);
Settings.setStringIfNotEmpty(key, ""); // NO-OP
String expResults = Settings.getString(key);
Assert.assertEquals(expResults, value);
}
/**
* Test of getString method, of class Settings.
*/