/** * Create a new Transformer that uses the input object as a key to find the * transformer to call. * <p> * The Map consists of object keys and Transformer values. A transformer * is called if the input object equals the key. If there is no match, the * default transformer is called. The default transformer is set in the map * using a null key. If no default is set, null will be returned in a default case. * * @param <I> the input type * @param <O> the output type * @param objectsAndTransformers a map of objects to transformers * @return the transformer * @throws NullPointerException if the map is null * @throws NullPointerException if any transformer in the map is null * @see SwitchTransformer */ @SuppressWarnings("unchecked") public static <I, O> Transformer<I, O> switchMapTransformer( final Map<I, Transformer<I, O>> objectsAndTransformers) { if (objectsAndTransformers == null) { throw new NullPointerException("The object and transformer map must not be null"); } final Transformer<? super I, ? extends O> def = objectsAndTransformers.remove(null); final int size = objectsAndTransformers.size(); final Transformer<? super I, ? extends O>[] trs = new Transformer[size]; final Predicate<I>[] preds = new Predicate[size]; int i = 0; for (final Map.Entry<I, Transformer<I, O>> entry : objectsAndTransformers.entrySet()) { preds[i] = EqualPredicate.<I>equalPredicate(entry.getKey()); trs[i++] = entry.getValue(); } return TransformerUtils.switchTransformer(preds, trs, def); }
/** * Returns the number of occurrences of the provided object in the iterable. * * @param <E> the element type that the {@link Iterable} may contain * @param <T> the element type of the object to find * @param iterable the {@link Iterable} to search * @param obj the object to find the cardinality of * @return the the number of occurrences of obj in iterable */ public static <E, T extends E> int frequency(final Iterable<E> iterable, final T obj) { if (iterable instanceof Set<?>) { return ((Set<E>) iterable).contains(obj) ? 1 : 0; } if (iterable instanceof Bag<?>) { return ((Bag<E>) iterable).getCount(obj); } return size(filteredIterable(emptyIfNull(iterable), EqualPredicate.<E>equalPredicate(obj))); }
@Before public void setUp() throws IOException { SimpleClasspathHelper helper = new SimpleClasspathHelper(); helper.setClassLoader(new FilterableClassLoader(getClass().getClassLoader(), new NotPredicate<>(new EqualPredicate<>("javax.mail.Transport")))); ClasspathUtils.setHelper(helper); Properties additionalProps = new Properties(); additionalProps.setProperty("mail.smtp.host", ServerSetupTest.SMTP.getBindAddress()); additionalProps.setProperty("mail.smtp.port", String.valueOf(ServerSetupTest.SMTP.getPort())); oghamService = MessagingBuilder.standard() .environment() .properties("/application.properties") .properties(additionalProps) .and() .build(); }
/** * Test collections util select predicate test. */ @Test public void testCollectionsUtilSelectPredicateTest(){ User zhangfei1 = new User("zhangfei", 22); User zhangfei2 = new User("zhangFei", 22); User zhangfei3 = new User("Zhangfei", 22); User zhangfeinull = new User((String) null, 22); User guanyu = new User("guanyu", 25); User liubei = new User("liubei", 30); List<User> list = toList(zhangfei1, zhangfei2, zhangfei3, zhangfeinull, guanyu, liubei); List<User> select = select(list, new BeanPredicate<User>( "name", // EqualPredicate.equalPredicate("zhangfei", IgnoreCaseEquator.INSTANCE))); assertThat(select, allOf(// hasItem(zhangfei1), hasItem(zhangfei2), hasItem(zhangfei3), not(hasItem(zhangfeinull)), not(hasItem(guanyu)), not(hasItem(liubei)) // )); }
/** * Create a new Closure that uses the input object as a key to find the * closure to call. * <p> * The Map consists of object keys and Closure values. A closure * is called if the input object equals the key. If there is no match, the * default closure is called. The default closure is set in the map * using a null key. * * @see org.apache.commons.collections4.functors.SwitchClosure * * @param <E> the type that the closure acts on * @param objectsAndClosures a map of objects to closures * @return the closure * @throws NullPointerException if the map is null * @throws NullPointerException if any closure in the map is null * @throws IllegalArgumentException if the map is empty */ @SuppressWarnings("unchecked") public static <E> Closure<E> switchMapClosure(final Map<? extends E, Closure<E>> objectsAndClosures) { if (objectsAndClosures == null) { throw new NullPointerException("The object and closure map must not be null"); } final Closure<? super E> def = objectsAndClosures.remove(null); final int size = objectsAndClosures.size(); final Closure<? super E>[] trs = new Closure[size]; final Predicate<E>[] preds = new Predicate[size]; int i = 0; for (final Map.Entry<? extends E, Closure<E>> entry : objectsAndClosures.entrySet()) { preds[i] = EqualPredicate.<E>equalPredicate(entry.getKey()); trs[i] = entry.getValue(); i++; } return ClosureUtils.<E>switchClosure(preds, trs, def); }
/** * Test select rejected predicate equal predicate. */ @Test public void testSelectRejectedPredicateEqualPredicate(){ List<Long> list = toList(1L, 1L, 2L, 3L); assertThat(CollectionsUtil.selectRejected(list, new EqualPredicate<Long>(1L)), contains(2L, 3L)); }
/** * Test select rejected predicate null collection. */ @Test public void testSelectRejectedPredicateNullCollection(){ assertEquals(emptyList(), CollectionsUtil.selectRejected(null, new EqualPredicate<Long>(1L))); }
/** * Test select rejected predicate empty collection. */ @Test public void testSelectRejectedPredicateEmptyCollection(){ assertEquals(emptyList(), CollectionsUtil.selectRejected(new ArrayList<Long>(), new EqualPredicate<Long>(1L))); }
/** * Test select predicate 1. */ @Test public void testSelectPredicateEqualPredicate(){ List<Long> list = toList(1L, 1L, 2L, 3L); assertThat(CollectionsUtil.select(list, new EqualPredicate<Long>(1L)), contains(1L, 1L)); }
/** * Test select predicate null collection. */ @Test public void testSelectPredicateNullCollection(){ assertEquals(emptyList(), CollectionsUtil.select(null, new EqualPredicate<Long>(1L))); }
/** * Test select predicate empty collection. */ @Test public void testSelectPredicateEmptyCollection(){ assertEquals(emptyList(), CollectionsUtil.select(new ArrayList<Long>(), new EqualPredicate<Long>(1L))); }
/** * Checks if the object is contained in the given iterable. Object equality * is tested with an {@code equator} unlike {@link #contains(Iterable, Object)} * which uses {@link Object#equals(Object)}. * <p> * A <code>null</code> or empty iterable returns false. * A <code>null</code> object will not be passed to the equator, instead a * {@link org.apache.commons.collections4.functors.NullPredicate NullPredicate} * will be used. * * @param <E> the type of object the {@link Iterable} contains * @param iterable the iterable to check, may be null * @param object the object to check * @param equator the equator to use to check, may not be null * @return true if the object is contained in the iterable, false otherwise * @throws NullPointerException if equator is null */ public static <E> boolean contains(final Iterable<? extends E> iterable, final E object, final Equator<? super E> equator) { if (equator == null) { throw new NullPointerException("Equator must not be null."); } return matchesAny(iterable, EqualPredicate.equalPredicate(object, equator)); }
/** * Creates a Predicate that checks if the input object is equal to the * specified object using equals(). * * @param <T> the type that the predicate queries * @param value the value to compare against * @return the predicate * @see EqualPredicate */ public static <T> Predicate<T> equalPredicate(final T value) { return EqualPredicate.equalPredicate(value); }
/** * Checks if the object is contained in the given iterator. * <p> * A <code>null</code> or empty iterator returns false. * * @param <E> the type of object the {@link Iterator} contains * @param iterator the iterator to check, may be null * @param object the object to check * @return true if the object is contained in the iterator, false otherwise * @since 4.1 */ public static <E> boolean contains(final Iterator<E> iterator, final Object object) { return matchesAny(iterator, EqualPredicate.equalPredicate(object)); }
/** * 用来指定 <code>T</code> 对象的特定属性 <code>propertyName</code> equalsIgnoreCase 指定的 <code>propertyValue</code>. * * <h3>说明:</h3> * <blockquote> * <ol> * <li> * 常用于解析集合,如 {@link CollectionsUtil#select(Iterable, Predicate) select},{@link CollectionsUtil#find(Iterable, Predicate) find}, * {@link CollectionsUtil#selectRejected(Iterable, Predicate) selectRejected}, * {@link CollectionsUtil#group(Iterable, String, Predicate) group}, * {@link AggregateUtil#groupCount(Iterable, String, Predicate) groupCount}, * {@link AggregateUtil#sum(Iterable, String, Predicate) sum} 等方法. * </li> * </ol> * </blockquote> * * <h3>示例:</h3> * * <blockquote> * * <p> * <b>场景:</b> 在list中查找 name是 zhangfei(忽视大小写) 的user * </p> * * <pre class="code"> * User zhangfei1 = new User("zhangfei", 22); * User zhangfei2 = new User("zhangFei", 22); * User zhangfei3 = new User("Zhangfei", 22); * User zhangfeinull = new User((String) null, 22); * User guanyu = new User("guanyu", 25); * User liubei = new User("liubei", 30); * * List{@code <User>} list = toList(zhangfei1, zhangfei2, zhangfei3, zhangfeinull, guanyu, liubei); * * List{@code <User>} select = select(list, BeanPredicateUtil.{@code <User>} equalIgnoreCasePredicate("name", "zhangfei")); * * assertThat(select, allOf(// * hasItem(zhangfei1), * hasItem(zhangfei2), * hasItem(zhangfei3), * * not(hasItem(zhangfeinull)), * not(hasItem(guanyu)), * not(hasItem(liubei)) * // * )); * </pre> * * </blockquote> * * @param <T> * the generic type * @param propertyName * 泛型T对象指定的属性名称,Possibly indexed and/or nested name of the property to be modified,参见 * <a href="../../bean/BeanUtil.html#propertyName">propertyName</a> * @param propertyValue * the property value * @return 如果 <code>propertyName</code> 是null,抛出 {@link NullPointerException}<br> * 如果 <code>propertyName</code> 是blank,抛出 {@link IllegalArgumentException}<br> * @see org.apache.commons.collections4.PredicateUtils#equalPredicate(Object) * @see com.feilong.core.util.equator.IgnoreCaseEquator#INSTANCE * @since 1.10.1 */ public static <T> Predicate<T> equalIgnoreCasePredicate(String propertyName,String propertyValue){ Validate.notBlank(propertyName, "propertyName can't be blank!"); return new BeanPredicate<>(propertyName, EqualPredicate.equalPredicate(propertyValue, IgnoreCaseEquator.INSTANCE)); }