Switch to Objects.requireNonNull
[mdsal.git] / binding2 / mdsal-binding2-generator-util / src / main / java / org / opendaylight / mdsal / binding / javav2 / generator / util / AbstractBaseType.java
1 /*
2  * Copyright (c) 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.mdsal.binding.javav2.generator.util;
10
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import java.util.Objects;
15 import org.opendaylight.mdsal.binding.javav2.generator.context.ModuleContext;
16 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
17
18 /**
19  * This class represents ancestor for other <code>Type</code>s.
20  */
21 @Beta
22 public abstract class AbstractBaseType implements Type {
23
24     /**
25      * Name of the package to which this <code>Type</code> belongs.
26      */
27     protected final String packageName;
28
29     /**
30      * Name of this <code>Type</code>.
31      */
32     protected final String name;
33
34     /**
35      * Constructs the instance of this class with the concrete package name type
36      * name.
37      *
38      * @param pkName
39      *            string with the package name to which this <code>Type</code>
40      *            belongs
41      * @param name
42      *            string with the name for this <code>Type</code>
43      */
44     protected AbstractBaseType(final String pkName, final String name, final ModuleContext context) {
45         requireNonNull(pkName, "Package Name for Generated Type cannot be null!");
46         requireNonNull(name, "Name of Generated Type cannot be null!");
47         this.packageName = JavaIdentifierNormalizer.normalizeFullPackageName(pkName);
48         requireNonNull(context,
49             "In case of not having identifiers normalized, ModuleContext instance must be provided.");
50         this.name = JavaIdentifierNormalizer.normalizeClassIdentifier(pkName, name, context);
51     }
52
53     /**
54      * Constructs the instance of this class with the concrete package name type
55      * name.
56      *
57      * @param pkName
58      *            string with the package name to which this <code>Type</code>
59      *            belongs
60      * @param name
61      *            string with the name for this <code>Type</code>
62      * @param isNormalized
63      *            true if pkName and name are normalized
64      */
65     protected AbstractBaseType(final String pkName, final String name, final boolean isNormalized,
66             final ModuleContext context) {
67         requireNonNull(pkName, "Package Name for Generated Type cannot be null!");
68         requireNonNull(name, "Name of Generated Type cannot be null!");
69         if (isNormalized) {
70             this.packageName = pkName;
71             this.name = name;
72         } else {
73             this.packageName = JavaIdentifierNormalizer.normalizeFullPackageName(pkName);
74             requireNonNull(context,
75                 "In case of not having identifiers normalized, ModuleContext instance must be provided.");
76             this.name = JavaIdentifierNormalizer.normalizeClassIdentifier(pkName, name, context);
77         }
78     }
79
80     protected AbstractBaseType(final String pkName, final String name, final boolean isPkNameNormalized,
81             final boolean isTypeNormalized, final ModuleContext context) {
82         requireNonNull(pkName, "Package Name for Generated Type cannot be null!");
83         requireNonNull(name, "Name of Generated Type cannot be null!");
84         if (isPkNameNormalized) {
85             this.packageName = pkName;
86         } else {
87             this.packageName = JavaIdentifierNormalizer.normalizeFullPackageName(pkName);
88         }
89
90         if (isTypeNormalized) {
91             this.name = name;
92         } else {
93             requireNonNull(context,
94                 "In case of not having identifiers normalized ModuleContext instance must be provided.");
95             this.name = JavaIdentifierNormalizer.normalizeClassIdentifier(pkName, name, context);
96         }
97     }
98
99     @Override
100     public int hashCode() {
101         return Objects.hash(this.name, this.packageName);
102     }
103
104     @Override
105     public boolean equals(final Object obj) {
106         if (this == obj) {
107             return true;
108         }
109         if (obj == null) {
110             return false;
111         }
112         if (!(obj instanceof Type)) {
113             return false;
114         }
115         final Type other = (Type) obj;
116         return Objects.equals(this.name, other.getName()) && Objects.equals(this.packageName, other.getPackageName());
117     }
118
119
120     @Override
121     public String toString() {
122         if (this.packageName.isEmpty()) {
123             return "Type (" + this.name + ")";
124         }
125         return "Type (" + this.packageName + "." + this.name + ")";
126     }
127
128     @Override
129     public String getPackageName() {
130         return this.packageName;
131     }
132
133     @Override
134     public String getName() {
135         return this.name;
136     }
137
138     @Override
139     public String getFullyQualifiedName() {
140         if (this.packageName.isEmpty()) {
141             return this.name;
142         } else {
143             return this.packageName + "." + this.name;
144         }
145     }
146 }