1/**
2 * Copyright (C) 2009 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
17package com.google.inject.spi;
18
19import com.google.inject.Binder;
20import com.google.inject.TypeLiteral;
21import com.google.inject.matcher.Matcher;
22
23/**
24 * Binds types (picked using a Matcher) to an type listener. Registrations are created explicitly in
25 * a module using {@link com.google.inject.Binder#bindListener(Matcher, TypeListener)} statements:
26 *
27 * <pre>
28 *     register(only(new TypeLiteral&lt;PaymentService&lt;CreditCard>>() {}), listener);</pre>
29 *
30 * @author jessewilson@google.com (Jesse Wilson)
31 * @since 2.0
32 */
33public final class TypeListenerBinding implements Element {
34
35  private final Object source;
36  private final Matcher<? super TypeLiteral<?>> typeMatcher;
37  private final TypeListener listener;
38
39  TypeListenerBinding(Object source, TypeListener listener,
40      Matcher<? super TypeLiteral<?>> typeMatcher) {
41    this.source = source;
42    this.listener = listener;
43    this.typeMatcher = typeMatcher;
44  }
45
46  /** Returns the registered listener. */
47  public TypeListener getListener() {
48    return listener;
49  }
50
51  /** Returns the type matcher which chooses which types the listener should be notified of. */
52  public Matcher<? super TypeLiteral<?>> getTypeMatcher() {
53    return typeMatcher;
54  }
55
56  public Object getSource() {
57    return source;
58  }
59
60  public <T> T acceptVisitor(ElementVisitor<T> visitor) {
61    return visitor.visit(this);
62  }
63
64  public void applyTo(Binder binder) {
65    binder.withSource(getSource()).bindListener(typeMatcher, listener);
66  }
67}
68