Coverage Report - org.apache.onami.guava.eventbus.EventBusModule
 
Classes in this File Line Coverage Branch Coverage Complexity
EventBusModule
100%
3/3
50%
1/2
1
EventBusModule$1
100%
8/8
50%
1/2
1
EventBusModule$1$1
100%
3/3
N/A
1
EventBusModule$1$1$1
100%
3/3
N/A
1
 
 1  
 package org.apache.onami.guava.eventbus;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import static com.google.common.base.Preconditions.checkArgument;
 23  
 import static com.google.inject.matcher.Matchers.any;
 24  
 
 25  
 import static com.google.inject.name.Names.named;
 26  
 
 27  
 import com.google.common.eventbus.EventBus;
 28  
 import com.google.inject.AbstractModule;
 29  
 import com.google.inject.TypeLiteral;
 30  
 import com.google.inject.matcher.Matcher;
 31  
 import com.google.inject.spi.InjectionListener;
 32  
 import com.google.inject.spi.TypeEncounter;
 33  
 import com.google.inject.spi.TypeListener;
 34  
 
 35  
 /**
 36  
  * This class was originally developed by <a href="http://spin.atomicobject.com/author/dewind/">Justin DeWind</a>
 37  
  * on <a href="http://spin.atomicobject.com/2012/01/13/the-guava-eventbus-on-guice/">Atomicobject</a> blog, under the
 38  
  * terms of the MIT License.
 39  
  */
 40  3
 public abstract class EventBusModule
 41  
     extends AbstractModule
 42  
 {
 43  
 
 44  
     protected BusMatcher bindBus( final String identifier )
 45  
     {
 46  1
         checkArgument( identifier != null, "Event bus identifier must be not null" );
 47  
 
 48  1
         return new BusMatcher()
 49  1
         {
 50  
 
 51  
             public void toAnyBoundClass()
 52  
             {
 53  1
                 to( any() );
 54  1
             }
 55  
 
 56  
             public void to( Matcher<? super TypeLiteral<?>> matcher )
 57  
             {
 58  1
                 checkArgument( matcher != null, "Event bus matcher must be not null" );
 59  
 
 60  1
                 final EventBus eventBus = new EventBus( identifier );
 61  
 
 62  1
                 bind( EventBus.class ).annotatedWith( named( identifier ) ).toInstance( eventBus );
 63  
 
 64  1
                 bindListener( matcher, new TypeListener()
 65  1
                 {
 66  
                     public <I> void hear( TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter )
 67  
                     {
 68  3
                         typeEncounter.register( new InjectionListener<I>()
 69  3
                         {
 70  
                             public void afterInjection( I injectee )
 71  
                             {
 72  4
                                 eventBus.register( injectee );
 73  4
                             }
 74  
                         } );
 75  3
                     }
 76  
                 } );
 77  1
             }
 78  
 
 79  
         };
 80  
     }
 81  
 
 82  
 }