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 * @param <T> the type of the object managed by this initializer
039 */
040public class ConstantInitializer<T> implements ConcurrentInitializer<T> {
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 {@code 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     * Directly returns the object that was passed to the constructor. This is
062     * the same object as returned by {@code get()}. However, this method does
063     * not declare that it throws an exception.
064     *
065     * @return the object managed by this initializer
066     */
067    public final T getObject() {
068        return object;
069    }
070
071    /**
072     * Returns the object managed by this initializer. This implementation just
073     * returns the object passed to the constructor.
074     *
075     * @return the object managed by this initializer
076     * @throws ConcurrentException if an error occurs
077     */
078    @Override
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    @SuppressWarnings( "deprecation" ) // ObjectUtils.equals(Object, Object) has been deprecated in 3.2
104    @Override
105    public boolean equals(final Object obj) {
106        if (this == obj) {
107            return true;
108        }
109        if (!(obj instanceof ConstantInitializer<?>)) {
110            return false;
111        }
112
113        final ConstantInitializer<?> c = (ConstantInitializer<?>) obj;
114        return ObjectUtils.equals(getObject(), c.getObject());
115    }
116
117    /**
118     * Returns a string representation for this object. This string also
119     * contains a string representation of the object managed by this
120     * initializer.
121     *
122     * @return a string for this object
123     */
124    @Override
125    public String toString() {
126        return String.format(FMT_TO_STRING, Integer.valueOf(System.identityHashCode(this)),
127                String.valueOf(getObject()));
128    }
129}