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