/*
 * ################################################################
 *
 * ProActive: The Java(TM) library for Parallel, Distributed,
 *            Concurrent computing with Security and Mobility
 *
 * Copyright (C) 1997-2008 INRIA/University of Nice-Sophia Antipolis
 * Contact: proactive@ow2.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 *  Initial developer(s):               The ProActive Team
 *                        http://proactive.inria.fr/team_members.htm
 *  Contributor(s):
 *
 * ################################################################
 * $$PROACTIVE_INITIAL_DEV$$
 */
package org.objectweb.proactive.core.component;

import java.io.Serializable;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.Type;
import org.objectweb.proactive.core.component.representative.ProActiveComponentRepresentativeImpl;


/**
 * Abstract implementation of the Interface interface of the Fractal api
 * <p>
 * As functional interfaces are specified for each component, they are generated
 * at instantiation time (bytecode generation), by subclassing this class.
 *
 * @author The ProActive Team
 */
public abstract class ProActiveInterfaceImpl implements ProActiveInterface, Serializable {
    private Component owner;
    private String name;
    private Type type;
    private boolean isInternal;

    public ProActiveInterfaceImpl() {
    }

    /*
     *
     * @see org.objectweb.fractal.api.Interface#getFcItfOwner()
     */
    public Component getFcItfOwner() {
        return owner;
    }

    /*
     *
     * @see org.objectweb.fractal.api.Interface#getFcItfName()
     */
    public String getFcItfName() {
        return name;
    }

    /*
     *
     * @see org.objectweb.fractal.api.Interface#getFcItfType()
     */
    public Type getFcItfType() {
        return type;
    }

    /*
     *
     * @see org.objectweb.fractal.api.Interface#isFcInternalItf()
     */
    public boolean isFcInternalItf() {
        return isInternal;
    }

    /*
     *
     * @see org.objectweb.proactive.core.component.ProActiveInterface#setFcIsInternal(boolean)
     */
    public void setFcIsInternal(boolean isInternal) {
        this.isInternal = isInternal;
    }

    /*
     *
     * @see org.objectweb.proactive.core.component.ProActiveInterface#setFcItfName(java.lang.String)
     */
    public void setFcItfName(String name) {
        this.name = name;
    }

    /*
     *
     * @see org.objectweb.proactive.core.component.ProActiveInterface#setFcItfOwner(org.objectweb.fractal.api.Component)
     */
    public void setFcItfOwner(Component owner) {
        this.owner = owner;
    }

    /*
     *
     * @see org.objectweb.proactive.core.component.ProActiveInterface#setFcType(org.objectweb.fractal.api.Type)
     */
    public void setFcType(Type type) {
        this.type = type;
    }

    /**
     *
     * @see org.objectweb.proactive.core.component.ProActiveInterface#getFcItfImpl()
     */
    public abstract Object getFcItfImpl();

    /**
     *
     * @see org.objectweb.proactive.core.component.ProActiveInterface#setFcItfImpl(java.lang.Object)
     */
    public abstract void setFcItfImpl(final Object impl);

    @Override
    public boolean equals(Object anObject) {
        if (anObject instanceof ProActiveComponentRepresentativeImpl) {
            return super.equals(anObject);

        }
        ProActiveInterfaceImpl itf = (ProActiveInterfaceImpl) anObject;
        boolean one = itf.getFcItfName().equals(name);
        ProActiveComponentRepresentativeImpl pr1 = (ProActiveComponentRepresentativeImpl) itf.getFcItfOwner();
        ProActiveComponentRepresentativeImpl pr2 = (ProActiveComponentRepresentativeImpl) owner;
        boolean two = pr1.getID().equals(pr2.getID());
        return one & two;
    }

    @Override
    public int hashCode() {
        return name.hashCode() + owner.hashCode();
    }

    @Override
    public String toString() {
        String string = "name : " + getFcItfName() + "\n" + //            "componentIdentity : " + getFcItfOwner() + "\n" + "type : " +
            getFcItfType() + "\n" + "isInternal : " + isFcInternalItf() + "\n";
        return string;
    }
}

@@ -1372,16 +1372,60 @@
 ponent;%0A
+import org.objectweb.fractal.api.Interface;%0A
 import o
@@ -3927,24 +3927,82 @@
 anObject) %7B%0A
+        //it's the %22component%22 itf, compare directly them%0A
         if (
@@ -4115,24 +4115,25 @@
       %7D%0A
+%0A
         
 ProActiv
@@ -4128,47 +4128,109 @@
     
-ProActiveInterfaceImpl itf = (ProActive
+if (!(anObject instanceof Interface)) %7B%0A            return false;%0A        %7D%0A        Interface itf = (
 Inte
@@ -4234,20 +4234,16 @@
 nterface
-Impl
 ) anObje
@@ -4262,19 +4262,28 @@
 boolean 
-one
+nameEquality
  = itf.g
@@ -4309,16 +4309,74 @@
 s(name);
+%0A%0A        // Are the two itf belong to the same component?
 %0A       
@@ -4592,19 +4592,29 @@
 boolean 
-two
+ownerEquality
  = pr1.g
@@ -4641,16 +4641,17 @@
 tID());%0A
+%0A
         
@@ -4661,17 +4661,36 @@
 urn 
-one & two
+nameEquality & ownerEquality
 ;%0A  

fix PROACTIVE-609 prevent ClassCast exception