1
1
/*
2
- * Copyright 2012 the original author or authors.
2
+ * Copyright 2012-2017 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
20
20
import java .util .Optional ;
21
21
22
22
import org .springframework .data .util .AnnotationDetectionFieldCallback ;
23
+ import org .springframework .data .util .Lazy ;
23
24
import org .springframework .util .Assert ;
24
25
import org .springframework .util .ReflectionUtils ;
25
26
32
33
public class AnnotationRevisionMetadata <N extends Number & Comparable <N >> implements RevisionMetadata <N > {
33
34
34
35
private final Object entity ;
35
- private final N revisionNumber ;
36
- private final LocalDateTime revisionDate ;
36
+ private final Lazy < Optional < N >> revisionNumber ;
37
+ private final Lazy < Optional < LocalDateTime >> revisionDate ;
37
38
38
39
/**
39
40
* Creates a new {@link AnnotationRevisionMetadata} inspecting the given entity for the given annotations. If no
40
41
* annotations will be provided these values will not be looked up from the entity and return {@literal null}.
41
42
*
42
43
* @param entity must not be {@literal null}.
43
- * @param revisionNumberAnnotation
44
- * @param revisionTimeStampAnnotation
44
+ * @param revisionNumberAnnotation must not be {@literal null}.
45
+ * @param revisionTimeStampAnnotation must not be {@literal null}.
45
46
*/
46
- public AnnotationRevisionMetadata (final Object entity , Class <? extends Annotation > revisionNumberAnnotation ,
47
+ public AnnotationRevisionMetadata (Object entity , Class <? extends Annotation > revisionNumberAnnotation ,
47
48
Class <? extends Annotation > revisionTimeStampAnnotation ) {
48
49
49
50
Assert .notNull (entity , "Entity must not be null!" );
50
- this .entity = entity ;
51
-
52
- if (revisionNumberAnnotation != null ) {
53
- AnnotationDetectionFieldCallback numberCallback = new AnnotationDetectionFieldCallback (revisionNumberAnnotation );
54
- ReflectionUtils .doWithFields (entity .getClass (), numberCallback );
55
- this .revisionNumber = numberCallback .getValue (entity );
56
- } else {
57
- this .revisionNumber = null ;
58
- }
51
+ Assert .notNull (revisionNumberAnnotation , "Revision number annotation must not be null!" );
52
+ Assert .notNull (revisionTimeStampAnnotation , "Revision time stamp annotation must not be null!" );
59
53
60
- if (revisionTimeStampAnnotation != null ) {
61
- AnnotationDetectionFieldCallback revisionCallback = new AnnotationDetectionFieldCallback (
62
- revisionTimeStampAnnotation );
63
- ReflectionUtils .doWithFields (entity .getClass (), revisionCallback );
64
- this .revisionDate = revisionCallback .getValue (entity );
65
- } else {
66
- this .revisionDate = null ;
67
- }
54
+ this .entity = entity ;
55
+ this .revisionNumber = detectAnnotation (entity , revisionNumberAnnotation );
56
+ this .revisionDate = detectAnnotation (entity , revisionTimeStampAnnotation );
68
57
}
69
58
70
59
/*
71
60
* (non-Javadoc)
72
61
* @see org.springframework.data.repository.history.RevisionMetadata#getRevisionNumber()
73
62
*/
74
63
public Optional <N > getRevisionNumber () {
75
- return Optional . ofNullable ( revisionNumber );
64
+ return revisionNumber . get ( );
76
65
}
77
66
78
67
/*
79
68
* (non-Javadoc)
80
69
* @see org.springframework.data.history.RevisionMetadata#getRevisionDate()
81
70
*/
82
71
public Optional <LocalDateTime > getRevisionDate () {
83
- return Optional . ofNullable ( revisionDate );
72
+ return revisionDate . get ( );
84
73
}
85
74
86
75
/*
@@ -91,4 +80,14 @@ public Optional<LocalDateTime> getRevisionDate() {
91
80
public <T > T getDelegate () {
92
81
return (T ) entity ;
93
82
}
83
+
84
+ private static <T > Lazy <Optional <T >> detectAnnotation (Object entity , Class <? extends Annotation > annotationType ) {
85
+
86
+ return Lazy .of (() -> {
87
+
88
+ AnnotationDetectionFieldCallback numberCallback = new AnnotationDetectionFieldCallback (annotationType );
89
+ ReflectionUtils .doWithFields (entity .getClass (), numberCallback );
90
+ return numberCallback .getValue (entity );
91
+ });
92
+ }
94
93
}
0 commit comments