|
| 1 | +/* |
| 2 | + * Copyright 2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.domain; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.List; |
| 21 | +import java.util.function.Consumer; |
| 22 | +import java.util.function.Supplier; |
| 23 | +import java.util.stream.Stream; |
| 24 | + |
| 25 | +import org.springframework.data.util.Lazy; |
| 26 | + |
| 27 | +/** |
| 28 | + * Types managed by a Spring Data implementation. Used to predefine a set of know entities that might need processing |
| 29 | + * during the Spring container, Spring Data Repository initialization phase. |
| 30 | + * |
| 31 | + * @author Christoph Strobl |
| 32 | + * @author John Blum |
| 33 | + * @see java.lang.FunctionalInterface |
| 34 | + * @since 3.0 |
| 35 | + */ |
| 36 | +@FunctionalInterface |
| 37 | +public interface ManagedTypes { |
| 38 | + |
| 39 | + /** |
| 40 | + * Factory method used to construct a new instance of {@link ManagedTypes} containing no {@link Class types}. |
| 41 | + * |
| 42 | + * @return an empty {@link ManagedTypes} instance. |
| 43 | + * @see java.util.Collections#emptySet() |
| 44 | + * @see #fromIterable(Iterable) |
| 45 | + */ |
| 46 | + static ManagedTypes empty() { |
| 47 | + return fromIterable(Collections.emptySet()); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Factory method used to construct {@link ManagedTypes} from the given, required {@link Iterable} of {@link Class |
| 52 | + * types}. |
| 53 | + * |
| 54 | + * @param types {@link Iterable} of {@link Class types} used to initialize the {@link ManagedTypes}; must not be |
| 55 | + * {@literal null}. |
| 56 | + * @return new instance of {@link ManagedTypes} initialized the given, required {@link Iterable} of {@link Class |
| 57 | + * types}. |
| 58 | + * @see java.lang.Iterable |
| 59 | + * @see #fromStream(Stream) |
| 60 | + * @see #fromSupplier(Supplier) |
| 61 | + */ |
| 62 | + static ManagedTypes fromIterable(Iterable<? extends Class<?>> types) { |
| 63 | + return types::forEach; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Factory method used to construct {@link ManagedTypes} from the given, required {@link Stream} of {@link Class |
| 68 | + * types}. |
| 69 | + * |
| 70 | + * @param types {@link Stream} of {@link Class types} used to initialize the {@link ManagedTypes}; must not be |
| 71 | + * {@literal null}. |
| 72 | + * @return new instance of {@link ManagedTypes} initialized the given, required {@link Stream} of {@link Class types}. |
| 73 | + * @see java.util.stream.Stream |
| 74 | + * @see #fromIterable(Iterable) |
| 75 | + * @see #fromSupplier(Supplier) |
| 76 | + */ |
| 77 | + static ManagedTypes fromStream(Stream<? extends Class<?>> types) { |
| 78 | + return types::forEach; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Factory method used to construct {@link ManagedTypes} from the given, required {@link Supplier} of an |
| 83 | + * {@link Iterable} of {@link Class types}. |
| 84 | + * |
| 85 | + * @param dataProvider {@link Supplier} of an {@link Iterable} of {@link Class types} used to lazily initialize the |
| 86 | + * {@link ManagedTypes}; must not be {@literal null}. |
| 87 | + * @return new instance of {@link ManagedTypes} initialized the given, required {@link Supplier} of an |
| 88 | + * {@link Iterable} of {@link Class types}. |
| 89 | + * @see java.util.function.Supplier |
| 90 | + * @see java.lang.Iterable |
| 91 | + * @see #fromIterable(Iterable) |
| 92 | + * @see #fromStream(Stream) |
| 93 | + */ |
| 94 | + static ManagedTypes fromSupplier(Supplier<Iterable<Class<?>>> dataProvider) { |
| 95 | + |
| 96 | + return new ManagedTypes() { |
| 97 | + |
| 98 | + final Lazy<Iterable<Class<?>>> lazyProvider = Lazy.of(dataProvider); |
| 99 | + |
| 100 | + @Override |
| 101 | + public void forEach(Consumer<Class<?>> action) { |
| 102 | + lazyProvider.get().forEach(action); |
| 103 | + } |
| 104 | + }; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Applies the given {@link Consumer action} to each of the {@link Class types} contained in this {@link ManagedTypes} |
| 109 | + * instance. |
| 110 | + * |
| 111 | + * @param action {@link Consumer} defining the action to perform on the {@link Class types} contained in this |
| 112 | + * {@link ManagedTypes} instance; must not be {@literal null}. |
| 113 | + * @see java.util.function.Consumer |
| 114 | + */ |
| 115 | + void forEach(Consumer<Class<?>> action); |
| 116 | + |
| 117 | + /** |
| 118 | + * Returns all the {@link ManagedTypes} in a {@link List}. |
| 119 | + * |
| 120 | + * @return these {@link ManagedTypes} in a {@link List}; never {@literal null}. |
| 121 | + * @see java.util.List |
| 122 | + */ |
| 123 | + default List<Class<?>> toList() { |
| 124 | + |
| 125 | + List<Class<?>> list = new ArrayList<>(100); |
| 126 | + forEach(list::add); |
| 127 | + return list; |
| 128 | + } |
| 129 | +} |
0 commit comments