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 */
017 package org.apache.commons.lang3.concurrent;
018
019 import 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 1088899 2011-04-05 05:31:27Z bayard $
039 * @param <T> the type of the object managed by this initializer
040 */
041 public 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(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 public T get() throws ConcurrentException {
080 return getObject();
081 }
082
083 /**
084 * Returns a hash code for this object. This implementation returns the hash
085 * code of the managed object.
086 *
087 * @return a hash code for this object
088 */
089 @Override
090 public int hashCode() {
091 return (getObject() != null) ? getObject().hashCode() : 0;
092 }
093
094 /**
095 * Compares this object with another one. This implementation returns
096 * <b>true</b> if and only if the passed in object is an instance of
097 * {@code ConstantInitializer} which refers to an object equals to the
098 * object managed by this instance.
099 *
100 * @param obj the object to compare to
101 * @return a flag whether the objects are equal
102 */
103 @Override
104 public boolean equals(Object obj) {
105 if (this == obj) {
106 return true;
107 }
108 if (!(obj instanceof ConstantInitializer<?>)) {
109 return false;
110 }
111
112 ConstantInitializer<?> c = (ConstantInitializer<?>) obj;
113 return ObjectUtils.equals(getObject(), c.getObject());
114 }
115
116 /**
117 * Returns a string representation for this object. This string also
118 * contains a string representation of the object managed by this
119 * initializer.
120 *
121 * @return a string for this object
122 */
123 @Override
124 public String toString() {
125 return String.format(FMT_TO_STRING, Integer.valueOf(System.identityHashCode(this)),
126 String.valueOf(getObject()));
127 }
128 }