DATACMNS-867 - Less Optional for auditing subsystem.
Removed Optional<Object> parameters in favor of plain Object and clients that avoid calling methods for absent values in the first place.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
* Copyright 2015-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -28,8 +28,8 @@ public interface AuditableBeanWrapperFactory {
|
||||
/**
|
||||
* Returns the {@link AuditableBeanWrapper} for the given source object if it's eligible for auditing.
|
||||
*
|
||||
* @param source.
|
||||
* @param source must not be {@literal null}.
|
||||
* @return the {@link AuditableBeanWrapper} for the given source object if it's eligible for auditing.
|
||||
*/
|
||||
Optional<AuditableBeanWrapper> getBeanWrapperFor(Optional<? extends Object> source);
|
||||
Optional<AuditableBeanWrapper> getBeanWrapperFor(Object source);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
package org.springframework.data.auditing;
|
||||
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -125,7 +124,7 @@ public class AuditingHandler implements InitializingBean {
|
||||
*
|
||||
* @param source
|
||||
*/
|
||||
public void markCreated(Optional<? extends Object> source) {
|
||||
public void markCreated(Object source) {
|
||||
touch(source, true);
|
||||
}
|
||||
|
||||
@@ -134,7 +133,7 @@ public class AuditingHandler implements InitializingBean {
|
||||
*
|
||||
* @param source
|
||||
*/
|
||||
public void markModified(Optional<? extends Object> source) {
|
||||
public void markModified(Object source) {
|
||||
touch(source, false);
|
||||
}
|
||||
|
||||
@@ -144,15 +143,16 @@ public class AuditingHandler implements InitializingBean {
|
||||
* @param source must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
protected final boolean isAuditable(Optional<Object> source) {
|
||||
return source.flatMap(o -> factory.getBeanWrapperFor(source)).isPresent();
|
||||
protected final boolean isAuditable(Object source) {
|
||||
|
||||
Assert.notNull(source, "Source must not be null!");
|
||||
|
||||
return factory.getBeanWrapperFor(source).isPresent();
|
||||
}
|
||||
|
||||
private void touch(Optional<? extends Object> target, boolean isNew) {
|
||||
private void touch(Object target, boolean isNew) {
|
||||
|
||||
Optional<AuditableBeanWrapper> wrapper = factory.getBeanWrapperFor(target);
|
||||
|
||||
wrapper.ifPresent(it -> {
|
||||
factory.getBeanWrapperFor(target).ifPresent(it -> {
|
||||
|
||||
Optional<Object> auditor = touchAuditor(it, isNew);
|
||||
Optional<TemporalAccessor> now = dateTimeForNow ? touchDate(it, isNew) : Optional.empty();
|
||||
|
||||
@@ -50,9 +50,11 @@ class DefaultAuditableBeanWrapperFactory implements AuditableBeanWrapperFactory
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public Optional<AuditableBeanWrapper> getBeanWrapperFor(Optional<? extends Object> source) {
|
||||
public Optional<AuditableBeanWrapper> getBeanWrapperFor(Object source) {
|
||||
|
||||
return source.map(it -> {
|
||||
Assert.notNull(source, "Source must not be null!");
|
||||
|
||||
return Optional.of(source).map(it -> {
|
||||
|
||||
if (it instanceof Auditable) {
|
||||
return new AuditableInterfaceBeanWrapper((Auditable<Object, ?, TemporalAccessor>) it);
|
||||
@@ -191,9 +193,10 @@ class DefaultAuditableBeanWrapperFactory implements AuditableBeanWrapperFactory
|
||||
|
||||
if (conversionService.canConvert(Date.class, targetType)) {
|
||||
|
||||
if(!conversionService.canConvert(it.getClass(), Date.class)) {
|
||||
throw new IllegalArgumentException(String.format("Cannot convert date type for member %s! From %s to java.util.Date to %s.",
|
||||
source, it.getClass(), targetType));
|
||||
if (!conversionService.canConvert(it.getClass(), Date.class)) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Cannot convert date type for member %s! From %s to java.util.Date to %s.", source,
|
||||
it.getClass(), targetType));
|
||||
}
|
||||
|
||||
Date date = conversionService.convert(it, Date.class);
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package org.springframework.data.auditing;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -74,7 +73,7 @@ public class IsNewAwareAuditingHandler extends AuditingHandler {
|
||||
*
|
||||
* @param object
|
||||
*/
|
||||
public void markAudited(Optional<Object> object) {
|
||||
public void markAudited(Object object) {
|
||||
|
||||
Assert.notNull(object, "Source object must not be null!");
|
||||
|
||||
@@ -82,15 +81,12 @@ public class IsNewAwareAuditingHandler extends AuditingHandler {
|
||||
return;
|
||||
}
|
||||
|
||||
object.ifPresent(it -> {
|
||||
IsNewStrategy strategy = isNewStrategyFactory.getIsNewStrategy(object.getClass());
|
||||
|
||||
IsNewStrategy strategy = isNewStrategyFactory.getIsNewStrategy(it.getClass());
|
||||
|
||||
if (strategy.isNew(object)) {
|
||||
markCreated(object);
|
||||
} else {
|
||||
markModified(object);
|
||||
}
|
||||
});
|
||||
if (strategy.isNew(object)) {
|
||||
markCreated(object);
|
||||
} else {
|
||||
markModified(object);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,9 +65,9 @@ public class MappingAuditableBeanWrapperFactory extends DefaultAuditableBeanWrap
|
||||
* @see org.springframework.data.auditing.AuditableBeanWrapperFactory#getBeanWrapperFor(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public Optional<AuditableBeanWrapper> getBeanWrapperFor(Optional<? extends Object> source) {
|
||||
public Optional<AuditableBeanWrapper> getBeanWrapperFor(Object source) {
|
||||
|
||||
return source.flatMap(it -> {
|
||||
return Optional.of(source).flatMap(it -> {
|
||||
|
||||
if (it instanceof Auditable) {
|
||||
return super.getBeanWrapperFor(source);
|
||||
@@ -80,7 +80,7 @@ public class MappingAuditableBeanWrapperFactory extends DefaultAuditableBeanWrap
|
||||
MappingAuditingMetadata metadata = metadataCache.computeIfAbsent(type,
|
||||
key -> new MappingAuditingMetadata(entity));
|
||||
|
||||
return Optional.<AuditableBeanWrapper>ofNullable(metadata.isAuditable()
|
||||
return Optional.<AuditableBeanWrapper> ofNullable(metadata.isAuditable()
|
||||
? new MappingMetadataAuditableBeanWrapper(entity.getPropertyAccessor(it), metadata) : null);
|
||||
|
||||
}).orElseGet(() -> super.getBeanWrapperFor(source));
|
||||
@@ -172,7 +172,8 @@ public class MappingAuditableBeanWrapperFactory extends DefaultAuditableBeanWrap
|
||||
@Override
|
||||
public Optional<TemporalAccessor> setCreatedDate(Optional<TemporalAccessor> value) {
|
||||
|
||||
metadata.createdDateProperty.ifPresent(it -> this.accessor.setProperty(it, getDateValueToSet(value, it.getType(), it)));
|
||||
metadata.createdDateProperty
|
||||
.ifPresent(it -> this.accessor.setProperty(it, getDateValueToSet(value, it.getType(), it)));
|
||||
|
||||
return value;
|
||||
}
|
||||
@@ -206,7 +207,8 @@ public class MappingAuditableBeanWrapperFactory extends DefaultAuditableBeanWrap
|
||||
@Override
|
||||
public Optional<TemporalAccessor> setLastModifiedDate(Optional<TemporalAccessor> value) {
|
||||
|
||||
metadata.lastModifiedDateProperty.ifPresent(it -> this.accessor.setProperty(it, getDateValueToSet(value, it.getType(), it)));
|
||||
metadata.lastModifiedDateProperty
|
||||
.ifPresent(it -> this.accessor.setProperty(it, getDateValueToSet(value, it.getType(), it)));
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user