1 /** 2 * Copyright (C) 2011 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.common.collect.ImmutableList; 20 import com.google.inject.Binder; 21 import com.google.inject.Binding; 22 import com.google.inject.matcher.Matcher; 23 24 import java.util.List; 25 26 /** 27 * Binds keys (picked using a Matcher) to a provision listener. Listeners are created explicitly in 28 * a module using {@link Binder#bindListener(Matcher, ProvisionListener...)} statements: 29 * 30 * @author sameb@google.com (Sam Berlin) 31 * @since 4.0 32 */ 33 public final class ProvisionListenerBinding implements Element { 34 35 private final Object source; 36 private final Matcher<? super Binding<?>> bindingMatcher; 37 private final List<ProvisionListener> listeners; 38 ProvisionListenerBinding(Object source, Matcher<? super Binding<?>> bindingMatcher, ProvisionListener[] listeners)39 ProvisionListenerBinding(Object source, 40 Matcher<? super Binding<?>> bindingMatcher, 41 ProvisionListener[] listeners) { 42 this.source = source; 43 this.bindingMatcher = bindingMatcher; 44 this.listeners = ImmutableList.copyOf(listeners); 45 } 46 47 /** Returns the registered listeners. */ getListeners()48 public List<ProvisionListener> getListeners() { 49 return listeners; 50 } 51 52 /** 53 * Returns the binding matcher which chooses which bindings the listener should be notified of. 54 */ getBindingMatcher()55 public Matcher<? super Binding<?>> getBindingMatcher() { 56 return bindingMatcher; 57 } 58 getSource()59 public Object getSource() { 60 return source; 61 } 62 acceptVisitor(ElementVisitor<R> visitor)63 public <R> R acceptVisitor(ElementVisitor<R> visitor) { 64 return visitor.visit(this); 65 } 66 applyTo(Binder binder)67 public void applyTo(Binder binder) { 68 binder.withSource(getSource()).bindListener(bindingMatcher, 69 listeners.toArray(new ProvisionListener[listeners.size()])); 70 } 71 } 72