    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 *      https://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.Closure;
    023import org.apache.commons.collections4.Predicate;
    024
    025/**
    026 * Closure implementation acts as an if statement calling one or other closure
    027 * based on a predicate.
    028 *
    029 * @param <T> The type of the input to the operation.
    030 * @since 3.0
    031 */
    032public class IfClosure<T> implements Closure<T>, Serializable {
    033
    034    /** Serial version UID */
    035    private static final long serialVersionUID = 3518477308466486130L;
    036
    037    /**
    038     * Factory method that performs validation.
    039     * <p>
    040     * This factory creates a closure that performs no action when
    041     * the predicate is false.
    042     * </p>
    043     *
    044     * @param <E> The type that the closure acts on
    045     * @param predicate  predicate to switch on
    046     * @param trueClosure  closure used if true
    047     * @return The {@code if} closure
    048     * @throws NullPointerException if either argument is null
    049     * @since 3.2
    050     */
    051    public static <E> Closure<E> ifClosure(final Predicate<? super E> predicate, final Closure<? super E> trueClosure) {
    052        return IfClosure.<E>ifClosure(predicate, trueClosure, NOPClosure.<E>nopClosure());
    053    }
    054
    055    /**
    056     * Factory method that performs validation.
    057     *
    058     * @param <E> The type that the closure acts on
    059     * @param predicate  predicate to switch on
    060     * @param trueClosure  closure used if true
    061     * @param falseClosure  closure used if false
    062     * @return The {@code if} closure
    063     * @throws NullPointerException if any argument is null
    064     */
    065    public static <E> Closure<E> ifClosure(final Predicate<? super E> predicate,
    066                                           final Closure<? super E> trueClosure,
    067                                           final Closure<? super E> falseClosure) {
    068        return new IfClosure<>(Objects.requireNonNull(predicate, "predicate"),
    069                Objects.requireNonNull(trueClosure, "trueClosure"),
    070                Objects.requireNonNull(falseClosure, "falseClosure"));
    071    }
    072
    073    /** The test */
    074    private final Predicate<? super T> iPredicate;
    075
    076    /** The closure to use if true */
    077    private final Closure<? super T> iTrueClosure;
    078
    079    /** The closure to use if false */
    080    private final Closure<? super T> iFalseClosure;
    081
    082    /**
    083     * Constructor that performs no validation.
    084     * Use {@code ifClosure} if you want that.
    085     * <p>
    086     * This constructor creates a closure that performs no action when
    087     * the predicate is false.
    088     * </p>
    089     *
    090     * @param predicate  predicate to switch on, not null
    091     * @param trueClosure  closure used if true, not null
    092     * @since 3.2
    093     */
    094    public IfClosure(final Predicate<? super T> predicate, final Closure<? super T> trueClosure) {
    095        this(predicate, trueClosure, NOPClosure.nopClosure());
    096    }
    097
    098    /**
    099     * Constructor that performs no validation.
    100     * Use {@code ifClosure} if you want that.
    101     *
    102     * @param predicate  predicate to switch on, not null
    103     * @param trueClosure  closure used if true, not null
    104     * @param falseClosure  closure used if false, not null
    105     */
    106    public IfClosure(final Predicate<? super T> predicate, final Closure<? super T> trueClosure,
    107                     final Closure<? super T> falseClosure) {
    108        iPredicate = predicate;
    109        iTrueClosure = trueClosure;
    110        iFalseClosure = falseClosure;
    111    }
    112
    113    /**
    114     * Executes the true or false closure according to the result of the predicate.
    115     *
    116     * @param input  The input object
    117     */
    118    @Override
    119    public void execute(final T input) {
    120        if (iPredicate.test(input)) {
    121            iTrueClosure.accept(input);
    122        } else {
    123            iFalseClosure.accept(input);
    124        }
    125    }
    126
    127    /**
    128     * Gets the closure called when false.
    129     *
    130     * @return The closure
    131     * @since 3.1
    132     */
    133    public Closure<? super T> getFalseClosure() {
    134        return iFalseClosure;
    135    }
    136
    137    /**
    138     * Gets the predicate.
    139     *
    140     * @return The predicate
    141     * @since 3.1
    142     */
    143    public Predicate<? super T> getPredicate() {
    144        return iPredicate;
    145    }
    146
    147    /**
    148     * Gets the closure called when true.
    149     *
    150     * @return The closure
    151     * @since 3.1
    152     */
    153    public Closure<? super T> getTrueClosure() {
    154        return iTrueClosure;
    155    }
    156
    157}