Fix checkstyle warnings in yang-jmx-generator.
[controller.git] / opendaylight / config / yang-jmx-generator / src / main / java / org / opendaylight / controller / config / yangjmxgenerator / attribute / ListAttribute.java
1 /*
2  * Copyright (c) 2013 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.controller.config.yangjmxgenerator.attribute;
9
10 import java.util.List;
11 import javax.management.openmbean.ArrayType;
12 import javax.management.openmbean.OpenDataException;
13 import javax.management.openmbean.OpenType;
14 import org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper;
15 import org.opendaylight.yangtools.binding.generator.util.Types;
16 import org.opendaylight.yangtools.sal.binding.model.api.Type;
17 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
20
21 public class ListAttribute extends AbstractAttribute implements TypedAttribute {
22
23     private final String nullableDescription, nullableDefault;
24     private final TypedAttribute innerAttribute;
25
26     public static ListAttribute create(ListSchemaNode node,
27             TypeProviderWrapper typeProvider, String packageName) {
28
29         TOAttribute innerAttribute = TOAttribute.create(node, typeProvider, packageName);
30
31         return new ListAttribute(node, innerAttribute, node.getDescription());
32     }
33
34     public static ListAttribute create(LeafListSchemaNode node,
35             TypeProviderWrapper typeProvider) {
36
37         JavaAttribute innerAttribute = new JavaAttribute(node, typeProvider);
38
39         return new ListAttribute(node, innerAttribute, node.getDescription());
40     }
41
42     ListAttribute(DataSchemaNode attrNode, TypedAttribute innerAttribute,
43             String description) {
44         super(attrNode);
45         this.nullableDescription = description;
46         this.innerAttribute = innerAttribute;
47         this.nullableDefault = null;
48     }
49
50     @Override
51     public String getNullableDescription() {
52         return nullableDescription;
53     }
54
55     @Override
56     public String getNullableDefault() {
57         return nullableDefault;
58     }
59
60     public AttributeIfc getInnerAttribute() {
61         return innerAttribute;
62     }
63
64     @Override
65     public String toString() {
66         return "ListAttribute{" + getAttributeYangName() + "," + "to="
67                 + innerAttribute + '}';
68     }
69
70     @Override
71     public int hashCode() {
72         int result = super.hashCode();
73         result = 31
74                 * result
75                 + (nullableDescription != null ? nullableDescription.hashCode()
76                         : 0);
77         result = 31 * result
78                 + (nullableDefault != null ? nullableDefault.hashCode() : 0);
79         return result;
80     }
81
82     @Override
83     public boolean equals(Object o) {
84         if (this == o) {
85             return true;
86         }
87         if (o == null || getClass() != o.getClass()) {
88             return false;
89         }
90         if (!super.equals(o)) {
91             return false;
92         }
93
94         ListAttribute that = (ListAttribute) o;
95
96         if (nullableDefault != null ? !nullableDefault
97                 .equals(that.nullableDefault) : that.nullableDefault != null) {
98             return false;
99         }
100         if (nullableDescription != null ? !nullableDescription
101                 .equals(that.nullableDescription)
102                 : that.nullableDescription != null) {
103             return false;
104         }
105
106         return true;
107     }
108
109
110     @Override
111     public Type getType() {
112         return Types.parameterizedTypeFor(Types.typeForClass(List.class), innerAttribute.getType());
113     }
114
115     @Override
116     public ArrayType<?> getOpenType() {
117         OpenType<?> innerOpenType = innerAttribute.getOpenType();
118         return constructArrayType(innerOpenType);
119     }
120
121     static ArrayType<?> constructArrayType(OpenType<?> innerOpenType){
122         try {
123             return new ArrayType<>(1, innerOpenType);
124         } catch (OpenDataException e) {
125             throw new RuntimeException("Unable to create " + ArrayType.class
126                     + " with inner element of type " + innerOpenType, e);
127         }
128     }
129
130 }