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 21 import org.apache.commons.collections4.Closure; 22 import org.apache.commons.collections4.Transformer; 23 24 /** 25 * Closure implementation that calls a Transformer using the input object 26 * and ignore the result. 27 * 28 * @since 3.0 29 */ 30 public class TransformerClosure<E> implements Closure<E>, Serializable { 31 32 /** Serial version UID */ 33 private static final long serialVersionUID = -5194992589193388969L; 34 35 /** 36 * Factory method that performs validation. 37 * <p> 38 * A null transformer will return the {@code NOPClosure}. 39 * 40 * @param <E> the type that the closure acts on 41 * @param transformer the transformer to call, null means nop 42 * @return the {@code transformer} closure 43 */ 44 public static <E> Closure<E> transformerClosure(final Transformer<? super E, ?> transformer) { 45 if (transformer == null) { 46 return NOPClosure.<E>nopClosure(); 47 } 48 return new TransformerClosure<>(transformer); 49 } 50 51 /** The transformer to wrap */ 52 private final Transformer<? super E, ?> iTransformer; 53 54 /** 55 * Constructor that performs no validation. 56 * Use {@code transformerClosure} if you want that. 57 * 58 * @param transformer the transformer to call, not null 59 */ 60 public TransformerClosure(final Transformer<? super E, ?> transformer) { 61 iTransformer = transformer; 62 } 63 64 /** 65 * Executes the closure by calling the decorated transformer. 66 * 67 * @param input the input object 68 */ 69 @Override 70 public void execute(final E input) { 71 iTransformer.transform(input); 72 } 73 74 /** 75 * Gets the transformer. 76 * 77 * @return the transformer 78 * @since 3.1 79 */ 80 public Transformer<? super E, ?> getTransformer() { 81 return iTransformer; 82 } 83 84 }