/*
 * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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
 */
package org.mandarax.kernel;


/**
 * Abstract super class for simple predicates.
 * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
 * @version 3.3.1 <9 August 2004>
 * @since 1.3
 */
abstract class SimpleConstructor {

    private Class[] structure = null;
    private String  name      = null;

    /**
     * Constructor.
     */
    public SimpleConstructor() {
        super ();
    }

    /**
     * Constructor.
     * @param aName the name of the predicate
     * @param types the structure of the predicate (the types of the terms)
     */
    public SimpleConstructor(String aName, Class[] types) {
        super ();

        name      = aName;
        structure = types;
    }

    /**
     * Indicates whether the two objects are equal.
     * @return true if the objects are equal, false otherwise
     * @param obj the object to compare
     */
    public boolean equals(Object obj) {
        if(obj instanceof SimpleConstructor) {
            SimpleConstructor sc = (SimpleConstructor) obj;

            if(sc.name.equals (name)
                    && (structure.length == sc.structure.length)) {
                for(int i = 0; i < structure.length; i++) {
                    if(structure[i] != sc.structure[i]) {
                        return false;
                    }
                }

                return true;
            }
        }

        return false;
    }

    /**
     * Get the name of the predicate or function.
     * @return the name of the predicate or function
     */
    public String getName() {
        return name;
    }

    /**
     * Set the name of the predicate or function.
     * @param the name of the predicate or function
     */
    public void setName(String aName) {
        name = aName;
    }

    /**
     * Get the structure of the predicate or function.
     * @return the structure of the predicate or function
     */
    public Class[] getStructure() {
        return structure;
    }

    /**
     * Set the structure of the predicate or function.
     * @param struct the structure of the predicate or function
     */
    public void setStructure(Class[] struct) {
        structure = struct;
    }

    /**
     * Get the hashcode of the object.
     * @return the hash code value
     */
    public int hashCode() {
        int h = (name == null)
                ? 0
                : name.hashCode ();

        for(int i = 0; i < structure.length; i++) {
            h = h ^ structure[i].hashCode ();
        }

        return h;
    }

    /**
     * Perform the function or predicate using an array of terms as parameters.
     * @return the result of the perform operation
     * @param parameter an array of terms
     * @param session a session object
     * @throws java.lang.UnsupportedOperationException - thrown if this constructor does not have a sematics and this operation cannot be supported
     * @throws java.lang.IllegalArgumentException
     *
     */
    public Object perform(Term[] parameter,Session session) throws IllegalArgumentException, UnsupportedOperationException {
        throw new UnsupportedOperationException ("Simple predicates / simple functions do not suppot perform");
    }

    /**
     * Convert the object to a string.
     * @return the string representation of this object
     */
    public String toString() {
        return getName ();
    }
}
