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    *      https://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  
18  package org.apache.commons.collections4;
19  
20  import java.util.function.Function;
21  
22  /**
23   * Defines a functor interface implemented by classes that transform one object into another.
24   * <p>
25   * A {@code Transformer} converts the input object to the output object. The input object SHOULD be left unchanged. Transformers are typically used for type
26   * conversions, or extracting data from an object.
27   * </p>
28   * <p>
29   * Standard implementations of common transformers are provided by {@link TransformerUtils}. These include method invocation, returning a constant, cloning and
30   * returning the string value.
31   * </p>
32   *
33   * @param <T> The type of the input to the function.
34   * @param <R> The type of the result of the function.
35   * @since 1.0 This will be deprecated in 5.0 in favor of {@link Function}.
36   */
37  //@Deprecated
38  @FunctionalInterface
39  public interface Transformer<T, R> extends Function<T, R> {
40  
41      @Override
42      default R apply(final T t) {
43          return transform(t);
44      }
45  
46      /**
47       * Transforms the input object into some output object.
48       * <p>
49       * The input object SHOULD be left unchanged.
50       * </p>
51       *
52       * @param input The object to be transformed, should be left unchanged.
53       * @return A transformed object.
54       * @throws ClassCastException       (runtime) if the input is the wrong class.
55       * @throws IllegalArgumentException (runtime) if the input is invalid.
56       * @throws FunctorException         (runtime) if the transform cannot be completed.
57       */
58      R transform(T input);
59  }