|
16 | 16 | package org.springframework.data;
|
17 | 17 |
|
18 | 18 | import java.util.ArrayList;
|
| 19 | +import java.util.Collections; |
19 | 20 | import java.util.List;
|
20 | 21 | import java.util.function.Consumer;
|
21 | 22 | import java.util.function.Supplier;
|
22 | 23 | import java.util.stream.Stream;
|
23 | 24 |
|
24 | 25 | import org.springframework.data.util.Lazy;
|
| 26 | +import org.springframework.lang.NonNull; |
| 27 | +import org.springframework.lang.Nullable; |
25 | 28 |
|
26 | 29 | /**
|
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. |
29 | 34 | *
|
30 | 35 | * @author Christoph Strobl
|
| 36 | + * @author John Blum |
| 37 | + * @see java.lang.FunctionalInterface |
31 | 38 | * @since 3.0
|
32 | 39 | */
|
| 40 | +// TODO: Does this need to be in org.springframework.data? Maybe move to org.springframework.data.domain? |
| 41 | +@FunctionalInterface |
33 | 42 | public interface ManagedTypes {
|
34 | 43 |
|
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 | + } |
38 | 55 |
|
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(); |
42 | 67 | }
|
43 | 68 |
|
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) { |
45 | 83 | return types::forEach;
|
46 | 84 | }
|
47 | 85 |
|
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) { |
49 | 100 | return types::forEach;
|
50 | 101 | }
|
51 | 102 |
|
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) { |
53 | 118 |
|
54 | 119 | return new ManagedTypes() {
|
55 | 120 |
|
56 |
| - Lazy<Iterable<? extends Class<?>>> lazyProvider = Lazy.of(dataProvider); |
| 121 | + final Lazy<Iterable<? extends Class<?>>> lazyProvider = Lazy.of(dataProvider); |
57 | 122 |
|
58 | 123 | @Override
|
59 |
| - public void forEach(Consumer<Class<?>> action) { |
| 124 | + public void forEach(@NonNull Consumer<Class<?>> action) { |
60 | 125 | lazyProvider.get().forEach(action);
|
61 | 126 | }
|
62 | 127 | };
|
63 | 128 | }
|
| 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 | + } |
64 | 152 | }
|
0 commit comments