org.apache.commons.collections.Predicate类的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(7.1k)|赞(0)|评价(0)|浏览(183)

本文整理了Java中org.apache.commons.collections.Predicate类的一些代码示例,展示了Predicate类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Predicate类的具体详情如下:
包路径:org.apache.commons.collections.Predicate
类名称:Predicate

Predicate介绍

[英]Defines a functor interface implemented by classes that perform a predicate test on an object.

A Predicate is the object equivalent of an if statement. It uses the input object to return a true or false value, and is often used in validation or filtering.

Standard implementations of common predicates are provided by PredicateUtils. These include true, false, instanceof, equals, and, or, not, method invokation and null testing.
[中]定义由对对象执行谓词测试的类实现的函子接口。
Predicateif语句的对象等价物。它使用输入对象返回真值或假值,通常用于验证或筛选。
PredicateUtils提供了公共谓词的标准实现。其中包括true、false、instanceof、equals和(或)not、方法调用和空测试。

代码示例

代码示例来源:origin: commons-collections/commons-collections

/**
 * Evaluates the predicate returning true if both predicates return true.
 * 
 * @param object  the input object
 * @return true if both decorated predicates return true
 */
public boolean evaluate(Object object) {
  return (iPredicate1.evaluate(object) && iPredicate2.evaluate(object));
}

代码示例来源:origin: commons-collections/commons-collections

/**
 * Evaluates the predicate returning the opposite to the stored predicate.
 * 
 * @param object  the input object
 * @return true if predicate returns false
 */
public boolean evaluate(Object object) {
  return !(iPredicate.evaluate(object));
}

代码示例来源:origin: commons-collections/commons-collections

/**
 * Transforms the input to result by calling a predicate.
 * 
 * @param input  the input object to transform
 * @return the transformed result
 */
public Object transform(Object input) {
  return (iPredicate.evaluate(input) ? Boolean.TRUE : Boolean.FALSE);
}

代码示例来源:origin: commons-collections/commons-collections

/**
 * Evaluates the predicate returning true if either predicate returns true.
 * 
 * @param object  the input object
 * @return true if either decorated predicate returns true
 */
public boolean evaluate(Object object) {
  return (iPredicate1.evaluate(object) || iPredicate2.evaluate(object));
}

代码示例来源:origin: commons-collections/commons-collections

/**
 * Evaluates the predicate returning the result of the decorated predicate
 * once a null check is performed.
 * 
 * @param object  the input object
 * @return true if decorated predicate returns true, false if input is null
 */
public boolean evaluate(Object object) {
  if (object == null) {
    return false;
  }
  return iPredicate.evaluate(object);
}

代码示例来源:origin: commons-collections/commons-collections

/**
 * Evaluates the predicate returning the result of the decorated predicate
 * once a null check is performed.
 * 
 * @param object  the input object
 * @return true if decorated predicate returns true or input is null
 */
public boolean evaluate(Object object) {
  if (object == null) {
    return true;
  }
  return iPredicate.evaluate(object);
}

代码示例来源:origin: pentaho/pentaho-kettle

/**
 * {@inheritDoc}
 *
 * @see org.apache.commons.collections.Predicate#evaluate(java.lang.Object)
 */
public boolean evaluate( final Object object ) {
 return !EMPTY_MAP.evaluate( object );
}

代码示例来源:origin: pentaho/pentaho-kettle

/**
 * {@inheritDoc}
 *
 * @see org.apache.commons.collections.Predicate#evaluate(java.lang.Object)
 */
public boolean evaluate( final Object object ) {
 return object != null && !EMPTY_ARRAY.evaluate( object );
}

代码示例来源:origin: commons-collections/commons-collections

/**
 * Override to validate an object set into the map via <code>setValue</code>.
 * 
 * @param value  the value to validate
 * @throws IllegalArgumentException if invalid
 * @since Commons Collections 3.1
 */
protected Object checkSetValue(Object value) {
  if (valuePredicate.evaluate(value) == false) {
    throw new IllegalArgumentException("Cannot set value - Predicate rejected it");
  }
  return value;
}

代码示例来源:origin: commons-collections/commons-collections

/**
 * Validates the object being added to ensure it matches the predicate.
 * <p>
 * The predicate itself should not throw an exception, but return false to
 * indicate that the object cannot be added.
 * 
 * @param object  the object being added
 * @throws IllegalArgumentException if the add is invalid
 */
protected void validate(Object object) {
  if (predicate.evaluate(object) == false) {
    throw new IllegalArgumentException("Cannot add Object '" + object + "' - Predicate rejected it");
  }
}

代码示例来源:origin: commons-collections/commons-collections

/**
 * Evaluates the predicate returning the result of the decorated predicate
 * once the input has been transformed
 * 
 * @param object  the input object which will be transformed
 * @return true if decorated predicate returns true
 */
public boolean evaluate(Object object) {
  Object result = iTransformer.transform(object);
  return iPredicate.evaluate(result);
}

代码示例来源:origin: commons-collections/commons-collections

/**
 * Evaluates the predicate returning the result of the decorated predicate
 * once a null check is performed.
 * 
 * @param object  the input object
 * @return true if decorated predicate returns true
 * @throws FunctorException if input is null
 */
public boolean evaluate(Object object) {
  if (object == null) {
    throw new FunctorException("Input Object must not be null");
  }
  return iPredicate.evaluate(object);
}

代码示例来源:origin: commons-collections/commons-collections

/**
 * Executes the true or false closure accoring to the result of the predicate.
 * 
 * @param input  the input object
 */
public void execute(Object input) {
  if (iPredicate.evaluate(input) == true) {
    iTrueClosure.execute(input);
  } else {
    iFalseClosure.execute(input);
  }
}

代码示例来源:origin: commons-collections/commons-collections

/**
 * Executes the closure until the predicate is false.
 * 
 * @param input  the input object
 */
public void execute(Object input) {
  if (iDoLoop) {
    iClosure.execute(input);
  }
  while (iPredicate.evaluate(input)) {
    iClosure.execute(input);
  }
}

代码示例来源:origin: commons-collections/commons-collections

/**
 * Executes the closure whose matching predicate returns true
 * 
 * @param input  the input object
 */
public void execute(Object input) {
  for (int i = 0; i < iPredicates.length; i++) {
    if (iPredicates[i].evaluate(input) == true) {
      iClosures[i].execute(input);
      return;
    }
  }
  iDefault.execute(input);
}

代码示例来源:origin: commons-collections/commons-collections

public void testUniquePredicate() {
  Predicate p = PredicateUtils.uniquePredicate();
  assertEquals(true, p.evaluate(new Object()));
  assertEquals(true, p.evaluate(new Object()));
  assertEquals(true, p.evaluate(new Object()));
  assertEquals(true, p.evaluate(cString));
  assertEquals(false, p.evaluate(cString));
  assertEquals(false, p.evaluate(cString));
}

代码示例来源:origin: commons-collections/commons-collections

public void testInvokerPredicate() {
  List list = new ArrayList();
  assertEquals(true, PredicateUtils.invokerPredicate("isEmpty").evaluate(list));
  list.add(new Object());
  assertEquals(false, PredicateUtils.invokerPredicate("isEmpty").evaluate(list));
}

代码示例来源:origin: commons-collections/commons-collections

public void testInvokerPredicate2() {
  List list = new ArrayList();
  assertEquals(false, PredicateUtils.invokerPredicate(
    "contains", new Class[] {Object.class}, new Object[] {cString}).evaluate(list));
  list.add(cString);
  assertEquals(true, PredicateUtils.invokerPredicate(
    "contains", new Class[] {Object.class}, new Object[] {cString}).evaluate(list));
}

代码示例来源:origin: commons-collections/commons-collections

public void testInvokerPredicateEx3() {
  try {
    PredicateUtils.invokerPredicate("noSuchMethod").evaluate(new Object());
  } catch (FunctorException ex) {
    return;
  }
  fail();
}

代码示例来源:origin: commons-collections/commons-collections

public void testInvokerPredicate2Ex3() {
  try {
    PredicateUtils.invokerPredicate(
      "noSuchMethod", new Class[] {Object.class}, new Object[] {cString}).evaluate(new Object());
  } catch (FunctorException ex) {
    return;
  }
  fail();
}

相关文章

Predicate类方法