Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
Mandarax的判断某Object是否在某集合中的Predicate

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

/*
 * Copyright (C) 1999-2004 <a href="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="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";
    }
}




相关文章

相关软件