Bug 2364: Migrated Binding MD-SAL to not use composites nodes
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / spi / AdapterLoader.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.Optional;
11 import com.google.common.cache.CacheLoader;
12 import javax.annotation.Nonnull;
13 import javax.annotation.Nullable;
14
15 public abstract class AdapterLoader<T, D> extends CacheLoader<Class<? extends T>, Optional<T>> {
16
17     @Override
18     public Optional<T> load(final Class<? extends T> key) {
19
20         final AdapterBuilder<? extends T, D> builder = createBuilder(key);
21         for(final Class<? extends D> reqDeleg : builder.getRequiredDelegates()) {
22             final D deleg = getDelegate(reqDeleg);
23             if(deleg != null) {
24                 builder.addDelegate(reqDeleg,deleg);
25             } else {
26                 return Optional.absent();
27             }
28         }
29         return  Optional.<T>of(builder.build());
30     }
31
32     protected abstract @Nullable D getDelegate(Class<? extends D> reqDeleg);
33
34     protected abstract @Nonnull AdapterBuilder<? extends T, D> createBuilder(Class<? extends T> key) throws IllegalArgumentException;
35
36 }

©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.