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;
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.data.mapping.context;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
@@ -112,11 +111,11 @@ public class MappingContextIsNewStrategyFactory extends IsNewStrategyFactorySupp
|
||||
* @see org.springframework.data.support.IsNewStrategy#isNew(java.util.Optional)
|
||||
*/
|
||||
@Override
|
||||
public boolean isNew(Optional<? extends Object> entity) {
|
||||
public boolean isNew(Object entity) {
|
||||
|
||||
return entity//
|
||||
.map(it -> isNew.apply(property.getOwner().getPropertyAccessor(it).getProperty(property)))//
|
||||
.orElse(false);
|
||||
Assert.notNull(entity, "Entity must not be null!");
|
||||
|
||||
return isNew.apply(property.getOwner().getPropertyAccessor(entity).getProperty(property));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,8 +15,6 @@
|
||||
*/
|
||||
package org.springframework.data.support;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Strategy interface to determine whether a given entity is to be considered new.
|
||||
*
|
||||
@@ -28,8 +26,8 @@ public interface IsNewStrategy {
|
||||
/**
|
||||
* Returns whether the given entity is new, i.e. has never been persisted before or not.
|
||||
*
|
||||
* @param entity can be {@literal null}.
|
||||
* @param entity must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
boolean isNew(Optional<? extends Object> entity);
|
||||
boolean isNew(Object entity);
|
||||
}
|
||||
|
||||
@@ -15,9 +15,8 @@
|
||||
*/
|
||||
package org.springframework.data.support;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.domain.Persistable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link IsNewStrategy} that invokes {@link Persistable#isNew()} on the given object.
|
||||
@@ -28,21 +27,20 @@ public enum PersistableIsNewStrategy implements IsNewStrategy {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
/*
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.support.IsNewStrategy#isNew(java.util.Optional)
|
||||
* @see org.springframework.data.support.IsNewStrategy#isNew(java.lang.Object)
|
||||
*/
|
||||
public boolean isNew(Optional<? extends Object> entity) {
|
||||
@Override
|
||||
public boolean isNew(Object entity) {
|
||||
|
||||
return entity.map(it -> {
|
||||
Assert.notNull(entity, "Entity must not be null!");
|
||||
|
||||
if (!(it instanceof Persistable)) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Given object of type %s does not implement %s!", it.getClass(), Persistable.class));
|
||||
}
|
||||
if (!(entity instanceof Persistable)) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Given object of type %s does not implement %s!", entity.getClass(), Persistable.class));
|
||||
}
|
||||
|
||||
return ((Persistable<?>) it).isNew();
|
||||
|
||||
}).orElse(false);
|
||||
return ((Persistable<?>) entity).isNew();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user