/*
 * 
 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * The contents of this file are subject to the Sun Community Source
 * License v3.0/Jini Technology Specific Attachment v 1.0 (the
 * "License"). You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://wwws.sun.com/software/jini/licensing/SCSL3_JiniTSA1.html.
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Reference Code is Jini Technology Core Platform code, v2.0. The
 * Developer of the Reference Code is Sun Microsystems, Inc.
 * 
 * Contributor(s): Sun Microsystems, Inc.
 * 
 * The contents of this file comply with the Jini Technology Core
 * Platform Compatibility Kit, v2.0A.
 * 
 * Tester(s): Sun Microsystems, Inc.
 * 
 * Test Platform(s):
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Solaris(TM)SPARC/x86
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Linux (Intel x86)
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Windows (Intel Platform)
 * 
 */

package com.sun.jini.jeri.internal.runtime;

/**
 * Wraps collection elements (such as hash table keys) so that two
 * wrapped elements are only considered to be equal if they have the
 * same class (as well as being equal by Object.equals).
 *
 * This class is used to wrap elements in a collection when their
 * classes are not all trusted, with the restriction that only
 * elements of the same class can be considered equal.  This class
 * never invokes Object.equals on an underlying element with an
 * argument that has a different class than the element, to avoid
 * leaking sensitive information to untrusted elements.
 *
 * For this isolation scheme to work, the Object.equals method of any
 * trusted element class should not invoke comparison methods (such as
 * Object.equals) on any pluggable component without first verifying
 * that the component's implementation is at least as trusted as the
 * implementation of the corresponding component in the Object.equals
 * argument (such as by verifying that the corresponding component
 * objects have the same actual class).  If any such verification
 * fails, the Object.equals method should return false without
 * invoking a comparison method on the component.  Furthermore, these
 * guidelines should be recursively obeyed by the comparison methods
 * of each such component for its subcomponents.
 *
 * @author Sun Microsystems, Inc.
 **/
final class SameClassKey {

    private final Object key;

    SameClassKey(Object key) {
	this.key = key;
    }

    public int hashCode() {
	return key.hashCode();
    }

    public boolean equals(Object obj) {
	if (obj == this) {
	    return true;
	} else if (!(obj instanceof SameClassKey)) {
	    return false;
	}
	SameClassKey other = (SameClassKey) obj;
	return Util.sameClassAndEquals(key, other.key);
    }
}
