Skip to content

Commit d8118de

Browse files
jxblumchristophstrobl
authored andcommitted
Review, refactor and polish Spring Data AOT infrastructure classes.
* Refactored logic in AOT infrastructure classes. * Annotated AOT API with Spring's @nonnull and @nullable annotations. * Edited Javadoc. * Introduced PredicateUtils abstract utility class encapsulating common Predicates on types and class members. * Added comments for review and clarification. Original Pull Request: #2624
1 parent 21ff2a7 commit d8118de

30 files changed

+1785
-1029
lines changed

src/main/java/org/springframework/data/ManagedTypes.java

Lines changed: 101 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,137 @@
1616
package org.springframework.data;
1717

1818
import java.util.ArrayList;
19+
import java.util.Collections;
1920
import java.util.List;
2021
import java.util.function.Consumer;
2122
import java.util.function.Supplier;
2223
import java.util.stream.Stream;
2324

2425
import org.springframework.data.util.Lazy;
26+
import org.springframework.lang.NonNull;
27+
import org.springframework.lang.Nullable;
2528

2629
/**
27-
* Types managed by a Spring Data implementation. Used to predefine a set of know entities that might need processing
28-
* during container/repository initialization phase.
30+
* Types managed by a Spring Data implementation.
31+
*
32+
* Used to predefine a set of know entities that might need processing during the Spring container,
33+
* Spring Data Repository initialization phase.
2934
*
3035
* @author Christoph Strobl
36+
* @author John Blum
37+
* @see java.lang.FunctionalInterface
3138
* @since 3.0
3239
*/
40+
// TODO: Does this need to be in org.springframework.data? Maybe move to org.springframework.data.domain?
41+
@FunctionalInterface
3342
public interface ManagedTypes {
3443

35-
void forEach(Consumer<Class<?>> action);
36-
37-
default List<Class<?>> toList() {
44+
/**
45+
* Factory method used to construct a new instance of {@link ManagedTypes} containing no {@link Class types}.
46+
*
47+
* @return an empty {@link ManagedTypes} instance.
48+
* @see java.util.Collections#emptySet()
49+
* @see #of(Iterable)
50+
*/
51+
@NonNull
52+
static ManagedTypes empty() {
53+
return of(Collections.emptySet());
54+
}
3855

39-
List<Class<?>> tmp = new ArrayList<>(100);
40-
forEach(tmp::add);
41-
return tmp;
56+
/**
57+
* Factory method used to return a {@literal null-safe} instance of {@link ManagedTypes}.
58+
*
59+
* @param types {@link ManagedTypes} to evaluate.
60+
* @return the given {@link ManagedTypes} if not {@literal null}
61+
* or an {@link #empty()} {@link ManagedTypes} instance.
62+
* @see #empty()
63+
*/
64+
@NonNull
65+
static ManagedTypes nullSafeManagedTypes(@Nullable ManagedTypes types) {
66+
return types != null ? types : empty();
4267
}
4368

44-
static ManagedTypes of(Iterable<? extends Class<?>> types) {
69+
/**
70+
* Factory method used to construct {@link ManagedTypes} from the given, required {@link Iterable}
71+
* of {@link Class types}.
72+
*
73+
* @param types {@link Iterable} of {@link Class types} used to initialize the {@link ManagedTypes};
74+
* must not be {@literal null}.
75+
* @return new instance of {@link ManagedTypes} initialized the given, required {@link Iterable}
76+
* of {@link Class types}.
77+
* @see java.lang.Iterable
78+
* @see #of(Stream)
79+
* @see #of(Supplier)
80+
*/
81+
@NonNull
82+
static ManagedTypes of(@NonNull Iterable<? extends Class<?>> types) {
4583
return types::forEach;
4684
}
4785

48-
static ManagedTypes of(Stream<? extends Class<?>> types) {
86+
/**
87+
* Factory method used to construct {@link ManagedTypes} from the given, required {@link Stream}
88+
* of {@link Class types}.
89+
*
90+
* @param types {@link Stream} of {@link Class types} used to initialize the {@link ManagedTypes};
91+
* must not be {@literal null}.
92+
* @return new instance of {@link ManagedTypes} initialized the given, required {@link Stream}
93+
* of {@link Class types}.
94+
* @see java.util.stream.Stream
95+
* @see #of(Iterable)
96+
* @see #of(Supplier)
97+
*/
98+
@NonNull
99+
static ManagedTypes of(@NonNull Stream<? extends Class<?>> types) {
49100
return types::forEach;
50101
}
51102

52-
static ManagedTypes o(Supplier<Iterable<? extends Class<?>>> dataProvider) {
103+
/**
104+
* Factory method used to construct {@link ManagedTypes} from the given, required {@link Supplier} of
105+
* an {@link Iterable} of {@link Class types}.
106+
*
107+
* @param dataProvider {@link Supplier} of an {@link Iterable} of {@link Class types} used to lazily initialize
108+
* the {@link ManagedTypes}; must not be {@literal null}.
109+
* @return new instance of {@link ManagedTypes} initialized the given, required {@link Supplier} of
110+
* an {@link Iterable} of {@link Class types}.
111+
* @see java.util.function.Supplier
112+
* @see java.lang.Iterable
113+
* @see #of(Iterable)
114+
* @see #of(Stream)
115+
*/
116+
@NonNull
117+
static ManagedTypes of(@NonNull Supplier<Iterable<? extends Class<?>>> dataProvider) {
53118

54119
return new ManagedTypes() {
55120

56-
Lazy<Iterable<? extends Class<?>>> lazyProvider = Lazy.of(dataProvider);
121+
final Lazy<Iterable<? extends Class<?>>> lazyProvider = Lazy.of(dataProvider);
57122

58123
@Override
59-
public void forEach(Consumer<Class<?>> action) {
124+
public void forEach(@NonNull Consumer<Class<?>> action) {
60125
lazyProvider.get().forEach(action);
61126
}
62127
};
63128
}
129+
130+
/**
131+
* Applies the given {@link Consumer action} to each of the {@link Class types} contained in
132+
* this {@link ManagedTypes} instance.
133+
*
134+
* @param action {@link Consumer} defining the action to perform on the {@link Class types}
135+
* contained in this {@link ManagedTypes} instance; must not be {@literal null}.
136+
* @see java.util.function.Consumer
137+
*/
138+
void forEach(Consumer<Class<?>> action);
139+
140+
/**
141+
* Returns all the {@link ManagedTypes} in a {@link List}.
142+
*
143+
* @return these {@link ManagedTypes} in a {@link List}; never {@literal null}.
144+
* @see java.util.List
145+
*/
146+
default List<Class<?>> toList() {
147+
148+
List<Class<?>> list = new ArrayList<>(100);
149+
forEach(list::add);
150+
return list;
151+
}
64152
}

0 commit comments

Comments
 (0)