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.pool2.impl;
018
019import java.lang.ref.SoftReference;
020
021/**
022 * Extension of {@link DefaultPooledObject} to wrap pooled soft references.
023 *
024 * <p>This class is intended to be thread-safe.</p>
025 *
026 * @param <T> the type of the underlying object that the wrapped SoftReference
027 * refers to.
028 *
029 * @since 2.0
030 */
031public class PooledSoftReference<T> extends DefaultPooledObject<T> {
032
033    /** SoftReference wrapped by this object */
034    private volatile SoftReference<T> reference;
035
036    /**
037     * Creates a new PooledSoftReference wrapping the provided reference.
038     *
039     * @param reference SoftReference to be managed by the pool
040     */
041    public PooledSoftReference(final SoftReference<T> reference) {
042        super(null);  // Null the hard reference in the parent
043        this.reference = reference;
044    }
045
046    /**
047     * Gets the object that the wrapped SoftReference refers to.
048     * <p>
049     * Note that if the reference has been cleared, this method will return
050     * null.
051     * </p>
052     *
053     * @return Object referred to by the SoftReference
054     */
055    @Override
056    public T getObject() {
057        return reference.get();
058    }
059
060    /**
061     * Gets the SoftReference wrapped by this object.
062     *
063     * @return underlying SoftReference
064     */
065    public synchronized SoftReference<T> getReference() {
066        return reference;
067    }
068
069    /**
070     * Sets the wrapped reference.
071     *
072     * <p>This method exists to allow a new, non-registered reference to be
073     * held by the pool to track objects that have been checked out of the pool.
074     * The actual parameter <strong>should</strong> be a reference to the same
075     * object that {@link #getObject()} returns before calling this method.</p>
076     *
077     * @param reference new reference
078     */
079    public synchronized void setReference(final SoftReference<T> reference) {
080        this.reference = reference;
081    }
082
083    /**
084     * {@inheritDoc}
085     */
086    @Override
087    public String toString() {
088        final StringBuilder result = new StringBuilder();
089        result.append("Referenced Object: ");
090        result.append(getObject().toString());
091        result.append(", State: ");
092        synchronized (this) {
093            result.append(getState().toString());
094        }
095        return result.toString();
096        // TODO add other attributes
097        // TODO encapsulate state and other attribute display in parent
098    }
099}