Improve NonJavaCharsConverter
[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.model.api.Type;
15
16 /**
17  * This class represents ancestor for other <code>Type</code>s
18  */
19 @Beta
20 public abstract class AbstractBaseType implements Type {
21
22     /**
23      * Name of the package to which this <code>Type</code> belongs.
24      */
25     protected final String packageName;
26
27     /**
28      * Name of this <code>Type</code>.
29      */
30     protected final String name;
31
32     /**
33      * Constructs the instance of this class with the concrete package name type
34      * name.
35      *
36      * @param pkName
37      *            string with the package name to which this <code>Type</code>
38      *            belongs
39      * @param name
40      *            string with the name for this <code>Type</code>
41      */
42     protected AbstractBaseType(final String pkName, final String name) {
43         Preconditions.checkNotNull(pkName, "Package Name for Generated Type cannot be null!");
44         Preconditions.checkNotNull(name, "Name of Generated Type cannot be null!");
45         this.packageName = JavaIdentifierNormalizer.normalizeFullPackageName(pkName);
46         this.name = JavaIdentifierNormalizer.normalizeClassIdentifier(pkName, name);
47     }
48
49     /**
50      * Constructs the instance of this class with the concrete package name type
51      * name.
52      *
53      * @param pkName
54      *            string with the package name to which this <code>Type</code>
55      *            belongs
56      * @param name
57      *            string with the name for this <code>Type</code>
58      * @param isNormalized
59      *            true if pkName and name are normalized
60      */
61     protected AbstractBaseType(final String pkName, final String name, final boolean isNormalized) {
62         Preconditions.checkNotNull(pkName, "Package Name for Generated Type cannot be null!");
63         Preconditions.checkNotNull(name, "Name of Generated Type cannot be null!");
64         if (isNormalized) {
65             this.packageName = pkName;
66             this.name = name;
67         } else {
68             this.packageName = JavaIdentifierNormalizer.normalizeFullPackageName(pkName);
69             this.name = JavaIdentifierNormalizer.normalizeClassIdentifier(pkName, name);
70         }
71     }
72
73     @Override
74     public int hashCode() {
75         return Objects.hash(this.name, this.packageName);
76     }
77
78     @Override
79     public boolean equals(final Object obj) {
80         if (this == obj) {
81             return true;
82         }
83         if (obj == null) {
84             return false;
85         }
86         if (!(obj instanceof Type)) {
87             return false;
88         }
89         final Type other = (Type) obj;
90         return Objects.equals(this.name, other.getName()) && Objects.equals(this.packageName, other.getPackageName());
91     }
92
93
94     @Override
95     public String toString() {
96         if (this.packageName.isEmpty()) {
97             return "Type (" + this.name + ")";
98         }
99         return "Type (" + this.packageName + "." + this.name + ")";
100     }
101
102     @Override
103     public String getPackageName() {
104         return this.packageName;
105     }
106
107     @Override
108     public String getName() {
109         return this.name;
110     }
111
112     @Override
113     public String getFullyQualifiedName() {
114         if (this.packageName.isEmpty()) {
115             return this.name;
116         } else {
117             return this.packageName + "." + this.name;
118         }
119     }
120 }