    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.Objects;
    021
    022import org.apache.commons.collections4.Closure;
    023import org.apache.commons.collections4.Transformer;
    024
    025/**
    026 * Transformer implementation that calls a Closure using the input object
    027 * and then returns the input.
    028 *
    029 * @since 3.0
    030 */
    031public class ClosureTransformer<T> implements Transformer<T, T>, Serializable {
    032
    033    /** Serial version UID */
    034    private static final long serialVersionUID = 478466901448617286L;
    035
    036    /**
    037     * Factory method that performs validation.
    038     *
    039     * @param <T>  the type of the object to transform
    040     * @param closure  the closure to call, not null
    041     * @return the {@code closure} transformer
    042     * @throws NullPointerException if the closure is null
    043     */
    044    public static <T> Transformer<T, T> closureTransformer(final Closure<? super T> closure) {
    045        return new ClosureTransformer<>(Objects.requireNonNull(closure, "closure"));
    046    }
    047
    048    /** The closure to wrap */
    049    private final Closure<? super T> iClosure;
    050
    051    /**
    052     * Constructor that performs no validation.
    053     * Use {@code closureTransformer} if you want that.
    054     *
    055     * @param closure  the closure to call, not null
    056     */
    057    public ClosureTransformer(final Closure<? super T> closure) {
    058        iClosure = closure;
    059    }
    060
    061    /**
    062     * Gets the closure.
    063     *
    064     * @return the closure
    065     * @since 3.1
    066     */
    067    public Closure<? super T> getClosure() {
    068        return iClosure;
    069    }
    070
    071    /**
    072     * Transforms the input to result by executing a closure.
    073     *
    074     * @param input  the input object to transform
    075     * @return the transformed result
    076     */
    077    @Override
    078    public T transform(final T input) {
    079        iClosure.execute(input);
    080        return input;
    081    }
    082
    083}