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