sberlin | 132a5db | 2011-06-05 18:32:05 +0000 | [diff] [blame] | 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 java.util.List;
|
| 20 |
|
| 21 | import com.google.inject.Binder;
|
| 22 | import com.google.inject.Key;
|
sberlin | d9c913a | 2011-06-26 21:02:54 +0000 | [diff] [blame^] | 23 | import com.google.common.collect.ImmutableList;
|
sberlin | 132a5db | 2011-06-05 18:32:05 +0000 | [diff] [blame] | 24 | import com.google.inject.matcher.Matcher;
|
| 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 Key<?>> keyMatcher;
|
| 37 | private final List<ProvisionListener> listeners;
|
| 38 |
|
| 39 | ProvisionListenerBinding(Object source,
|
| 40 | Matcher<? super Key<?>> typeMatcher,
|
| 41 | ProvisionListener[] listeners) {
|
| 42 | this.source = source;
|
| 43 | this.keyMatcher = typeMatcher;
|
| 44 | this.listeners = ImmutableList.of(listeners);
|
| 45 | }
|
| 46 |
|
| 47 | /** Returns the registered listeners. */
|
| 48 | public List<ProvisionListener> getListeners() {
|
| 49 | return listeners;
|
| 50 | }
|
| 51 |
|
| 52 | /** Returns the key matcher which chooses which keys the listener should be notified of. */
|
| 53 | public Matcher<? super Key<?>> getKeyMatcher() {
|
| 54 | return keyMatcher;
|
| 55 | }
|
| 56 |
|
| 57 | public Object getSource() {
|
| 58 | return source;
|
| 59 | }
|
| 60 |
|
| 61 | public <R> R acceptVisitor(ElementVisitor<R> visitor) {
|
| 62 | return visitor.visit(this);
|
| 63 | }
|
| 64 |
|
| 65 | public void applyTo(Binder binder) {
|
| 66 | binder.withSource(getSource()).bindListener(keyMatcher,
|
| 67 | listeners.toArray(new ProvisionListener[listeners.size()]));
|
| 68 | }
|
| 69 | }
|