827N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
827N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
827N/A *
827N/A * This code is free software; you can redistribute it and/or modify it
827N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
827N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
827N/A *
827N/A * This code is distributed in the hope that it will be useful, but WITHOUT
827N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
827N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
827N/A * version 2 for more details (a copy is included in the LICENSE file that
827N/A * accompanied this code).
827N/A *
827N/A * You should have received a copy of the GNU General Public License version
827N/A * 2 along with this work; if not, write to the Free Software Foundation,
827N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
827N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
827N/A */
827N/Apackage com.sun.beans.finder;
827N/A
827N/A/**
827N/A * This class is designed to be a key of a cache
827N/A * of constructors or methods.
827N/A *
827N/A * @since 1.7
827N/A *
827N/A * @author Sergey A. Malenkov
827N/A */
827N/Afinal class Signature {
827N/A private final Class<?> type;
827N/A private final String name;
827N/A private final Class<?>[] args;
827N/A
827N/A private volatile int code;
827N/A
827N/A /**
827N/A * Constructs signature for constructor.
827N/A *
827N/A * @param type the class that contains constructor
827N/A * @param args the types of constructor's parameters
827N/A */
827N/A Signature(Class<?> type, Class<?>[] args) {
827N/A this(type, null, args);
827N/A }
827N/A
827N/A /**
827N/A * Constructs signature for method.
827N/A *
827N/A * @param type the class that contains method
827N/A * @param name the name of the method
827N/A * @param args the types of method's parameters
827N/A */
827N/A Signature(Class<?> type, String name, Class<?>[] args) {
827N/A this.type = type;
827N/A this.name = name;
827N/A this.args = args;
827N/A }
827N/A
827N/A /**
827N/A * Indicates whether some other object is "equal to" this one.
827N/A *
827N/A * @param object the reference object with which to compare
827N/A * @return {@code true} if this object is the same as the
827N/A * {@code object} argument, {@code false} otherwise
827N/A * @see #hashCode()
827N/A */
827N/A @Override
827N/A public boolean equals(Object object) {
827N/A if (this == object) {
827N/A return true;
827N/A }
827N/A if (object instanceof Signature) {
827N/A Signature signature = (Signature) object;
827N/A return isEqual(signature.type, this.type)
827N/A && isEqual(signature.name, this.name)
827N/A && isEqual(signature.args, this.args);
827N/A }
827N/A return false;
827N/A }
827N/A
827N/A /**
827N/A * Indicates whether some object is "equal to" another one.
827N/A * This method supports {@code null} values.
827N/A *
827N/A * @param obj1 the first reference object that will compared
827N/A * @param obj2 the second reference object that will compared
827N/A * @return {@code true} if first object is the same as the second object,
827N/A * {@code false} otherwise
827N/A */
827N/A private static boolean isEqual(Object obj1, Object obj2) {
827N/A return (obj1 == null)
827N/A ? obj2 == null
827N/A : obj1.equals(obj2);
827N/A }
827N/A
827N/A /**
827N/A * Indicates whether some array is "equal to" another one.
827N/A * This method supports {@code null} values.
827N/A *
827N/A * @param args1 the first reference array that will compared
827N/A * @param args2 the second reference array that will compared
827N/A * @return {@code true} if first array is the same as the second array,
827N/A * {@code false} otherwise
827N/A */
827N/A private static boolean isEqual(Class<?>[] args1, Class<?>[] args2) {
827N/A if ((args1 == null) || (args2 == null)) {
827N/A return args1 == args2;
827N/A }
827N/A if (args1.length != args2.length) {
827N/A return false;
827N/A }
827N/A for (int i = 0; i < args1.length; i++) {
827N/A if (!isEqual(args1[i], args2[i])) {
827N/A return false;
827N/A }
827N/A }
827N/A return true;
827N/A }
827N/A
827N/A /**
827N/A * Returns a hash code value for the object.
827N/A * This method is supported for the benefit of hashtables
827N/A * such as {@link java.util.HashMap} or {@link java.util.HashSet}.
827N/A * Hash code computed using algorithm
827N/A * suggested in Effective Java, Item 8.
827N/A *
827N/A * @return a hash code value for this object
827N/A * @see #equals(Object)
827N/A */
827N/A @Override
827N/A public int hashCode() {
827N/A if (this.code == 0) {
827N/A int code = 17;
827N/A code = addHashCode(code, this.type);
827N/A code = addHashCode(code, this.name);
827N/A
827N/A if (this.args != null) {
827N/A for (Class<?> arg : this.args) {
827N/A code = addHashCode(code, arg);
827N/A }
827N/A }
827N/A this.code = code;
827N/A }
827N/A return this.code;
827N/A }
827N/A
827N/A /**
827N/A * Adds hash code value if specified object.
827N/A * This is a part of the algorithm
827N/A * suggested in Effective Java, Item 8.
827N/A *
827N/A * @param code current hash code value
827N/A * @param object object that updates hash code value
827N/A * @return updated hash code value
827N/A * @see #hashCode()
827N/A */
827N/A private static int addHashCode(int code, Object object) {
827N/A code *= 37;
827N/A return (object != null)
827N/A ? code + object.hashCode()
827N/A : code;
827N/A }
827N/A}