View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.functors;
18  
19  import java.io.Serializable;
20  import java.util.Objects;
21  
22  import org.apache.commons.collections4.Factory;
23  import org.apache.commons.collections4.Transformer;
24  
25  /**
26   * Transformer implementation that calls a Factory and returns the result.
27   *
28   * @param <T> the type of the input to the function.
29   * @param <R> the type of the result of the function.
30   * @since 3.0
31   */
32  public class FactoryTransformer<T, R> implements Transformer<T, R>, Serializable {
33  
34      /** Serial version UID */
35      private static final long serialVersionUID = -6817674502475353160L;
36  
37      /**
38       * Factory method that performs validation.
39       *
40       * @param <I>  the input type
41       * @param <O>  the output type
42       * @param factory  the factory to call, not null
43       * @return the {@code factory} transformer
44       * @throws NullPointerException if the factory is null
45       */
46      public static <I, O> Transformer<I, O> factoryTransformer(final Factory<? extends O> factory) {
47          return new FactoryTransformer<>(Objects.requireNonNull(factory, "factory"));
48      }
49  
50      /** The factory to wrap */
51      private final Factory<? extends R> iFactory;
52  
53      /**
54       * Constructor that performs no validation.
55       * Use {@code factoryTransformer} if you want that.
56       *
57       * @param factory  the factory to call, not null
58       */
59      public FactoryTransformer(final Factory<? extends R> factory) {
60          iFactory = factory;
61      }
62  
63      /**
64       * Gets the factory.
65       *
66       * @return the factory
67       * @since 3.1
68       */
69      public Factory<? extends R> getFactory() {
70          return iFactory;
71      }
72  
73      /**
74       * Transforms the input by ignoring the input and returning the result of
75       * calling the decorated factory.
76       *
77       * @param input  the input object to transform
78       * @return the transformed result
79       */
80      @Override
81      public R transform(final T input) {
82          return iFactory.get();
83      }
84  
85  }