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.Collection; 021 022import org.apache.commons.collections4.Closure; 023 024/** 025 * Closure implementation that chains the specified closures together. 026 * 027 * @since 3.0 028 * @version $Id: ChainedClosure.html 972421 2015-11-14 20:00:04Z tn $ 029 */ 030public class ChainedClosure<E> implements Closure<E>, Serializable { 031 032 /** Serial version UID */ 033 private static final long serialVersionUID = -3520677225766901240L; 034 035 /** The closures to call in turn */ 036 private final Closure<? super E>[] iClosures; 037 038 /** 039 * Factory method that performs validation and copies the parameter array. 040 * 041 * @param <E> the type that the closure acts on 042 * @param closures the closures to chain, copied, no nulls 043 * @return the <code>chained</code> closure 044 * @throws IllegalArgumentException if the closures array is null 045 * @throws IllegalArgumentException if any closure in the array is null 046 */ 047 public static <E> Closure<E> chainedClosure(final Closure<? super E>... closures) { 048 FunctorUtils.validate(closures); 049 if (closures.length == 0) { 050 return NOPClosure.<E>nopClosure(); 051 } 052 return new ChainedClosure<E>(closures); 053 } 054 055 /** 056 * Create a new Closure that calls each closure in turn, passing the 057 * result into the next closure. The ordering is that of the iterator() 058 * method on the collection. 059 * 060 * @param <E> the type that the closure acts on 061 * @param closures a collection of closures to chain 062 * @return the <code>chained</code> closure 063 * @throws IllegalArgumentException if the closures collection is null 064 * @throws IllegalArgumentException if any closure in the collection is null 065 */ 066 @SuppressWarnings("unchecked") 067 public static <E> Closure<E> chainedClosure(final Collection<Closure<E>> closures) { 068 if (closures == null) { 069 throw new IllegalArgumentException("Closure collection must not be null"); 070 } 071 if (closures.size() == 0) { 072 return NOPClosure.<E>nopClosure(); 073 } 074 // convert to array like this to guarantee iterator() ordering 075 final Closure<? super E>[] cmds = new Closure[closures.size()]; 076 int i = 0; 077 for (final Closure<? super E> closure : closures) { 078 cmds[i++] = closure; 079 } 080 FunctorUtils.validate(cmds); 081 return new ChainedClosure<E>(false, cmds); 082 } 083 084 /** 085 * Hidden constructor for the use by the static factory methods. 086 * 087 * @param clone if {@code true} the input argument will be cloned 088 * @param closures the closures to chain, no nulls 089 */ 090 private ChainedClosure(final boolean clone, final Closure<? super E>... closures) { 091 super(); 092 iClosures = clone ? FunctorUtils.copy(closures) : closures; 093 } 094 095 /** 096 * Constructor that performs no validation. 097 * Use <code>chainedClosure</code> if you want that. 098 * 099 * @param closures the closures to chain, copied, no nulls 100 */ 101 public ChainedClosure(final Closure<? super E>... closures) { 102 this(true, closures); 103 } 104 105 /** 106 * Execute a list of closures. 107 * 108 * @param input the input object passed to each closure 109 */ 110 public void execute(final E input) { 111 for (final Closure<? super E> iClosure : iClosures) { 112 iClosure.execute(input); 113 } 114 } 115 116 /** 117 * Gets the closures. 118 * 119 * @return a copy of the closures 120 * @since 3.1 121 */ 122 public Closure<? super E>[] getClosures() { 123 return FunctorUtils.<E>copy(iClosures); 124 } 125 126}