ConstantInitializer.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.lang3.concurrent;

  18. import java.util.Objects;

  19. /**
  20.  * A very simple implementation of the {@link ConcurrentInitializer} interface
  21.  * which always returns the same object.
  22.  *
  23.  * <p>
  24.  * An instance of this class is passed a reference to an object when it is
  25.  * constructed. The {@link #get()} method just returns this object. No
  26.  * synchronization is required.
  27.  * </p>
  28.  * <p>
  29.  * This class is useful for instance for unit testing or in cases where a
  30.  * specific object has to be passed to an object which expects a
  31.  * {@link ConcurrentInitializer}.
  32.  * </p>
  33.  *
  34.  * @since 3.0
  35.  * @param <T> the type of the object managed by this initializer
  36.  */
  37. public class ConstantInitializer<T> implements ConcurrentInitializer<T> {

  38.     /** Constant for the format of the string representation. */
  39.     private static final String FMT_TO_STRING = "ConstantInitializer@%d [ object = %s ]";

  40.     /** Stores the managed object. */
  41.     private final T object;

  42.     /**
  43.      * Creates a new instance of {@link ConstantInitializer} and initializes it
  44.      * with the object to be managed. The {@code get()} method will always
  45.      * return the object passed here. This class does not place any restrictions
  46.      * on the object. It may be <b>null</b>, then {@code get()} will return
  47.      * <b>null</b>, too.
  48.      *
  49.      * @param obj the object to be managed by this initializer
  50.      */
  51.     public ConstantInitializer(final T obj) {
  52.         object = obj;
  53.     }

  54.     /**
  55.      * Compares this object with another one. This implementation returns
  56.      * <b>true</b> if and only if the passed in object is an instance of
  57.      * {@link ConstantInitializer} which refers to an object equals to the
  58.      * object managed by this instance.
  59.      *
  60.      * @param obj the object to compare to
  61.      * @return a flag whether the objects are equal
  62.      */
  63.     @Override
  64.     public boolean equals(final Object obj) {
  65.         if (this == obj) {
  66.             return true;
  67.         }
  68.         if (!(obj instanceof ConstantInitializer<?>)) {
  69.             return false;
  70.         }

  71.         final ConstantInitializer<?> c = (ConstantInitializer<?>) obj;
  72.         return Objects.equals(getObject(), c.getObject());
  73.     }

  74.     /**
  75.      * Returns the object managed by this initializer. This implementation just
  76.      * returns the object passed to the constructor.
  77.      *
  78.      * @return the object managed by this initializer
  79.      * @throws ConcurrentException if an error occurs
  80.      */
  81.     @Override
  82.     public T get() throws ConcurrentException {
  83.         return getObject();
  84.     }

  85.     /**
  86.      * Directly returns the object that was passed to the constructor. This is
  87.      * the same object as returned by {@code get()}. However, this method does
  88.      * not declare that it throws an exception.
  89.      *
  90.      * @return the object managed by this initializer
  91.      */
  92.     public final T getObject() {
  93.         return object;
  94.     }

  95.     /**
  96.      * Returns a hash code for this object. This implementation returns the hash
  97.      * code of the managed object.
  98.      *
  99.      * @return a hash code for this object
  100.      */
  101.     @Override
  102.     public int hashCode() {
  103.         return Objects.hashCode(object);
  104.     }

  105.     /**
  106.      * As a {@link ConstantInitializer} is initialized on construction this will
  107.      * always return true.
  108.      *
  109.      * @return true.
  110.      * @since 3.14.0
  111.      */
  112.     public boolean isInitialized() {
  113.         return true;
  114.     }

  115.     /**
  116.      * Returns a string representation for this object. This string also
  117.      * contains a string representation of the object managed by this
  118.      * initializer.
  119.      *
  120.      * @return a string for this object
  121.      */
  122.     @Override
  123.     public String toString() {
  124.         return String.format(FMT_TO_STRING, Integer.valueOf(System.identityHashCode(this)), getObject());
  125.     }
  126. }