DATACMNS-1251 - Added methods returning Instant to Revision.

Revision and RevisionMetadata now have methods returning Instant instead of LocalDateTime. The existing methods returning LocalDateTime are now deprecated.

Original pull request: #270.
This commit is contained in:
Jens Schauder
2018-02-01 14:59:33 +01:00
committed by Oliver Gierke
parent 6675e33233
commit 139e098e85
5 changed files with 156 additions and 13 deletions

View File

@@ -16,7 +16,10 @@
package org.springframework.data.history;
import java.lang.annotation.Annotation;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.temporal.TemporalAccessor;
import java.util.Optional;
import org.springframework.data.util.AnnotationDetectionFieldCallback;
@@ -26,15 +29,17 @@ import org.springframework.util.ReflectionUtils;
/**
* A {@link RevisionMetadata} implementation that inspects the given object for fields with the configured annotations
* and returns the field's values on calls to {@link #getRevisionDate()} and {@link #getRevisionNumber()}.
* and returns the field's values on calls to {@link #getRevisionDate()}, {@link #getRevisionInstant()} and
* {@link #getRevisionNumber()}.
*
* @author Oliver Gierke
* @author Jens Schauder
*/
public class AnnotationRevisionMetadata<N extends Number & Comparable<N>> implements RevisionMetadata<N> {
private final Object entity;
private final Lazy<Optional<N>> revisionNumber;
private final Lazy<Optional<LocalDateTime>> revisionDate;
private final Lazy<Optional<TemporalAccessor>> revisionDate;
/**
* Creates a new {@link AnnotationRevisionMetadata} inspecting the given entity for the given annotations. If no
@@ -68,8 +73,44 @@ public class AnnotationRevisionMetadata<N extends Number & Comparable<N>> implem
* (non-Javadoc)
* @see org.springframework.data.history.RevisionMetadata#getRevisionDate()
*/
@Deprecated
public Optional<LocalDateTime> getRevisionDate() {
return revisionDate.get();
return revisionDate.get().map(this::convertToLocalDateTime);
}
/*
* (non-Javadoc)
* @see org.springframework.data.history.RevisionMetadata#getRevisionDate()
*/
public Optional<Instant> getRevisionInstant() {
return revisionDate.get().map(this::convertToInstant);
}
private LocalDateTime convertToLocalDateTime(TemporalAccessor temporalAccessor) {
if (temporalAccessor instanceof LocalDateTime) {
return (LocalDateTime) temporalAccessor;
}
if (temporalAccessor instanceof Instant) {
return LocalDateTime.ofInstant((Instant) temporalAccessor, ZoneOffset.systemDefault());
}
throw new IllegalArgumentException(String.format("Can't convert %s to LocalDateTime", temporalAccessor));
}
private Instant convertToInstant(TemporalAccessor temporalAccessor) {
if (temporalAccessor instanceof Instant) {
return (Instant) temporalAccessor;
}
if (temporalAccessor instanceof LocalDateTime) {
return ((LocalDateTime) temporalAccessor).atZone(ZoneOffset.systemDefault()).toInstant();
}
throw new IllegalArgumentException(String.format("Can't convert %s to LocalDateTime", temporalAccessor));
}
/*

View File

@@ -22,6 +22,7 @@ import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.Optional;
@@ -33,6 +34,7 @@ import org.springframework.lang.Nullable;
* @author Oliver Gierke
* @author Philipp Huegelmeyer
* @author Christoph Strobl
* @author Jens Schauder
*/
@Value
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@@ -80,21 +82,43 @@ public final class Revision<N extends Number & Comparable<N>, T> implements Comp
/**
* Returns the revision date of the revision.
*
* @return
* @return Guaranteed to be not {@literal null}.
* @deprecated Use {@link #getRevisionInstant()} instead.
*/
@Deprecated
public Optional<LocalDateTime> getRevisionDate() {
return metadata.getRevisionDate();
}
/**
* Returns the timestamp of the revision.
*
* @return Guaranteed to be not {@literal null}.
*/
public Optional<Instant> getRevisionInstant() {
return metadata.getRevisionInstant();
}
/**
* Returns the revision date of the revision, immediately failing on absence.
*
* @return the revision date.
* @deprecated Use {@link #getRequiredRevisionInstant()} instead.
*/
@Deprecated
public LocalDateTime getRequiredRevisionDate() {
return metadata.getRequiredRevisionDate();
}
/**
* Returns the timestamp of the revision, immediately failing on absence.
*
* @return the revision {@link Instant}. May be {@literal null}.
*/
public Instant getRequiredRevisionInstant() {
return metadata.getRequiredRevisionInstant();
}
/*
* (non-Javadoc)
* @see java.lang.Comparable#compareTo(java.lang.Object)

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.history;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.Optional;
@@ -23,6 +24,7 @@ import java.util.Optional;
*
* @author Philipp Huegelmeyer
* @author Oliver Gierke
* @author Jens Schauder
*/
public interface RevisionMetadata<N extends Number & Comparable<N>> {
@@ -48,20 +50,43 @@ public interface RevisionMetadata<N extends Number & Comparable<N>> {
* Returns the date of the revision.
*
* @return will never be {@literal null}.
* @deprecated use {@link #getRevisionInstant()} instead.
*/
@Deprecated
Optional<LocalDateTime> getRevisionDate();
/**
* Returns the timestamp of the revision.
*
* @return will never be {@literal null}.
*/
Optional<Instant> getRevisionInstant();
/**
* Returns the revision date of the revision, immediately failing on absence.
*
* @return will never be {@literal null}.
* @throw IllegalStateException if no revision date is available.
* @throws IllegalStateException if no revision date is available.
* @deprecated Use {@link #getRevisionInstant()} instead.
*/
@Deprecated
default LocalDateTime getRequiredRevisionDate() {
return getRevisionDate().orElseThrow(
() -> new IllegalStateException(String.format("No revision date found on %s!", (Object) getDelegate())));
}
/**
* Returns the timestamp of the revision, immediately failing on absence.
*
* @return will never be {@literal null}.
* @throws IllegalStateException if no revision date is available.
*/
default Instant getRequiredRevisionInstant() {
return getRevisionInstant().orElseThrow(
() -> new IllegalStateException(String.format("No revision date found on %s!", (Object) getDelegate())));
}
/**
* Returns the underlying revision metadata which might provider more detailed implementation specific information.
*