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 * @param <T> the type of the input to the operation. 30 * @since 3.0 31 */ 32 public class IfClosure<T> implements Closure<T>, Serializable { 33 34 /** Serial version UID */ 35 private static final long serialVersionUID = 3518477308466486130L; 36 37 /** 38 * Factory method that performs validation. 39 * <p> 40 * This factory creates a closure that performs no action when 41 * the predicate is false. 42 * 43 * @param <E> the type that the closure acts on 44 * @param predicate predicate to switch on 45 * @param trueClosure closure used if true 46 * @return the {@code if} closure 47 * @throws NullPointerException if either argument is null 48 * @since 3.2 49 */ 50 public static <E> Closure<E> ifClosure(final Predicate<? super E> predicate, final Closure<? super E> trueClosure) { 51 return IfClosure.<E>ifClosure(predicate, trueClosure, NOPClosure.<E>nopClosure()); 52 } 53 /** 54 * Factory method that performs validation. 55 * 56 * @param <E> the type that the closure acts on 57 * @param predicate predicate to switch on 58 * @param trueClosure closure used if true 59 * @param falseClosure closure used if false 60 * @return the {@code if} closure 61 * @throws NullPointerException if any argument is null 62 */ 63 public static <E> Closure<E> ifClosure(final Predicate<? super E> predicate, 64 final Closure<? super E> trueClosure, 65 final Closure<? super E> falseClosure) { 66 return new IfClosure<>(Objects.requireNonNull(predicate, "predicate"), 67 Objects.requireNonNull(trueClosure, "trueClosure"), 68 Objects.requireNonNull(falseClosure, "falseClosure")); 69 } 70 /** The test */ 71 private final Predicate<? super T> iPredicate; 72 73 /** The closure to use if true */ 74 private final Closure<? super T> iTrueClosure; 75 76 /** The closure to use if false */ 77 private final Closure<? super T> iFalseClosure; 78 79 /** 80 * Constructor that performs no validation. 81 * Use {@code ifClosure} if you want that. 82 * <p> 83 * This constructor creates a closure that performs no action when 84 * the predicate is false. 85 * 86 * @param predicate predicate to switch on, not null 87 * @param trueClosure closure used if true, not null 88 * @since 3.2 89 */ 90 public IfClosure(final Predicate<? super T> predicate, final Closure<? super T> trueClosure) { 91 this(predicate, trueClosure, NOPClosure.nopClosure()); 92 } 93 94 /** 95 * Constructor that performs no validation. 96 * Use {@code ifClosure} if you want that. 97 * 98 * @param predicate predicate to switch on, not null 99 * @param trueClosure closure used if true, not null 100 * @param falseClosure closure used if false, not null 101 */ 102 public IfClosure(final Predicate<? super T> predicate, final Closure<? super T> trueClosure, 103 final Closure<? super T> falseClosure) { 104 iPredicate = predicate; 105 iTrueClosure = trueClosure; 106 iFalseClosure = falseClosure; 107 } 108 109 /** 110 * Executes the true or false closure according to the result of the predicate. 111 * 112 * @param input the input object 113 */ 114 @Override 115 public void execute(final T input) { 116 if (iPredicate.test(input)) { 117 iTrueClosure.accept(input); 118 } else { 119 iFalseClosure.accept(input); 120 } 121 } 122 123 /** 124 * Gets the closure called when false. 125 * 126 * @return the closure 127 * @since 3.1 128 */ 129 public Closure<? super T> getFalseClosure() { 130 return iFalseClosure; 131 } 132 133 /** 134 * Gets the predicate. 135 * 136 * @return the predicate 137 * @since 3.1 138 */ 139 public Predicate<? super T> getPredicate() { 140 return iPredicate; 141 } 142 143 /** 144 * Gets the closure called when true. 145 * 146 * @return the closure 147 * @since 3.1 148 */ 149 public Closure<? super T> getTrueClosure() { 150 return iTrueClosure; 151 } 152 153 }