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 * https://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; 020 021import org.apache.commons.collections4.Closure; 022import org.apache.commons.collections4.Transformer; 023 024/** 025 * Closure implementation that calls a Transformer using the input object 026 * and ignore the result. 027 * 028 * @param <T> The type of the input to the operation. 029 * @since 3.0 030 */ 031public class TransformerClosure<T> implements Closure<T>, Serializable { 032 033 /** Serial version UID */ 034 private static final long serialVersionUID = -5194992589193388969L; 035 036 /** 037 * Factory method that performs validation. 038 * <p> 039 * A null transformer will return the {@code NOPClosure}. 040 * </p> 041 * 042 * @param <E> The type that the closure acts on 043 * @param transformer The transformer to call, null means nop 044 * @return The {@code transformer} closure 045 */ 046 public static <E> Closure<E> transformerClosure(final Transformer<? super E, ?> transformer) { 047 if (transformer == null) { 048 return NOPClosure.<E>nopClosure(); 049 } 050 return new TransformerClosure<>(transformer); 051 } 052 053 /** The transformer to wrap */ 054 private final Transformer<? super T, ?> iTransformer; 055 056 /** 057 * Constructor that performs no validation. 058 * Use {@code transformerClosure} if you want that. 059 * 060 * @param transformer The transformer to call, not null 061 */ 062 public TransformerClosure(final Transformer<? super T, ?> transformer) { 063 iTransformer = transformer; 064 } 065 066 /** 067 * Executes the closure by calling the decorated transformer. 068 * 069 * @param input The input object 070 */ 071 @Override 072 public void execute(final T input) { 073 iTransformer.apply(input); 074 } 075 076 /** 077 * Gets the transformer. 078 * 079 * @return The transformer 080 * @since 3.1 081 */ 082 public Transformer<? super T, ?> getTransformer() { 083 return iTransformer; 084 } 085 086}