extract PatchableMapWrapper.of for reusability / less boilerplate code

This commit is contained in:
Michael Hoennig 2024-04-29 15:32:39 +02:00
parent dbe695c214
commit a7d30726a6
3 changed files with 36 additions and 38 deletions

View File

@ -98,7 +98,15 @@ public class HsBookingItemEntity implements Stringifyable, RbacObject {
private Map<String, Object> resources = new HashMap<>(); private Map<String, Object> resources = new HashMap<>();
@Transient @Transient
private PatchableMapWrapper resourcesWrapper; private PatchableMapWrapper<Object> resourcesWrapper;
public PatchableMapWrapper<Object> getResources() {
return PatchableMapWrapper.of(resourcesWrapper, (newWrapper) -> {resourcesWrapper = newWrapper; }, resources );
}
public void putResources(Map<String, Object> newResources) {
getResources().assign(newResources);
}
public void setValidFrom(final LocalDate validFrom) { public void setValidFrom(final LocalDate validFrom) {
setValidity(toPostgresDateRange(validFrom, getValidTo())); setValidity(toPostgresDateRange(validFrom, getValidTo()));
@ -116,20 +124,6 @@ public class HsBookingItemEntity implements Stringifyable, RbacObject {
return upperInclusiveFromPostgresDateRange(getValidity()); return upperInclusiveFromPostgresDateRange(getValidity());
} }
public PatchableMapWrapper getResources() {
if ( resourcesWrapper == null ) {
resourcesWrapper = new PatchableMapWrapper(resources);
}
return resourcesWrapper;
}
public void putResources(Map<String, Object> entries) {
if ( resourcesWrapper == null ) {
resourcesWrapper = new PatchableMapWrapper(resources);
}
resourcesWrapper.assign(entries);
}
@Override @Override
public String toString() { public String toString() {
return stringify.apply(this); return stringify.apply(this);

View File

@ -105,20 +105,14 @@ public class HsHostingAssetEntity implements Stringifyable, RbacObject {
private Map<String, Object> config = new HashMap<>(); private Map<String, Object> config = new HashMap<>();
@Transient @Transient
private PatchableMapWrapper configWrapper; private PatchableMapWrapper<Object> configWrapper;
public PatchableMapWrapper getConfig() { public PatchableMapWrapper<Object> getConfig() {
if ( configWrapper == null ) { return PatchableMapWrapper.of(configWrapper, (newWrapper) -> {configWrapper = newWrapper; }, config );
configWrapper = new PatchableMapWrapper(config);
}
return configWrapper;
} }
public void putConfig(Map<String, Object> entries) { public void putConfig(Map<String, Object> newConfg) {
if ( configWrapper == null ) { PatchableMapWrapper.of(configWrapper, (newWrapper) -> {configWrapper = newWrapper; }, config).assign(newConfg);
configWrapper = new PatchableMapWrapper(config);
}
configWrapper.assign(entries);
} }
@Override @Override

View File

@ -6,31 +6,41 @@ import jakarta.validation.constraints.NotNull;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.function.Consumer;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.joining;
/** This class wraps another (usually persistent) map and /** This class wraps another (usually persistent) map and
* supports applying `PatchMap` as well as a toString method with stable entry order. * supports applying `PatchMap` as well as a toString method with stable entry order.
*/ */
public class PatchableMapWrapper implements Map<String, Object> { public class PatchableMapWrapper<T> implements Map<String, T> {
private final Map<String, Object> delegate; private final Map<String, T> delegate;
public PatchableMapWrapper(final Map<String, Object> map) { private PatchableMapWrapper(final Map<String, T> map) {
delegate = map; delegate = map;
} }
public static <T> PatchableMapWrapper<T> of(final PatchableMapWrapper<T> currentWrapper, final Consumer<PatchableMapWrapper<T>> setWrapper, final Map<String, T> target) {
return ofNullable(currentWrapper).orElseGet(() -> {
final var newWrapper = new PatchableMapWrapper<T>(target);
setWrapper.accept(newWrapper);
return newWrapper;
});
}
@NotNull @NotNull
public static ImmutablePair<String, Object> entry(final String key, final Object value) { public static <E> ImmutablePair<String, E> entry(final String key, final E value) {
return new ImmutablePair<>(key, value); return new ImmutablePair<>(key, value);
} }
public void assign(final Map<String, Object> entries) { public void assign(final Map<String, T> entries) {
delegate.clear(); delegate.clear();
delegate.putAll(entries); delegate.putAll(entries);
} }
public void patch(final Map<String, Object> patch) { public void patch(final Map<String, T> patch) {
patch.forEach((key, value) -> { patch.forEach((key, value) -> {
if (value == null) { if (value == null) {
remove(key); remove(key);
@ -73,22 +83,22 @@ public class PatchableMapWrapper implements Map<String, Object> {
} }
@Override @Override
public Object get(final Object key) { public T get(final Object key) {
return delegate.get(key); return delegate.get(key);
} }
@Override @Override
public Object put(final String key, final Object value) { public T put(final String key, final T value) {
return delegate.put(key, value); return delegate.put(key, value);
} }
@Override @Override
public Object remove(final Object key) { public T remove(final Object key) {
return delegate.remove(key); return delegate.remove(key);
} }
@Override @Override
public void putAll(final Map<? extends String, ?> m) { public void putAll(final @NotNull Map<? extends String, ? extends T> m) {
delegate.putAll(m); delegate.putAll(m);
} }
@ -103,12 +113,12 @@ public class PatchableMapWrapper implements Map<String, Object> {
} }
@Override @Override
public Collection<Object> values() { public Collection<T> values() {
return delegate.values(); return delegate.values();
} }
@Override @Override
public Set<Entry<String, Object>> entrySet() { public Set<Entry<String, T>> entrySet() {
return delegate.entrySet(); return delegate.entrySet();
} }
} }