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

此博客展示了Java代码,定义了一个名为CollectionPredicate的类,用于处理对象与集合的关联谓词。该类继承自AbstractPredicate,实现了获取谓词结构、执行谓词功能等方法,核心是判断对象是否包含在集合中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
* 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";
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值