Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/com/sun/beans/finder/Signature.java
41161 views
1
/*
2
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
package com.sun.beans.finder;
26
27
/**
28
* This class is designed to be a key of a cache
29
* of constructors or methods.
30
*
31
* @since 1.7
32
*
33
* @author Sergey A. Malenkov
34
*/
35
final class Signature {
36
private final Class<?> type;
37
private final String name;
38
private final Class<?>[] args;
39
40
private volatile int code;
41
42
/**
43
* Constructs signature for constructor.
44
*
45
* @param type the class that contains constructor
46
* @param args the types of constructor's parameters
47
*/
48
Signature(Class<?> type, Class<?>[] args) {
49
this(type, null, args);
50
}
51
52
/**
53
* Constructs signature for method.
54
*
55
* @param type the class that contains method
56
* @param name the name of the method
57
* @param args the types of method's parameters
58
*/
59
Signature(Class<?> type, String name, Class<?>[] args) {
60
this.type = type;
61
this.name = name;
62
this.args = args;
63
}
64
65
Class<?> getType() {
66
return this.type;
67
}
68
69
String getName() {
70
return this.name;
71
}
72
73
Class<?>[] getArgs() {
74
return this.args;
75
}
76
77
/**
78
* Indicates whether some other object is "equal to" this one.
79
*
80
* @param object the reference object with which to compare
81
* @return {@code true} if this object is the same as the
82
* {@code object} argument, {@code false} otherwise
83
* @see #hashCode()
84
*/
85
@Override
86
public boolean equals(Object object) {
87
if (this == object) {
88
return true;
89
}
90
if (object instanceof Signature) {
91
Signature signature = (Signature) object;
92
return isEqual(signature.type, this.type)
93
&& isEqual(signature.name, this.name)
94
&& isEqual(signature.args, this.args);
95
}
96
return false;
97
}
98
99
/**
100
* Indicates whether some object is "equal to" another one.
101
* This method supports {@code null} values.
102
*
103
* @param obj1 the first reference object that will compared
104
* @param obj2 the second reference object that will compared
105
* @return {@code true} if first object is the same as the second object,
106
* {@code false} otherwise
107
*/
108
private static boolean isEqual(Object obj1, Object obj2) {
109
return (obj1 == null)
110
? obj2 == null
111
: obj1.equals(obj2);
112
}
113
114
/**
115
* Indicates whether some array is "equal to" another one.
116
* This method supports {@code null} values.
117
*
118
* @param args1 the first reference array that will compared
119
* @param args2 the second reference array that will compared
120
* @return {@code true} if first array is the same as the second array,
121
* {@code false} otherwise
122
*/
123
private static boolean isEqual(Class<?>[] args1, Class<?>[] args2) {
124
if ((args1 == null) || (args2 == null)) {
125
return args1 == args2;
126
}
127
if (args1.length != args2.length) {
128
return false;
129
}
130
for (int i = 0; i < args1.length; i++) {
131
if (!isEqual(args1[i], args2[i])) {
132
return false;
133
}
134
}
135
return true;
136
}
137
138
/**
139
* Returns a hash code value for the object.
140
* This method is supported for the benefit of hashtables
141
* such as {@link java.util.HashMap} or {@link java.util.HashSet}.
142
* Hash code computed using algorithm
143
* suggested in Effective Java, Item 8.
144
*
145
* @return a hash code value for this object
146
* @see #equals(Object)
147
*/
148
@Override
149
public int hashCode() {
150
if (this.code == 0) {
151
int code = 17;
152
code = addHashCode(code, this.type);
153
code = addHashCode(code, this.name);
154
155
if (this.args != null) {
156
for (Class<?> arg : this.args) {
157
code = addHashCode(code, arg);
158
}
159
}
160
this.code = code;
161
}
162
return this.code;
163
}
164
165
/**
166
* Adds hash code value if specified object.
167
* This is a part of the algorithm
168
* suggested in Effective Java, Item 8.
169
*
170
* @param code current hash code value
171
* @param object object that updates hash code value
172
* @return updated hash code value
173
* @see #hashCode()
174
*/
175
private static int addHashCode(int code, Object object) {
176
code *= 37;
177
return (object != null)
178
? code + object.hashCode()
179
: code;
180
}
181
}
182
183