Path: blob/master/src/java.desktop/share/classes/com/sun/beans/finder/Signature.java
41161 views
/*1* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package com.sun.beans.finder;2526/**27* This class is designed to be a key of a cache28* of constructors or methods.29*30* @since 1.731*32* @author Sergey A. Malenkov33*/34final class Signature {35private final Class<?> type;36private final String name;37private final Class<?>[] args;3839private volatile int code;4041/**42* Constructs signature for constructor.43*44* @param type the class that contains constructor45* @param args the types of constructor's parameters46*/47Signature(Class<?> type, Class<?>[] args) {48this(type, null, args);49}5051/**52* Constructs signature for method.53*54* @param type the class that contains method55* @param name the name of the method56* @param args the types of method's parameters57*/58Signature(Class<?> type, String name, Class<?>[] args) {59this.type = type;60this.name = name;61this.args = args;62}6364Class<?> getType() {65return this.type;66}6768String getName() {69return this.name;70}7172Class<?>[] getArgs() {73return this.args;74}7576/**77* Indicates whether some other object is "equal to" this one.78*79* @param object the reference object with which to compare80* @return {@code true} if this object is the same as the81* {@code object} argument, {@code false} otherwise82* @see #hashCode()83*/84@Override85public boolean equals(Object object) {86if (this == object) {87return true;88}89if (object instanceof Signature) {90Signature signature = (Signature) object;91return isEqual(signature.type, this.type)92&& isEqual(signature.name, this.name)93&& isEqual(signature.args, this.args);94}95return false;96}9798/**99* Indicates whether some object is "equal to" another one.100* This method supports {@code null} values.101*102* @param obj1 the first reference object that will compared103* @param obj2 the second reference object that will compared104* @return {@code true} if first object is the same as the second object,105* {@code false} otherwise106*/107private static boolean isEqual(Object obj1, Object obj2) {108return (obj1 == null)109? obj2 == null110: obj1.equals(obj2);111}112113/**114* Indicates whether some array is "equal to" another one.115* This method supports {@code null} values.116*117* @param args1 the first reference array that will compared118* @param args2 the second reference array that will compared119* @return {@code true} if first array is the same as the second array,120* {@code false} otherwise121*/122private static boolean isEqual(Class<?>[] args1, Class<?>[] args2) {123if ((args1 == null) || (args2 == null)) {124return args1 == args2;125}126if (args1.length != args2.length) {127return false;128}129for (int i = 0; i < args1.length; i++) {130if (!isEqual(args1[i], args2[i])) {131return false;132}133}134return true;135}136137/**138* Returns a hash code value for the object.139* This method is supported for the benefit of hashtables140* such as {@link java.util.HashMap} or {@link java.util.HashSet}.141* Hash code computed using algorithm142* suggested in Effective Java, Item 8.143*144* @return a hash code value for this object145* @see #equals(Object)146*/147@Override148public int hashCode() {149if (this.code == 0) {150int code = 17;151code = addHashCode(code, this.type);152code = addHashCode(code, this.name);153154if (this.args != null) {155for (Class<?> arg : this.args) {156code = addHashCode(code, arg);157}158}159this.code = code;160}161return this.code;162}163164/**165* Adds hash code value if specified object.166* This is a part of the algorithm167* suggested in Effective Java, Item 8.168*169* @param code current hash code value170* @param object object that updates hash code value171* @return updated hash code value172* @see #hashCode()173*/174private static int addHashCode(int code, Object object) {175code *= 37;176return (object != null)177? code + object.hashCode()178: code;179}180}181182183