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