001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * https://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.lang3.concurrent; 018 019import java.util.Objects; 020 021/** 022 * A very simple implementation of the {@link ConcurrentInitializer} interface 023 * which always returns the same object. 024 * 025 * <p> 026 * An instance of this class is passed a reference to an object when it is 027 * constructed. The {@link #get()} method just returns this object. No 028 * synchronization is required. 029 * </p> 030 * <p> 031 * This class is useful for instance for unit testing or in cases where a 032 * specific object has to be passed to an object which expects a 033 * {@link ConcurrentInitializer}. 034 * </p> 035 * 036 * @param <T> The type of the object managed by this initializer 037 * @since 3.0 038 */ 039public class ConstantInitializer<T> implements ConcurrentInitializer<T> { 040 041 /** Constant for the format of the string representation. */ 042 private static final String FMT_TO_STRING = "ConstantInitializer@%d [ object = %s ]"; 043 044 /** Stores the managed object. */ 045 private final T object; 046 047 /** 048 * Creates a new instance of {@link ConstantInitializer} and initializes it 049 * with the object to be managed. The {@code get()} method will always 050 * return the object passed here. This class does not place any restrictions 051 * on the object. It may be {@code null}, then {@code get()} will return 052 * {@code null}, too. 053 * 054 * @param obj The object to be managed by this initializer 055 */ 056 public ConstantInitializer(final T obj) { 057 object = obj; 058 } 059 060 /** 061 * Compares this object with another one. This implementation returns 062 * <strong>true</strong> if and only if the passed in object is an instance of 063 * {@link ConstantInitializer} which refers to an object equals to the 064 * object managed by this instance. 065 * 066 * @param obj The object to compare to 067 * @return A flag whether the objects are equal 068 */ 069 @Override 070 public boolean equals(final Object obj) { 071 if (this == obj) { 072 return true; 073 } 074 if (!(obj instanceof ConstantInitializer<?>)) { 075 return false; 076 } 077 078 final ConstantInitializer<?> c = (ConstantInitializer<?>) obj; 079 return Objects.equals(getObject(), c.getObject()); 080 } 081 082 /** 083 * Gets the object managed by this initializer. This implementation just 084 * returns the object passed to the constructor. 085 * 086 * @return The object managed by this initializer 087 * @throws ConcurrentException Thrown if an error occurs. 088 */ 089 @Override 090 public T get() throws ConcurrentException { 091 return getObject(); 092 } 093 094 /** 095 * Gets the object passed to the constructor. This is the same object returned by {@code get()}, but this method does not declare a checked exception. 096 * 097 * @return The object managed by this initializer 098 */ 099 public final T getObject() { 100 return object; 101 } 102 103 /** 104 * Returns a hash code for this object. This implementation returns the hash 105 * code of the managed object. 106 * 107 * @return A hash code for this object 108 */ 109 @Override 110 public int hashCode() { 111 return Objects.hashCode(object); 112 } 113 114 /** 115 * Tests whether this initializer is initialized. Always returns true because initialization occurs during construction. 116 * 117 * @return true. 118 * @since 3.14.0 119 */ 120 public boolean isInitialized() { 121 return true; 122 } 123 124 /** 125 * Returns a string representation for this object. This string also 126 * contains a string representation of the object managed by this 127 * initializer. 128 * 129 * @return A string for this object 130 */ 131 @Override 132 public String toString() { 133 return String.format(FMT_TO_STRING, Integer.valueOf(System.identityHashCode(this)), getObject()); 134 } 135}