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