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  
21  import org.apache.commons.collections4.Transformer;
22  
23  /**
24   * Transformer implementation that returns the same constant each time.
25   * <p>
26   * No check is made that the object is immutable. In general, only immutable
27   * objects should use the constant factory. Mutable objects should
28   * use the prototype factory.
29   * </p>
30   *
31   * @since 3.0
32   */
33  public class ConstantTransformer<I, O> implements Transformer<I, O>, Serializable {
34  
35      /** Serial version UID */
36      private static final long serialVersionUID = 6374440726369055124L;
37  
38      /** Returns null each time */
39      @SuppressWarnings("rawtypes")
40      public static final Transformer NULL_INSTANCE = new ConstantTransformer<>(null);
41  
42      /**
43       * Transformer method that performs validation.
44       *
45       * @param <I>  the input type
46       * @param <O>  the output type
47       * @param constantToReturn  the constant object to return each time in the factory
48       * @return the {@code constant} factory.
49       */
50      public static <I, O> Transformer<I, O> constantTransformer(final O constantToReturn) {
51          if (constantToReturn == null) {
52              return nullTransformer();
53          }
54          return new ConstantTransformer<>(constantToReturn);
55      }
56  
57      /**
58       * Gets a typed null instance.
59       *
60       * @param <I>  the input type
61       * @param <O>  the output type
62       * @return Transformer&lt;I, O&gt; that always returns null.
63       */
64      public static <I, O> Transformer<I, O> nullTransformer() {
65          return NULL_INSTANCE;
66      }
67  
68      /** The closures to call in turn */
69      private final O iConstant;
70  
71      /**
72       * Constructor that performs no validation.
73       * Use {@code constantTransformer} if you want that.
74       *
75       * @param constantToReturn  the constant to return each time
76       */
77      public ConstantTransformer(final O constantToReturn) {
78          iConstant = constantToReturn;
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public boolean equals(final Object obj) {
86          if (obj == this) {
87              return true;
88          }
89          if (!(obj instanceof ConstantTransformer)) {
90              return false;
91          }
92          final Object otherConstant = ((ConstantTransformer<?, ?>) obj).getConstant();
93          return otherConstant == getConstant() || otherConstant != null && otherConstant.equals(getConstant());
94      }
95  
96      /**
97       * Gets the constant.
98       *
99       * @return the constant
100      * @since 3.1
101      */
102     public O getConstant() {
103         return iConstant;
104     }
105 
106     /**
107      * {@inheritDoc}
108      */
109     @Override
110     public int hashCode() {
111         int result = "ConstantTransformer".hashCode() << 2;
112         if (getConstant() != null) {
113             result |= getConstant().hashCode();
114         }
115         return result;
116     }
117 
118     /**
119      * Transforms the input by ignoring it and returning the stored constant instead.
120      *
121      * @param input  the input object which is ignored
122      * @return the stored constant
123      */
124     @Override
125     public O transform(final I input) {
126         return iConstant;
127     }
128 }