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