Fix Jdk8 compatibility
[openflowplugin.git] / extension / openflowplugin-extension-api / src / main / java / org / opendaylight / openflowplugin / extension / api / GroupingResolver.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 java.util.HashSet;
11 import java.util.Set;
12
13 import org.opendaylight.yangtools.yang.binding.Augmentable;
14 import org.opendaylight.yangtools.yang.binding.Augmentation;
15
16 import com.google.common.base.Optional;
17 import com.google.common.base.Preconditions;
18
19 /**
20  * Provides augmentation resolving upon given {@link Augmentable}.
21  * Used {@link #classes} share the same {@link Augmentable}.
22  * <br>
23  * <b>Usage:</b> in case there is {@link Augmentable} which might contain
24  * multiple {@link Augmentation}s depending on origin. And those {@link Augmentation}s 
25  * are sharing the same grouping so that they could be processed in the same way.
26  * 
27  * @param <G> grouping
28  * @param <T>
29  */
30 public class GroupingResolver<G, T extends Augmentable<T>> {
31
32     Class<G> commonInterface;
33     Set<Class<? extends Augmentation<T>>> classes;
34
35     /**
36      * @param commonInterface
37      */
38     public GroupingResolver(Class<G> commonInterface) {
39         this.commonInterface = commonInterface;
40         classes = new HashSet<>();
41     }
42
43     /**
44      * @param cls equivalent augmentation class
45      * @return this for chaining
46      */
47     public <X extends Augmentation<T>> GroupingResolver<G, T> add(Class<X> cls) {
48         Preconditions.checkArgument(commonInterface.isAssignableFrom(cls));
49         classes.add(cls);
50         return this;
51     }
52
53     /**
54      * @param clses set of equivalent augmentation classes
55      */
56     public void setAugmentations(Set<Class<? extends Augmentation<T>>> clses) {
57         for (Class<? extends Augmentation<T>> cls : clses) {
58             Preconditions.checkArgument(commonInterface.isAssignableFrom(cls));
59         }
60         classes = clses;
61     }
62
63     /**
64      * @param data
65      * @return shared grouping
66      */
67     @SuppressWarnings("unchecked")
68     public Optional<G> getExtension(T data) {
69         for (Class<? extends Augmentation<T>> cls : classes) {
70             Augmentation<T> potential = data.getAugmentation(cls);
71             if (potential != null) {
72                 return Optional.of((G) potential);
73             }
74         }
75         return Optional.absent();
76     }
77 }