View Javadoc
1   /*
2    * Copyright 2020 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    *      http://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  
17  package org.bremersee.test.beans;
18  
19  import java.util.function.Consumer;
20  import org.springframework.beans.BeansException;
21  import org.springframework.beans.factory.NoSuchBeanDefinitionException;
22  import org.springframework.beans.factory.ObjectProvider;
23  import org.springframework.lang.NonNull;
24  import org.springframework.lang.Nullable;
25  
26  /**
27   * The simple object provider.
28   *
29   * @param <T> the type parameter
30   */
31  public class SimpleObjectProvider<T> implements ObjectProvider<T> {
32  
33    private final T provides;
34  
35    private final Consumer<T> dependencyConsumer;
36  
37    /**
38     * Instantiates a new simple object provider.
39     *
40     * @param provides the provides
41     */
42    public SimpleObjectProvider(@Nullable T provides) {
43      this(provides, null);
44    }
45  
46    /**
47     * Instantiates a new simple object provider.
48     *
49     * <p>If a dependency consumer is set, the given consumer will be invoked when
50     * {@link ObjectProvider#ifAvailable(Consumer)} or {@link ObjectProvider#ifUnique(Consumer)} is called.
51     *
52     * @param provides the provides
53     * @param dependencyConsumer the dependency consumer
54     */
55    public SimpleObjectProvider(@Nullable T provides, @Nullable Consumer<T> dependencyConsumer) {
56      this.provides = provides;
57      this.dependencyConsumer = dependencyConsumer;
58    }
59  
60    private void callConsumer(Consumer<T> originalConsumer) {
61      if (provides == null) {
62        return;
63      }
64      if (dependencyConsumer != null) {
65        dependencyConsumer.accept(provides);
66      } else {
67        originalConsumer.accept(provides);
68      }
69    }
70  
71    @NonNull
72    @Override
73    public T getObject(@NonNull Object... objects) throws BeansException {
74      if (provides == null) {
75        throw new NoSuchBeanDefinitionException("Mocked object provider has no bean.");
76      }
77      return provides;
78    }
79  
80    @Override
81    public void ifAvailable(@NonNull Consumer<T> dependencyConsumer) throws BeansException {
82      callConsumer(dependencyConsumer);
83    }
84  
85    @Override
86    public void ifUnique(@NonNull Consumer<T> dependencyConsumer) throws BeansException {
87      callConsumer(dependencyConsumer);
88    }
89  
90    @Override
91    public T getIfAvailable() throws BeansException {
92      return provides;
93    }
94  
95    @Override
96    public T getIfUnique() throws BeansException {
97      return provides;
98    }
99  
100   @NonNull
101   @Override
102   public T getObject() throws BeansException {
103     if (provides == null) {
104       throw new NoSuchBeanDefinitionException("Mocked object provider has no bean.");
105     }
106     return provides;
107   }
108 }