编程语言

img tl_ang

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

发表于2004/10/21 9:08:00  693人阅读

分类: BPM

/*
 * Copyright (C) 1999-2004 <a href="mailto:tangliang@kongzhong.com">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="mailto:tangliang@kongzhong.com">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";
    }
}

阅读全文
0 0

相关文章推荐

img
取 消
img