2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.extension.api;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.HashSet;
14 import org.opendaylight.yangtools.yang.binding.Augmentable;
15 import org.opendaylight.yangtools.yang.binding.Augmentation;
18 * Provides augmentation resolving upon given {@link Augmentable}.
19 * Used {@link #classes} share the same {@link Augmentable}.
21 * <b>Usage:</b> in case there is {@link Augmentable} which might contain
22 * multiple {@link Augmentation}s depending on origin. And those {@link Augmentation}s
23 * are sharing the same grouping so that they could be processed in the same way.
26 * @param <T> Augmentable
28 public class GroupingResolver<G, T extends Augmentable<T>> {
30 Class<G> commonInterface;
31 Set<Class<? extends Augmentation<T>>> classes;
33 public GroupingResolver(Class<G> commonInterface) {
34 this.commonInterface = commonInterface;
35 classes = new HashSet<>();
39 * Adds an augmentation class.
41 * @param cls equivalent augmentation class
42 * @return this for chaining
44 public <X extends Augmentation<T>> GroupingResolver<G, T> add(Class<X> cls) {
45 Preconditions.checkArgument(commonInterface.isAssignableFrom(cls));
51 * Sets the augmentation classes.
53 * @param clses set of equivalent augmentation classes
55 public void setAugmentations(Set<Class<? extends Augmentation<T>>> clses) {
56 for (Class<? extends Augmentation<T>> cls : clses) {
57 Preconditions.checkArgument(commonInterface.isAssignableFrom(cls));
63 * Gets the extension for the given data.
66 * @return shared grouping
68 @SuppressWarnings("unchecked")
69 public Optional<G> getExtension(T data) {
70 for (Class<? extends Augmentation<T>> cls : classes) {
71 Augmentation<T> potential = data.getAugmentation(cls);
72 if (potential != null) {
73 return Optional.of((G) potential);
76 return Optional.absent();