001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.collections4.functors; 018 019import java.io.Serializable; 020import java.util.Objects; 021 022import org.apache.commons.collections4.Predicate; 023import org.apache.commons.collections4.Transformer; 024 025/** 026 * Predicate implementation that transforms the given object before invoking 027 * another {@code Predicate}. 028 * 029 * @param <T> the type of the input to the predicate. 030 * @since 3.1 031 */ 032public final class TransformedPredicate<T> extends AbstractPredicate<T> implements PredicateDecorator<T>, Serializable { 033 034 /** Serial version UID */ 035 private static final long serialVersionUID = -5596090919668315834L; 036 037 /** 038 * Creates the predicate. 039 * 040 * @param <T> the type that the predicate queries 041 * @param transformer the transformer to call 042 * @param predicate the predicate to call with the result of the transform 043 * @return the predicate 044 * @throws NullPointerException if the transformer or the predicate is null 045 */ 046 public static <T> Predicate<T> transformedPredicate(final Transformer<? super T, ? extends T> transformer, 047 final Predicate<? super T> predicate) { 048 return new TransformedPredicate<>(Objects.requireNonNull(transformer, "transformer"), 049 Objects.requireNonNull(predicate, "predicate")); 050 } 051 052 /** The transformer to call */ 053 private final Transformer<? super T, ? extends T> iTransformer; 054 055 /** The predicate to call */ 056 private final Predicate<? super T> iPredicate; 057 058 /** 059 * Constructor that performs no validation. 060 * Use {@code transformedPredicate} if you want that. 061 * 062 * @param transformer the transformer to use 063 * @param predicate the predicate to decorate 064 */ 065 public TransformedPredicate(final Transformer<? super T, ? extends T> transformer, 066 final Predicate<? super T> predicate) { 067 iTransformer = transformer; 068 iPredicate = predicate; 069 } 070 071 /** 072 * Gets the predicate being decorated. 073 * 074 * @return the predicate as the only element in an array 075 * @since 3.1 076 */ 077 @Override 078 @SuppressWarnings("unchecked") 079 public Predicate<? super T>[] getPredicates() { 080 return new Predicate[] {iPredicate}; 081 } 082 083 /** 084 * Gets the transformer in use. 085 * 086 * @return the transformer 087 */ 088 public Transformer<? super T, ? extends T> getTransformer() { 089 return iTransformer; 090 } 091 092 /** 093 * Evaluates the predicate returning the result of the decorated predicate 094 * once the input has been transformed 095 * 096 * @param object the input object which will be transformed 097 * @return true if decorated predicate returns true 098 */ 099 @Override 100 public boolean test(final T object) { 101 final T result = iTransformer.apply(object); 102 return iPredicate.test(result); 103 } 104 105}