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