Polishing.
Remove PropertyTransformerSupport in favor of default method in PropertyTransformer and refactor PropertyTransformer to functional interface. See gh-169.
This commit is contained in:
@@ -27,6 +27,7 @@ import java.util.Map;
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface PropertyTransformer {
|
||||
|
||||
/**
|
||||
@@ -38,7 +39,7 @@ public interface PropertyTransformer {
|
||||
* @param input must not be {@literal null}.
|
||||
* @return transformed properties.
|
||||
*/
|
||||
Map<String, Object> transformProperties(Map<String, Object> input);
|
||||
Map<String, Object> transformProperties(Map<String, ? extends Object> input);
|
||||
|
||||
/**
|
||||
* Return a composed transformer function that first applies this filter, and then
|
||||
@@ -47,5 +48,7 @@ public interface PropertyTransformer {
|
||||
* @return a composed transformer that first applies this function and then applies
|
||||
* the {@code after} transformer.
|
||||
*/
|
||||
PropertyTransformer andThen(PropertyTransformer after);
|
||||
default PropertyTransformer andThen(PropertyTransformer after) {
|
||||
return input -> after.transformProperties(transformProperties(input));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,33 +53,11 @@ public abstract class PropertyTransformers {
|
||||
return KeyPrefixPropertyTransformer.forPrefix(propertyNamePrefix);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation support class for classes implementing {@link PropertyTransformer}.
|
||||
*/
|
||||
abstract static class PropertyTransformerSupport implements PropertyTransformer {
|
||||
|
||||
@Override
|
||||
public PropertyTransformer andThen(final PropertyTransformer after) {
|
||||
|
||||
final PropertyTransformer that = this;
|
||||
|
||||
return new PropertyTransformerSupport() {
|
||||
|
||||
@Override
|
||||
public Map<String, Object> transformProperties(Map<String, Object> input) {
|
||||
|
||||
Map<String, Object> processed = that.transformProperties(input);
|
||||
return after.transformProperties(processed);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link PropertyTransformer} that passes the given properties through without
|
||||
* returning changed properties.
|
||||
*/
|
||||
static class NoOpPropertyTransformer extends PropertyTransformerSupport {
|
||||
static class NoOpPropertyTransformer implements PropertyTransformer {
|
||||
|
||||
static NoOpPropertyTransformer INSTANCE = new NoOpPropertyTransformer();
|
||||
|
||||
@@ -94,15 +72,15 @@ public abstract class PropertyTransformers {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> transformProperties(Map<String, Object> input) {
|
||||
return input;
|
||||
public Map<String, Object> transformProperties(Map<String, ? extends Object> input) {
|
||||
return (Map) input;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link PropertyTransformer} to remove {@literal null}-value properties.
|
||||
*/
|
||||
static class RemoveNullProperties extends PropertyTransformerSupport {
|
||||
static class RemoveNullProperties implements PropertyTransformer {
|
||||
|
||||
static RemoveNullProperties INSTANCE = new RemoveNullProperties();
|
||||
|
||||
@@ -117,12 +95,12 @@ public abstract class PropertyTransformers {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> transformProperties(Map<String, Object> input) {
|
||||
public Map<String, Object> transformProperties(Map<String, ? extends Object> input) {
|
||||
|
||||
Map<String, Object> target = new LinkedHashMap<>(input.size(),
|
||||
1);
|
||||
|
||||
for (Entry<String, Object> entry : input.entrySet()) {
|
||||
for (Entry<String, ? extends Object> entry : input.entrySet()) {
|
||||
|
||||
if (entry.getValue() == null) {
|
||||
continue;
|
||||
@@ -138,7 +116,7 @@ public abstract class PropertyTransformers {
|
||||
/**
|
||||
* {@link PropertyTransformer} that adds a prefix to each key name.
|
||||
*/
|
||||
static class KeyPrefixPropertyTransformer extends PropertyTransformerSupport {
|
||||
static class KeyPrefixPropertyTransformer implements PropertyTransformer {
|
||||
|
||||
private final String propertyNamePrefix;
|
||||
|
||||
@@ -162,17 +140,16 @@ public abstract class PropertyTransformers {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> transformProperties(Map<String, Object> input) {
|
||||
public Map<String, Object> transformProperties(Map<String, ? extends Object> input) {
|
||||
|
||||
Map<String, Object> target = new LinkedHashMap<>(input.size(),
|
||||
1);
|
||||
|
||||
for (Entry<String, Object> entry : input.entrySet()) {
|
||||
for (Entry<String, ? extends Object> entry : input.entrySet()) {
|
||||
target.put(propertyNamePrefix + entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
public class PropertyTransformersUnitTests {
|
||||
|
||||
Map<String, Object> properties = Collections.singletonMap("key", "value");
|
||||
Map<String, String> properties = Collections.singletonMap("key", "value");
|
||||
|
||||
@Test
|
||||
public void propertyNamePrefix() {
|
||||
|
||||
Reference in New Issue
Block a user