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 org.apache.commons.lang3.ObjectUtils; 020 021/** 022 * <p> 023 * A very simple implementation of the {@link ConcurrentInitializer} interface 024 * which always returns the same object. 025 * </p> 026 * <p> 027 * An instance of this class is passed a reference to an object when it is 028 * constructed. The {@link #get()} method just returns this object. No 029 * synchronization is required. 030 * </p> 031 * <p> 032 * This class is useful for instance for unit testing or in cases where a 033 * specific object has to be passed to an object which expects a 034 * {@link ConcurrentInitializer}. 035 * </p> 036 * 037 * @since 3.0 038 * @version $Id: ConstantInitializer.java 1557584 2014-01-12 18:26:49Z britter $ 039 * @param <T> the type of the object managed by this initializer 040 */ 041public class ConstantInitializer<T> implements ConcurrentInitializer<T> { 042 /** Constant for the format of the string representation. */ 043 private static final String FMT_TO_STRING = "ConstantInitializer@%d [ object = %s ]"; 044 045 /** Stores the managed object. */ 046 private final T object; 047 048 /** 049 * Creates a new instance of {@code ConstantInitializer} and initializes it 050 * with the object to be managed. The {@code get()} method will always 051 * return the object passed here. This class does not place any restrictions 052 * on the object. It may be <b>null</b>, then {@code get()} will return 053 * <b>null</b>, too. 054 * 055 * @param obj the object to be managed by this initializer 056 */ 057 public ConstantInitializer(final T obj) { 058 object = obj; 059 } 060 061 /** 062 * Directly returns the object that was passed to the constructor. This is 063 * the same object as returned by {@code get()}. However, this method does 064 * not declare that it throws an exception. 065 * 066 * @return the object managed by this initializer 067 */ 068 public final T getObject() { 069 return object; 070 } 071 072 /** 073 * Returns the object managed by this initializer. This implementation just 074 * returns the object passed to the constructor. 075 * 076 * @return the object managed by this initializer 077 * @throws ConcurrentException if an error occurs 078 */ 079 @Override 080 public T get() throws ConcurrentException { 081 return getObject(); 082 } 083 084 /** 085 * Returns a hash code for this object. This implementation returns the hash 086 * code of the managed object. 087 * 088 * @return a hash code for this object 089 */ 090 @Override 091 public int hashCode() { 092 return getObject() != null ? getObject().hashCode() : 0; 093 } 094 095 /** 096 * Compares this object with another one. This implementation returns 097 * <b>true</b> if and only if the passed in object is an instance of 098 * {@code ConstantInitializer} which refers to an object equals to the 099 * object managed by this instance. 100 * 101 * @param obj the object to compare to 102 * @return a flag whether the objects are equal 103 */ 104 @SuppressWarnings( "deprecation" ) // ObjectUtils.equals(Object, Object) has been deprecated in 3.2 105 @Override 106 public boolean equals(final Object obj) { 107 if (this == obj) { 108 return true; 109 } 110 if (!(obj instanceof ConstantInitializer<?>)) { 111 return false; 112 } 113 114 final ConstantInitializer<?> c = (ConstantInitializer<?>) obj; 115 return ObjectUtils.equals(getObject(), c.getObject()); 116 } 117 118 /** 119 * Returns a string representation for this object. This string also 120 * contains a string representation of the object managed by this 121 * initializer. 122 * 123 * @return a string for this object 124 */ 125 @Override 126 public String toString() { 127 return String.format(FMT_TO_STRING, Integer.valueOf(System.identityHashCode(this)), 128 String.valueOf(getObject())); 129 } 130}