View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.onami.factoryannotation;
20  
21  import java.lang.annotation.Annotation;
22  
23  import com.google.inject.Injector;
24  
25  /**
26   * The interface to describe a FactoryAnnotationProvider bound by {@link FactoryAnnotationModule} to the Guice
27   * {@link Injector}. <h3>Implementing a FactoryAnnotationProvider</h3> An {@link FactoryAnnotationProvider} is a special
28   * provider to return the actual instance of a injection type in combination with the actual factory annotation at the
29   * injection point. At this point we omit the UserEntity class. It can be a simple POJO, a JPA entity or whatever class
30   * you can image.
31   * 
32   * <pre>
33   * public class UserEntityProvider implements FactoryAnnotationProvider<UserEntity, User> {
34   * 
35   *     {@literal @}Override
36   *     public UserEntity buildValue(final User annotation) {
37   *         return getRealEntityById(annotation.byId());
38   *     }
39   *     
40   *     {@literal @}Override
41   *     public Class<UserEntity> getInjectionType() {
42   *         return UserEntity.class;
43   *     }
44   * 
45   *     private UserEntity getRealEntityById(int id) {
46   *         ...
47   *     }
48   *     
49   * }
50   * </pre>
51   * 
52   * @param <T> The type to be injected
53   * @param <A> Annotation type where injection happens
54   */
55  public interface FactoryAnnotationProvider<T, A extends Annotation>
56  {
57  
58      /**
59       * Returns the type of injection points that should be captured for provision.
60       */
61      Class<T> getInjectionType();
62  
63      /**
64       * <p>
65       * The implementation of this method is responsible for building / requesting the instance that is referenced by the
66       * actual injection points annotation to the annotation type bound at configuration time.
67       * </p>
68       * <p>
69       * For more information see {@link FactoryAnnotationModule}.
70       * </p>
71       * 
72       * @param annotation The actual annotation of the bound annotation type at the provision injection point.
73       * @return The actual instance for a given annotation based value.
74       */
75      T buildValue( A annotation );
76  
77  }