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