1 /**
2  * Copyright (C) 2010 Google Inc.
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 com.google.inject.spi;
18 
19 import com.google.inject.Binding;
20 import com.google.inject.Provider;
21 
22 /**
23  * A Provider that is part of an extension which supports a custom
24  * BindingTargetVisitor.
25  * <p>
26  * When an extension binds a provider instance, the provider can implement this
27  * interface to allow users using the
28  * {@link Binding#acceptTargetVisitor(BindingTargetVisitor)} method to visit a
29  * custom visitor designed for that extension. A typical implementation within
30  * the extension would look like
31  * <pre>
32  * &lt;V, B> V acceptExtensionVisitor(BindingTargetVisitor&lt;B, V> visitor, ProviderInstanceBinding&lt;? extends B> binding) {
33  *   if(visitor instanceof MyCustomExtensionVisitor) {
34  *     return ((MyCustomExtensionVisitor&lt;B, V>)visitor).visitCustomExtension(customProperties, binding);
35  *   } else {
36  *     return visitor.visit(binding);
37  *   }
38  * }</pre>
39  * 'MyCustomExtensionVisitor' in the example above would be an interface the
40  * extension provides that users can implement in order to be notified of custom
41  * extension information. These visitor interfaces must extend from
42  * BindingTargetVisitor.
43  *
44  * @since 3.0
45  * @author sameb@google.com (Sam Berlin)
46  */
47 public interface ProviderWithExtensionVisitor<T> extends Provider<T> {
48 
49   /**
50    * Instructs the extension determine if the visitor is an instance of a custom
51    * extension visitor, and if so, visit it using that method. If the visitor is
52    * not an instance of the custom extension visitor, this method <b>MUST</b>
53    * call visitor.visit(binding).
54    * <p>
55    * Due to issues with generics, the type parameters of this method do not
56    * relate to the type of the provider. In practice, the 'B' type will always
57    * be a supertype of 'T'.
58    */
acceptExtensionVisitor(BindingTargetVisitor<B, V> visitor, ProviderInstanceBinding<? extends B> binding)59   <B, V> V acceptExtensionVisitor(BindingTargetVisitor<B, V> visitor,
60       ProviderInstanceBinding<? extends B> binding);
61 }
62