net.shibboleth.utilities.java.support.logic.StrategyIndirectedPredicate Maven / Gradle / Ivy
/*
* Licensed to the University Corporation for Advanced Internet Development,
* Inc. (UCAID) under one or more contributor license agreements. See the
* NOTICE file distributed with this work for additional information regarding
* copyright ownership. The UCAID licenses this file to You 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 net.shibboleth.utilities.java.support.logic;
import java.util.Collection;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.shibboleth.utilities.java.support.annotation.constraint.NonnullElements;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
/**
* Generic predicate that checks a candidate {@link Object} returned by a lookup function
* against an injected predicate.
*
* @param type of object used as the source of the data to compare
* @param type of object being compared
*/
public class StrategyIndirectedPredicate implements Predicate {
/** Lookup strategy for object. */
@Nonnull private final Function objectLookupStrategy;
/** Predicate to apply to indirected object. */
@Nonnull private final Predicate predicate;
/**
* Constructor.
*
* @param objectStrategy lookup strategy for object
* @param pred the predicate to apply
*/
public StrategyIndirectedPredicate(@Nonnull final Function objectStrategy,
@Nonnull final Predicate pred) {
objectLookupStrategy = Constraint.isNotNull(objectStrategy, "Object lookup strategy cannot be null");
predicate = Constraint.isNotNull(pred, "Predicate cannot be null");
}
/**
* Constructor that simplifies constructing a test for containment in a collection, which
* is a common use case.
*
* @param objectStrategy lookup strategy for object
* @param collection a collection to test for containment
*/
public StrategyIndirectedPredicate(@Nonnull final Function objectStrategy,
@Nonnull @NonnullElements final Collection collection) {
objectLookupStrategy = Constraint.isNotNull(objectStrategy, "Object lookup strategy cannot be null");
predicate = Predicates.in(collection);
}
/** {@inheritDoc} */
@Override
public boolean apply(@Nullable final T1 input) {
return predicate.apply(objectLookupStrategy.apply(input));
}
}