0aa041bab3f148e8661e2cb506ab751b7978721a
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / spi / AdapterBuilder.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, 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.controller.md.sal.binding.spi;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ClassToInstanceMap;
12 import com.google.common.collect.ImmutableClassToInstanceMap;
13 import com.google.common.collect.MutableClassToInstanceMap;
14 import java.util.Set;
15 import org.opendaylight.yangtools.concepts.Builder;
16
17 @Deprecated
18 public abstract class AdapterBuilder<T,D> implements Builder<T> {
19
20     private final ClassToInstanceMap<D> delegates = MutableClassToInstanceMap.create();
21
22     public abstract Set<? extends Class<? extends D>> getRequiredDelegates();
23
24     protected abstract T createInstance(ClassToInstanceMap<D> classToInstanceMap);
25
26     private void checkAllRequiredServices() {
27         for (final Class<? extends D> type : getRequiredDelegates()) {
28             Preconditions.checkState(delegates.get(type) != null, "Requires service %s is not defined.",type);
29         }
30     }
31
32     public final <V extends D> void addDelegate(final Class<V> type,final D impl) {
33         delegates.put(type,impl);
34     }
35
36     @Override
37     public final  T build() {
38         checkAllRequiredServices();
39         return createInstance(ImmutableClassToInstanceMap.<D,D>copyOf(delegates));
40     }
41 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.