76e4dd80cf96b3cff34254242052e27699cca855
[mdsal.git] / binding / mdsal-binding-generator-util / src / main / java / org / opendaylight / yangtools / binding / generator / util / AbstractBaseType.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.yangtools.binding.generator.util;
9
10 import java.util.Objects;
11 import org.opendaylight.yangtools.sal.binding.model.api.Type;
12
13 /**
14  * It is used only as ancestor for other <code>Type</code>s
15  *
16  */
17 public class AbstractBaseType implements Type {
18
19     /**
20      * Name of the package to which this <code>Type</code> belongs.
21      */
22     private final String packageName;
23
24     /**
25      * Name of this <code>Type</code>.
26      */
27     private final String name;
28
29     @Override
30     public String getPackageName() {
31         return packageName;
32     }
33
34     @Override
35     public String getName() {
36         return name;
37     }
38
39     @Override
40     public String getFullyQualifiedName() {
41         if (packageName.isEmpty()) {
42             return name;
43         } else {
44             return packageName + "." + name;
45         }
46     }
47
48     /**
49      * Constructs the instance of this class with the concrete package name type
50      * name.
51      *
52      * @param pkName
53      *            string with the package name to which this <code>Type</code>
54      *            belongs
55      * @param name
56      *            string with the name for this <code>Type</code>
57      */
58     protected AbstractBaseType(String pkName, String name) {
59         if (pkName == null) {
60             throw new IllegalArgumentException("Package Name for Generated Type cannot be null!");
61         }
62         if (name == null) {
63             throw new IllegalArgumentException("Name of Generated Type cannot be null!");
64         }
65         this.packageName = pkName;
66         this.name = name;
67     }
68
69     @Override
70     public int hashCode() {
71         final int prime = 31;
72         int result = 1;
73         result = prime * result + Objects.hashCode(name);
74         result = prime * result + Objects.hashCode(packageName);
75         return result;
76     }
77
78     @Override
79     public boolean equals(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         Type other = (Type) obj;
90         if (name == null) {
91             if (other.getName() != null) {
92                 return false;
93             }
94         } else if (!name.equals(other.getName())) {
95             return false;
96         }
97         if (packageName == null) {
98             if (other.getPackageName() != null) {
99                 return false;
100             }
101         } else if (!packageName.equals(other.getPackageName())) {
102             return false;
103         }
104         return true;
105     }
106
107     @Override
108     public String toString() {
109         if (packageName.isEmpty()) {
110             return "Type (" + name + ")";
111         }
112         return "Type (" + packageName + "." + name + ")";
113     }
114 }