/*
 * 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.meta;


/**
 * Clause set containing clauses built from the equals relationship
 * between objects.
 * @see org.mandarax.util.AutoFacts
 * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
 * @version 3.3.1 <9 August 2004>
 * @since 1.0
 * @deprecated it is recommended to use the more general class org.mandarax.util.AutoFacts instead
 */
public final class EqualsClauseSet extends JSymmetricClauseSet {

    private static JPredicate equals    = null;
    private static JPredicate equalsNot = null;

    /**
     * Constructor. The set is build from the objects in the collection - each pair
     * of objects where equals sent to one object with the other object as parameter returns true
     * defines a fact in the clause set.
     * @param argumentSet a collection of objects
     * @throws java.lang.IllegalAccessException
     * @throws java.lang.IllegalArgumentException
     */
    public EqualsClauseSet(java.util.Collection argumentSet)
            throws IllegalAccessException, IllegalArgumentException {
        super (getEqualsPredicate (), argumentSet);
    }

    /**
     * Constructor.
     * @param argumentSet a collection of objects
     * @param isNegated true if the set should be negated, false otherwise
     * @throws java.lang.IllegalAccessException
     * @throws java.lang.IllegalArgumentException
     */
    public EqualsClauseSet(
            java.util.Collection argumentSet, boolean isNegated)
                throws NullPointerException, IllegalAccessException,
                       IllegalArgumentException {
        super (isNegated
               ? getEqualsNotPredicate ()
               : getEqualsPredicate (), argumentSet, isNegated);
    }

    /**
     * Constructor. We use a datasource instead of a collection.
     * @param argumentSet the data source (kind of dynamic collection)
     * @throws java.lang.IllegalAccessException
     * @throws java.lang.IllegalArgumentException
     */
    public EqualsClauseSet(org.mandarax.util.DataSource argumentSet)
            throws NullPointerException, IllegalAccessException,
                   IllegalArgumentException {
        super (getEqualsPredicate (), argumentSet);
    }

    /**
     * Constructor.
     * @param argumentSet the data source (kind of dynamic collection)
     * @param isNegated true if the set should be negated, false otherwise
     * @throws java.lang.IllegalAccessException
     * @throws java.lang.IllegalArgumentException
     */
    public EqualsClauseSet(
            org.mandarax.util.DataSource argumentSet, boolean isNegated)
                throws NullPointerException, IllegalAccessException,
                       IllegalArgumentException {
        super (isNegated
               ? getEqualsNotPredicate ()
               : getEqualsPredicate (), argumentSet, isNegated);
    }

    /**
     * Get the negated equals predicate. Use lazy initialization.
     * @return the respective predicate
     */
    private static org.mandarax.kernel.meta
            .JPredicate getEqualsNotPredicate() {
        if(equalsNot == null) {
            try {
                Class   obj = Object.class;
                Class[] par = new Class[1];

                par[0]    = obj;
                equalsNot =
                    new JPredicate (obj.getDeclaredMethod ("equals", par),
                                    "equalsNot", true);
            } catch(Exception t) {
                System.err.println ("Problems building the equals method");
            }
        }

        return equalsNot;
    }

    /**
     * Get the equals predicate. Use lazy initialization.
     * @return the respective predicate
     */
    private static org.mandarax.kernel.meta.JPredicate getEqualsPredicate() {
        if(equals == null) {
            try {
                Class   obj = Object.class;
                Class[] par = new Class[1];

                par[0] = obj;
                equals = new JPredicate (obj.getDeclaredMethod ("equals",
                        par));
            } catch(Exception t) {
                System.err.println ("Problems building the equals method");
            }
        }

        return equals;
    }
}
