org.apache.commons.collections.CollectionUtils.predicatedCollection()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(2.1k)|赞(0)|评价(0)|浏览(161)

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

CollectionUtils.predicatedCollection介绍

[英]Returns a predicated (validating) collection backed by the given collection.

Only objects that pass the test in the given predicate can be added to the collection. Trying to add an invalid object results in an IllegalArgumentException. It is important not to use the original collection after invoking this method, as it is a backdoor for adding invalid objects.
[中]返回给定集合支持的谓词(验证)集合。
只有在给定谓词中通过测试的对象才能添加到集合中。尝试添加无效对象会导致IllegalArgumentException。调用此方法后不要使用原始集合,因为它是添加无效对象的后门。

代码示例

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

public void testPredicatedCollection() {
  Predicate predicate = new Predicate() {
    public boolean evaluate(Object o) {
      return o instanceof String;
    }
  };
  Collection collection = 
    CollectionUtils.predicatedCollection(new ArrayList(), predicate);
  assertTrue("returned object should be a PredicatedCollection",
    collection instanceof PredicatedCollection);
  try { 
    collection = 
      CollectionUtils.predicatedCollection(new ArrayList(), null); 
    fail("Expecting IllegalArgumentException for null predicate.");
  } catch (IllegalArgumentException ex) {
    // expected
  }
  try { 
    collection = 
      CollectionUtils.predicatedCollection(null, predicate); 
    fail("Expecting IllegalArgumentException for null collection.");
  } catch (IllegalArgumentException ex) {
    // expected
  }             
}

代码示例来源:origin: com.atlassian.core/atlassian-core

public static <T> List<T> buildNonNull(Collection<T> c) {
  List<T> list;
  if (c != null && !c.isEmpty()) {
    list = build(CollectionUtils.predicatedCollection(c, ObjectUtils.getIsSetPredicate()));
  } else {
    list = Collections.emptyList();
  }
  return list;
}

代码示例来源:origin: com.atlassian.core/atlassian-core-utils

public static List buildNonNull(Collection c)
{
  List list;
  if (c != null && !c.isEmpty())
  {
    list = build(CollectionUtils.predicatedCollection(c, ObjectUtils.getIsSetPredicate()));
  }
  else
  {
    list = Collections.EMPTY_LIST;
  }
  return list;
}

相关文章