Change all the package names from infrautils to odlguice
[odlguice.git] / inject / inject-guice-testutils / src / main / java / org / opendaylight / odlguice / inject / guice / testutils / AbstractGuiceJsr250Module.java
1 /*
2  * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.odlguice.inject.guice.testutils;
9
10 /**
11  * Convenience Guice module support class, which installs the
12  * {@link AnnotationsModule}, and handles exceptions as the
13  * {@link AbstractCheckedModule} does.
14  *
15  * @author Michael Vorburger.ch
16  */
17 public abstract class AbstractGuiceJsr250Module extends AbstractCheckedModule {
18
19     @Override
20     @SuppressWarnings("checkstyle:IllegalCatch")
21     protected final void checkedConfigure() throws Exception {
22         install(new AnnotationsModule());
23         configureBindings();
24     }
25
26     protected abstract void configureBindings() throws Exception;
27
28     /**
29      * Binds instance to both the interfaceClass as well as the implementationClass.
30      *
31      * @param interfaceClass class type of an interface
32      * @param implementationClass class type of implementing class
33      * @param instance an instance implementing both interfaceClass & implementationClass
34      * @param <T> type of interfaceClass
35      */
36     @SuppressWarnings("unchecked")
37     protected <T> void bindTypesToInstance(Class<T> interfaceClass, Class<? extends T> implementationClass,
38             T instance) {
39         if (implementationClass.equals(interfaceClass)) {
40             throw new IllegalArgumentException("interfaceClass should not be the same as implementationClass: "
41                     + interfaceClass + "; " + implementationClass);
42         }
43         bind(interfaceClass).toInstance(instance);
44         bind((Class<T>) implementationClass).toInstance(instance);
45     }
46
47 }