Merge "Remove Optional.ofNullable() antipatterns"
[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 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Provides augmentation resolving upon given {@link Augmentable}.
22  * Used {@link Augmentation}s do not share {@link Augmentable}.
23  * <br>
24  * <b>Usage:</b> in case there are multiple {@link Augmentable} classes which might contain
25  * corresponding {@link Augmentation}s (1:1..n binding). And those {@link Augmentation}s
26  * are sharing the same grouping so that they could be processed in the same way.
27  *
28  * @param <G> grouping
29  */
30 public class GroupingLooseResolver<G> {
31     private static final Logger LOG = LoggerFactory.getLogger(GroupingLooseResolver.class);
32
33     private final Class<G> commonInterface;
34     private final Set<Class<? extends Augmentation<?>>> classes;
35
36     /**
37      * Constructor.
38      *
39      * @param commonInterface common interface
40      */
41     public GroupingLooseResolver(Class<G> commonInterface) {
42         this.commonInterface = commonInterface;
43         classes = new HashSet<>();
44     }
45
46     /**
47      * Get augmentation classes.
48      *
49      * @return list of augmentation classes
50      */
51     public Set<Class<? extends Augmentation<?>>> getClasses() {
52         return classes;
53     }
54
55     /**
56      * Adds an augmentation class.
57      *
58      * @param cls equivalent augmentation class
59      * @return this for chaining
60      */
61     public GroupingLooseResolver<G> add(Class<? extends Augmentation<?>> cls) {
62         Preconditions.checkArgument(commonInterface.isAssignableFrom(cls),
63                 "oh man! I got " + cls);
64         classes.add(cls);
65         return this;
66     }
67
68     /**
69      * Gets the extension for the give data.
70      *
71      * @param data parameter(data) for getExtension
72      * @param <T> type of data
73      * @return shared grouping
74      */
75     @SuppressWarnings("unchecked")
76     public <T extends Augmentable<T>> Optional<G> getExtension(DataObject data) {
77         // The type of 'data' should really be T for compile-time checking. Several call sites do not pass an
78         // Augmentable DataObject type which would result in a ClassCastException at runtime. This is clearly
79         // broken - those call sites need to be analyzed to determine the correct behavior in order for this method
80         // signature to be changed but for now catch ClassCastException.
81         T guessData;
82         try {
83             guessData = (T) data;
84         } catch (ClassCastException e) {
85             LOG.warn("Cannot cast to Augmentable", e);
86             return Optional.empty();
87         }
88
89         for (Class<? extends Augmentation<?>> cls : classes) {
90             Augmentation<T> potential = guessData
91                     .augmentation((Class<Augmentation<T>>) cls);
92             if (potential != null) {
93                 return Optional.of((G) potential);
94             }
95         }
96
97         return Optional.empty();
98     }
99 }