2 * Copyright © 2017 Red Hat, Inc. and others.
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.genius.mdsalutil.matches;
11 import org.opendaylight.genius.mdsalutil.MatchInfo;
12 import org.opendaylight.genius.utils.SuperTypeUtil;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
14 import org.opendaylight.yangtools.concepts.Builder;
15 import org.opendaylight.yangtools.yang.binding.DataObject;
18 * Helper for matches (this is designed to be absorbed into MatchInfo once we've cleaned up downstream users).
20 public abstract class MatchInfoHelper<T extends DataObject, B extends Builder<T>> extends MatchInfo {
21 private final Class<B> builderClass;
24 builderClass = SuperTypeUtil.getTypeParameter(getClass(), 1);
28 public void createInnerMatchBuilder(Map<Class<?>, Object> mapMatchBuilder) {
29 populateBuilder((B) mapMatchBuilder.computeIfAbsent(builderClass, key -> {
31 return builderClass.newInstance();
32 } catch (InstantiationException | IllegalAccessException e) {
33 throw new IllegalStateException("Unable to create an instance of " + builderClass, e);
39 public void setMatch(MatchBuilder matchBuilder, Map<Class<?>, Object> mapMatchBuilder) {
40 B builder = (B) mapMatchBuilder.remove(builderClass);
42 if (builder != null) {
43 applyValue(matchBuilder, builder.build());
47 protected abstract void applyValue(MatchBuilder matchBuilder, T value);
49 protected abstract void populateBuilder(B builder);
52 public boolean equals(Object other) {
56 if (other == null || getClass() != other.getClass()) {
60 MatchInfoHelper<?, ?> that = (MatchInfoHelper<?, ?>) other;
62 return builderClass != null ? builderClass.equals(that.builderClass) : that.builderClass == null;
66 public int hashCode() {
67 return builderClass != null ? builderClass.hashCode() : 0;