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