All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.softhouse.common.guavaextensions.Predicates2 Maven / Gradle / Ivy

/* Copyright 2013 Jonatan Jönsson
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package se.softhouse.common.guavaextensions;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.alwaysTrue;

import java.util.List;

import javax.annotation.Nonnull;

import se.softhouse.common.strings.Descriptions;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;

/**
 * Additional implementations of the {@link Predicate} interface, as a complement to
 * {@link Predicates}
 */
public final class Predicates2
{
	private Predicates2()
	{
	}

	/**
	 * Returns a predicate that throws an {@link IllegalArgumentException} if any element in a list
	 * doesn't return true for {@code elementLimiter.apply(Object)}. Useful for input validation
	 * where you already have a {@link Predicate} for the element type and want to check a list of
	 * such elements.
	 * Without {@link #listPredicate(Predicate)} it would look something like:
	 * 
	 * 
	 * 
	 * List<Integer> numbers = Arrays.asList(1, -1, -2);
	 * Optional<Integer> invalidNumber = Iterables.tryFind(numbers, Predicates.not(ABOVE_ZERO));
	 * if(invalidNumber.isPresent())
	 *   throw new IllegalArgumentException("'" + invalidNumber.get() + "' is not " + ABOVE_ZERO);
	 * 
	 * 
* * With {@link #listPredicate(Predicate)}: * *
	 * 
	 * Predicates2.listPredicate(ABOVE_ZERO).apply(numbers);
	 * 
	 * 
* * As this breaks the general contract of {@link Predicate#apply(Object)} it should be * considered useful for validating arguments only. * The detail message only mentions the first element that didn't return true. */ public static Predicate> listPredicate(Predicate elementLimiter) { checkNotNull(elementLimiter); if(elementLimiter == alwaysTrue()) return alwaysTrue(); return new ListPredicate(elementLimiter); } private static final class ListPredicate implements Predicate> { private final Predicate elementLimiter; private ListPredicate(Predicate elementLimiter) { this.elementLimiter = elementLimiter; } @Override public boolean apply(@Nonnull List values) { for(E value : values) { if(!elementLimiter.apply(value)) throw Descriptions.illegalArgument(Descriptions.format("'%s' is not %s", value, elementLimiter)); } return true; } @Override public String toString() { return elementLimiter.toString(); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy