Fix checkstyle reported by odlparent-3.0.0
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / mapping / attributes / AttributeIfcSwitchStatement.java
1 /*
2  * Copyright (c) 2015, 2017 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
9 package org.opendaylight.controller.config.facade.xml.mapping.attributes;
10
11 import javax.management.openmbean.ArrayType;
12 import javax.management.openmbean.CompositeType;
13 import javax.management.openmbean.OpenType;
14 import javax.management.openmbean.SimpleType;
15 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
16 import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute;
17 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
18 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
19 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
20 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
21 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
22
23 public abstract class AttributeIfcSwitchStatement<T> {
24
25     private AttributeIfc lastAttribute;
26
27     @SuppressWarnings("checkstyle:avoidHidingCauseException")
28     public T switchAttribute(final AttributeIfc attributeIfc) {
29
30         this.lastAttribute = attributeIfc;
31
32         OpenType<?> openType = attributeIfc.getOpenType();
33
34         if (attributeIfc instanceof JavaAttribute) {
35             try {
36                 if (((JavaAttribute) attributeIfc).getTypeDefinition() instanceof BinaryTypeDefinition) {
37                     return caseJavaBinaryAttribute(openType);
38                 } else if (((JavaAttribute) attributeIfc).isUnion()) {
39                     return caseJavaUnionAttribute(openType);
40                 } else if (((JavaAttribute) attributeIfc).isIdentityRef()) {
41                     return caseJavaIdentityRefAttribute(openType);
42                 } else if (((JavaAttribute) attributeIfc).isEnum()) {
43                     return caseJavaEnumAttribute(openType);
44                 } else {
45                     return caseJavaAttribute(openType);
46                 }
47             } catch (final UnknownOpenTypeException e) {
48                 throw getIllegalArgumentException(attributeIfc);
49             }
50
51         } else if (attributeIfc instanceof DependencyAttribute) {
52             return caseDependencyAttribute(((DependencyAttribute) attributeIfc).getOpenType());
53         } else if (attributeIfc instanceof ListAttribute) {
54             return caseListAttribute((ArrayType<?>) openType);
55         } else if (attributeIfc instanceof ListDependenciesAttribute) {
56             return caseListDependeciesAttribute((ArrayType<?>) openType);
57         } else if (attributeIfc instanceof TOAttribute) {
58             return caseTOAttribute(((TOAttribute) attributeIfc).getOpenType());
59         }
60
61         throw getIllegalArgumentException(attributeIfc);
62     }
63
64     public AttributeIfc getLastAttribute() {
65         return lastAttribute;
66     }
67
68     protected T caseJavaIdentityRefAttribute(final OpenType<?> openType) {
69         return caseJavaAttribute(openType);
70     }
71
72     protected T caseJavaUnionAttribute(final OpenType<?> openType) {
73         return caseJavaAttribute(openType);
74     }
75
76     protected T caseJavaEnumAttribute(final OpenType<?> openType) {
77         return caseJavaAttribute(openType);
78     }
79
80     protected T caseJavaBinaryAttribute(final OpenType<?> openType) {
81         return caseJavaAttribute(openType);
82     }
83
84     private IllegalArgumentException getIllegalArgumentException(final AttributeIfc attributeIfc) {
85         return new IllegalArgumentException("Unknown attribute type " + attributeIfc.getClass() + ", " + attributeIfc
86                 + " with open type:" + attributeIfc.getOpenType());
87     }
88
89     public final T caseJavaAttribute(final OpenType<?> openType) {
90         if (openType instanceof SimpleType<?>) {
91             return caseJavaSimpleAttribute((SimpleType<?>) openType);
92         } else if (openType instanceof ArrayType<?>) {
93             return caseJavaArrayAttribute((ArrayType<?>) openType);
94         } else if (openType instanceof CompositeType) {
95             return caseJavaCompositeAttribute((CompositeType) openType);
96         }
97
98         throw new UnknownOpenTypeException("Unknown attribute open type " + openType);
99     }
100
101     protected abstract T caseJavaSimpleAttribute(SimpleType<?> openType);
102
103     protected abstract T caseJavaArrayAttribute(ArrayType<?> openType);
104
105     protected abstract T caseJavaCompositeAttribute(CompositeType openType);
106
107     protected abstract T caseDependencyAttribute(SimpleType<?> attributeIfc);
108
109     protected abstract T caseTOAttribute(CompositeType openType);
110
111     protected abstract T caseListAttribute(ArrayType<?> openType);
112
113     protected abstract T caseListDependeciesAttribute(ArrayType<?> openType);
114
115     private static class UnknownOpenTypeException extends RuntimeException {
116         private static final long serialVersionUID = 1L;
117
118         UnknownOpenTypeException(final String message) {
119             super(message);
120         }
121     }
122 }