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 * http://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 * @since 3.0 037 * @param <T> the type of the object managed by this initializer 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 <b>null</b>, then {@code get()} will return 052 * <b>null</b>, 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 * <b>true</b> 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 * Returns 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 if an error occurs 088 */ 089 @Override 090 public T get() throws ConcurrentException { 091 return getObject(); 092 } 093 094 /** 095 * Directly returns the object that was passed to the constructor. This is 096 * the same object as returned by {@code get()}. However, this method does 097 * not declare that it throws an exception. 098 * 099 * @return the object managed by this initializer 100 */ 101 public final T getObject() { 102 return object; 103 } 104 105 /** 106 * Returns a hash code for this object. This implementation returns the hash 107 * code of the managed object. 108 * 109 * @return a hash code for this object 110 */ 111 @Override 112 public int hashCode() { 113 return Objects.hashCode(object); 114 } 115 116 /** 117 * As a {@link ConstantInitializer} is initialized on construction this will 118 * always return true. 119 * 120 * @return true. 121 * @since 3.14.0 122 */ 123 public boolean isInitialized() { 124 return true; 125 } 126 127 /** 128 * Returns a string representation for this object. This string also 129 * contains a string representation of the object managed by this 130 * initializer. 131 * 132 * @return a string for this object 133 */ 134 @Override 135 public String toString() { 136 return String.format(FMT_TO_STRING, Integer.valueOf(System.identityHashCode(this)), getObject()); 137 } 138}