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.collections.functors;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.collections.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   *
30   * @since 3.0
31   * @version $Id: ConstantTransformer.java 1435965 2013-01-20 21:13:18Z tn $
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      public static final Transformer<Object, Object> NULL_INSTANCE = new ConstantTransformer<Object, Object>(null);
40  
41      /** The closures to call in turn */
42      private final O iConstant;
43  
44      /**
45       * Get a typed null instance.
46       *
47       * @param <I>  the input type
48       * @param <O>  the output type
49       * @return Transformer<I, O> that always returns null.
50       */
51      @SuppressWarnings("unchecked")
52      public static <I, O> Transformer<I, O> nullTransformer() {
53          return (Transformer<I, O>) NULL_INSTANCE;
54      }
55  
56      /**
57       * Transformer method that performs validation.
58       *
59       * @param <I>  the input type
60       * @param <O>  the output type
61       * @param constantToReturn  the constant object to return each time in the factory
62       * @return the <code>constant</code> factory.
63       */
64      public static <I, O> Transformer<I, O> constantTransformer(final O constantToReturn) {
65          if (constantToReturn == null) {
66              return nullTransformer();
67          }
68          return new ConstantTransformer<I, O>(constantToReturn);
69      }
70      
71      /**
72       * Constructor that performs no validation.
73       * Use <code>getInstance</code> if you want that.
74       * 
75       * @param constantToReturn  the constant to return each time
76       */
77      public ConstantTransformer(final O constantToReturn) {
78          super();
79          iConstant = constantToReturn;
80      }
81  
82      /**
83       * Transforms the input by ignoring it and returning the stored constant instead.
84       * 
85       * @param input  the input object which is ignored
86       * @return the stored constant
87       */
88      public O transform(final I input) {
89          return iConstant;
90      }
91  
92      /**
93       * Gets the constant.
94       * 
95       * @return the constant
96       * @since 3.1
97       */
98      public O getConstant() {
99          return iConstant;
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public boolean equals(final Object obj) {
107         if (obj == this) {
108             return true;
109         }
110         if (obj instanceof ConstantTransformer == false) {
111             return false;
112         }
113         final Object otherConstant = ((ConstantTransformer<?, ?>) obj).getConstant();
114         return otherConstant == getConstant() || otherConstant != null && otherConstant.equals(getConstant());
115     }
116 
117     /**
118      * {@inheritDoc}
119      */
120     @Override
121     public int hashCode() {
122         int result = "ConstantTransformer".hashCode() << 2;
123         if (getConstant() != null) {
124             result |= getConstant().hashCode();
125         }
126         return result;
127     }
128 }