From 2f8c1e994bc035ee2f1112924e4e37f85b485b65 Mon Sep 17 00:00:00 2001 From: John Blum Date: Fri, 9 Sep 2016 13:39:15 -0700 Subject: [PATCH] SGF-515 - Allow a property to be unset. --- .../data/gemfire/util/PropertiesBuilder.java | 35 +++++++++++++++++++ .../gemfire/util/PropertiesBuilderTests.java | 10 ++++++ 2 files changed, 45 insertions(+) diff --git a/src/main/java/org/springframework/data/gemfire/util/PropertiesBuilder.java b/src/main/java/org/springframework/data/gemfire/util/PropertiesBuilder.java index 1676e6b7..7683e19f 100644 --- a/src/main/java/org/springframework/data/gemfire/util/PropertiesBuilder.java +++ b/src/main/java/org/springframework/data/gemfire/util/PropertiesBuilder.java @@ -212,6 +212,16 @@ public class PropertiesBuilder implements FactoryBean { return (value != null ? setProperty(name, value.toString()) : this); } + /** + * Sets the named property to the given array of object values. The property is only set + * if the array of object value is not null or empty. + * + * @param name name of the property to set. + * @param values array of object values used as the property's value. + * @return a reference to this {@link PropertiesBuilder} + * @see org.springframework.util.StringUtils#arrayToCommaDelimitedString(Object[]) + * @see #setProperty(String, String) + */ public PropertiesBuilder setProperty(String name, Object[] values) { return (!ObjectUtils.isEmpty(values) ? setProperty(name, StringUtils.arrayToCommaDelimitedString(values)) : this); @@ -238,10 +248,35 @@ public class PropertiesBuilder implements FactoryBean { return this; } + /** + * Sets the named property to the given {@literal value} if the {@literal defaultValue} is not null + * and {@literal value} is not equal to {@literal defaultValue}. + * + * @param Class type of the property value. + * @param name name of the property to set. + * @param value value to set for the property. + * @param defaultValue default value of the property to compare with the given value + * when determining whether to set the property. + * @return a reference to this {@link PropertiesBuilder}. + * @see #setProperty(String, Object) + */ public PropertiesBuilder setPropertyIfNotDefault(String name, Object value, T defaultValue) { return (defaultValue == null || !defaultValue.equals(value) ? setProperty(name, value) : this); } + /** + * Un-sets the named property. This method sets the given named property to an empty {@link String}. + * + * @param name name of the property to unset. + * @return a reference to this {@link PropertiesBuilder}. + * @throws IllegalArgumentException if the property name is not specified. + */ + public PropertiesBuilder unsetProperty(String name) { + Assert.hasText(name, String.format("Name [%s] mut be specified", name)); + this.properties.setProperty(name, ""); + return this; + } + /** * Determine whether the given {@link String} value is a valid {@link Properties} value. A property value is * considered valid if it is not null, not empty and not equal to (case-insensitive) {@link String} literal diff --git a/src/test/java/org/springframework/data/gemfire/util/PropertiesBuilderTests.java b/src/test/java/org/springframework/data/gemfire/util/PropertiesBuilderTests.java index 047422ce..8a920c69 100644 --- a/src/test/java/org/springframework/data/gemfire/util/PropertiesBuilderTests.java +++ b/src/test/java/org/springframework/data/gemfire/util/PropertiesBuilderTests.java @@ -279,6 +279,16 @@ public class PropertiesBuilderTests { assertThat(properties.getProperty("two"), is(equalTo("2"))); } + @Test + public void unsetPropertyIsSuccessful() { + Properties properties = PropertiesBuilder.create().unsetProperty("example").build(); + + assertThat(properties, is(notNullValue(Properties.class))); + assertThat(properties.size(), is(equalTo(1))); + assertThat(properties.containsKey("example"), is(true)); + assertThat(properties.getProperty("example"), is(equalTo(""))); + } + @Test public void stringLiteralIsValuable() { assertThat(PropertiesBuilder.create().isValuable("test"), is(true));