Mandarax的判断某Object是否在某集合中的Predicate

类别:Java 点击:0 评论:0 推荐:

/*
 * Copyright (C) 1999-2004 <a href="Aaron'>mailto:[email protected]">Aaron Tang</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.lib.collection;

import org.mandarax.kernel.*;
import java.util.AbstractCollection;

/**
 * Predicates associating one object and a collection may contain the object.
 *
 * @author <A HREF="Aaron'>mailto:[email protected]">Aaron Tang</A>
 * @version 3.3 <18 Oct. 2004>
 * @since 1.4
 */
public class CollectionPredicate extends
        org.mandarax.lib.AbstractPredicate {
    private static final long serialVersionUID = -1234619399185521391L;
   
    private static Class[] structure = { Object.class, AbstractCollection.class };

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

    /**
     * Perform the function or predicate using an array of terms as parameters.
     *
     * @return an object
     * @param parameter
     *            an array of terms
     * @param session
     *            a session object
     * @throws java.lang.UnsupportedOperationException
     * @throws java.lang.IllegalArgumentException
     */
    public Object perform(Term[] parameter, Session session)
            throws IllegalArgumentException, UnsupportedOperationException {
        Object s1;
        AbstractCollection s2;

        try {
            s1 = (parameter[0].resolve(session));
            s2 = (AbstractCollection) (parameter[1].resolve(session));
        } catch (Exception x) {
            throw new IllegalArgumentException();
        }

        if (s1 == null) {
            throw new IllegalArgumentException();
        }

        return (evaluate(s1, s2)) ? Boolean.TRUE : Boolean.FALSE;
    }

    /**
     * Evaluate the two values using the predicate.
     *
     * @return true or false
     * @param s1
     *            value 1
     * @param s2
     *            value 2
     */
    protected boolean evaluate(Object s1, AbstractCollection s2) {
        return s2.contains(s1);
    }

    /* (non-Javadoc)
     * @see org.mandarax.kernel.Constructor#getName()
     */
    public String getName() {
        return "contained in";
    }
}

本文地址:http://com.8s8s.com/it/it14418.htm