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