1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.collections4.functors; 18 19 import java.io.Serializable; 20 import java.util.Objects; 21 22 import org.apache.commons.collections4.Closure; 23 import org.apache.commons.collections4.Predicate; 24 25 /** 26 * Closure implementation acts as an if statement calling one or other closure 27 * based on a predicate. 28 * 29 * @since 3.0 30 */ 31 public class IfClosure<E> implements Closure<E>, Serializable { 32 33 /** Serial version UID */ 34 private static final long serialVersionUID = 3518477308466486130L; 35 36 /** 37 * Factory method that performs validation. 38 * <p> 39 * This factory creates a closure that performs no action when 40 * the predicate is false. 41 * 42 * @param <E> the type that the closure acts on 43 * @param predicate predicate to switch on 44 * @param trueClosure closure used if true 45 * @return the {@code if} closure 46 * @throws NullPointerException if either argument is null 47 * @since 3.2 48 */ 49 public static <E> Closure<E> ifClosure(final Predicate<? super E> predicate, final Closure<? super E> trueClosure) { 50 return IfClosure.<E>ifClosure(predicate, trueClosure, NOPClosure.<E>nopClosure()); 51 } 52 /** 53 * Factory method that performs validation. 54 * 55 * @param <E> the type that the closure acts on 56 * @param predicate predicate to switch on 57 * @param trueClosure closure used if true 58 * @param falseClosure closure used if false 59 * @return the {@code if} closure 60 * @throws NullPointerException if any argument is null 61 */ 62 public static <E> Closure<E> ifClosure(final Predicate<? super E> predicate, 63 final Closure<? super E> trueClosure, 64 final Closure<? super E> falseClosure) { 65 return new IfClosure<>(Objects.requireNonNull(predicate, "predicate"), 66 Objects.requireNonNull(trueClosure, "trueClosure"), 67 Objects.requireNonNull(falseClosure, "falseClosure")); 68 } 69 /** The test */ 70 private final Predicate<? super E> iPredicate; 71 72 /** The closure to use if true */ 73 private final Closure<? super E> iTrueClosure; 74 75 /** The closure to use if false */ 76 private final Closure<? super E> iFalseClosure; 77 78 /** 79 * Constructor that performs no validation. 80 * Use {@code ifClosure} if you want that. 81 * <p> 82 * This constructor creates a closure that performs no action when 83 * the predicate is false. 84 * 85 * @param predicate predicate to switch on, not null 86 * @param trueClosure closure used if true, not null 87 * @since 3.2 88 */ 89 public IfClosure(final Predicate<? super E> predicate, final Closure<? super E> trueClosure) { 90 this(predicate, trueClosure, NOPClosure.nopClosure()); 91 } 92 93 /** 94 * Constructor that performs no validation. 95 * Use {@code ifClosure} if you want that. 96 * 97 * @param predicate predicate to switch on, not null 98 * @param trueClosure closure used if true, not null 99 * @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 }