ConstantTransformer.java

  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. import java.io.Serializable;
  19. import java.util.Objects;

  20. import org.apache.commons.collections4.Transformer;

  21. /**
  22.  * Transformer implementation that returns the same constant each time.
  23.  * <p>
  24.  * No check is made that the object is immutable. In general, only immutable
  25.  * objects should use the constant factory. Mutable objects should
  26.  * use the prototype factory.
  27.  * </p>
  28.  *
  29.  * @param <T> the type of the input to the function.
  30.  * @param <R> the type of the result of the function.
  31.  * @since 3.0
  32.  */
  33. public class ConstantTransformer<T, R> implements Transformer<T, R>, Serializable {

  34.     /** Serial version UID */
  35.     private static final long serialVersionUID = 6374440726369055124L;

  36.     /** Returns null each time */
  37.     @SuppressWarnings("rawtypes")
  38.     public static final Transformer NULL_INSTANCE = new ConstantTransformer<>(null);

  39.     /**
  40.      * Transformer method that performs validation.
  41.      *
  42.      * @param <I>  the input type
  43.      * @param <O>  the output type
  44.      * @param constantToReturn  the constant object to return each time in the factory
  45.      * @return the {@code constant} factory.
  46.      */
  47.     public static <I, O> Transformer<I, O> constantTransformer(final O constantToReturn) {
  48.         if (constantToReturn == null) {
  49.             return nullTransformer();
  50.         }
  51.         return new ConstantTransformer<>(constantToReturn);
  52.     }

  53.     /**
  54.      * Gets a typed null instance.
  55.      *
  56.      * @param <I>  the input type
  57.      * @param <O>  the output type
  58.      * @return Transformer&lt;I, O&gt; that always returns null.
  59.      */
  60.     public static <I, O> Transformer<I, O> nullTransformer() {
  61.         return NULL_INSTANCE;
  62.     }

  63.     /** The closures to call in turn */
  64.     private final R iConstant;

  65.     /**
  66.      * Constructor that performs no validation.
  67.      * Use {@code constantTransformer} if you want that.
  68.      *
  69.      * @param constantToReturn  the constant to return each time
  70.      */
  71.     public ConstantTransformer(final R constantToReturn) {
  72.         iConstant = constantToReturn;
  73.     }

  74.     /**
  75.      * {@inheritDoc}
  76.      */
  77.     @Override
  78.     public boolean equals(final Object obj) {
  79.         if (obj == this) {
  80.             return true;
  81.         }
  82.         if (!(obj instanceof ConstantTransformer)) {
  83.             return false;
  84.         }
  85.         final Object otherConstant = ((ConstantTransformer<?, ?>) obj).getConstant();
  86.         return Objects.equals(otherConstant, getConstant());
  87.     }

  88.     /**
  89.      * Gets the constant.
  90.      *
  91.      * @return the constant
  92.      * @since 3.1
  93.      */
  94.     public R getConstant() {
  95.         return iConstant;
  96.     }

  97.     /**
  98.      * {@inheritDoc}
  99.      */
  100.     @Override
  101.     public int hashCode() {
  102.         int result = "ConstantTransformer".hashCode() << 2;
  103.         if (getConstant() != null) {
  104.             result |= getConstant().hashCode();
  105.         }
  106.         return result;
  107.     }

  108.     /**
  109.      * Transforms the input by ignoring it and returning the stored constant instead.
  110.      *
  111.      * @param input  the input object which is ignored
  112.      * @return the stored constant
  113.      */
  114.     @Override
  115.     public R transform(final T input) {
  116.         return iConstant;
  117.     }
  118. }