afa239257d1809d91bf17bb59e9c95bb5fcbb040
[openflowplugin.git] / extension / openflowplugin-extension-api / src / main / java / org / opendaylight / openflowplugin / extension / api / GroupingLooseResolver.java
1 /**
2  * Copyright (c) 2014 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.openflowplugin.extension.api;
9
10 import com.google.common.base.Preconditions;
11 import java.util.HashSet;
12 import java.util.Optional;
13 import java.util.Set;
14 import org.opendaylight.yangtools.yang.binding.Augmentable;
15 import org.opendaylight.yangtools.yang.binding.Augmentation;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17
18 /**
19  * Provides augmentation resolving upon given {@link Augmentable}.
20  * Used {@link Augmentation}s do not share {@link Augmentable}.
21  * <br>
22  * <b>Usage:</b> in case there are multiple {@link Augmentable} classes which might contain
23  * corresponding {@link Augmentation}s (1:1..n binding). And those {@link Augmentation}s
24  * are sharing the same grouping so that they could be processed in the same way.
25  *
26  * @param <G> grouping
27  */
28 public class GroupingLooseResolver<G> {
29
30     private final Class<G> commonInterface;
31     private final Set<Class<? extends Augmentation<?>>> classes;
32
33     /**
34      * Constructor.
35      *
36      * @param commonInterface common interface
37      */
38     public GroupingLooseResolver(Class<G> commonInterface) {
39         this.commonInterface = commonInterface;
40         classes = new HashSet<>();
41     }
42
43     /**
44      * Get augmentation classes.
45      *
46      * @return list of augmentation classes
47      */
48     public Set<Class<? extends Augmentation<?>>> getClasses() {
49         return classes;
50     }
51
52     /**
53      * Adds an augmentation class.
54      *
55      * @param cls equivalent augmentation class
56      * @return this for chaining
57      */
58     public GroupingLooseResolver<G> add(Class<? extends Augmentation<?>> cls) {
59         Preconditions.checkArgument(commonInterface.isAssignableFrom(cls),
60                 "oh man! I got " + cls);
61         classes.add(cls);
62         return this;
63     }
64
65     /**
66      * Gets the extension for the give data.
67      *
68      * @param data expected to match <tt>&lt;T extends Augmentable&lt;T&gt;&gt;</tt>
69      * @return shared grouping
70      */
71     @SuppressWarnings("unchecked")
72     public <T extends Augmentable<T>> Optional<G> getExtension(DataObject data) {
73         T guessData = (T) data;
74
75         for (Class<? extends Augmentation<?>> cls : classes) {
76             Augmentation<T> potential = guessData
77                     .getAugmentation((Class<Augmentation<T>>) cls);
78             if (potential != null) {
79                 return Optional.of((G) potential);
80             }
81         }
82
83         return Optional.empty();
84     }
85 }