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     *
052     * @return Object referred to by the SoftReference
053     */
054    @Override
055    public T getObject() {
056        return reference.get();
057    }
058
059    /**
060     * Gets the SoftReference wrapped by this object.
061     *
062     * @return underlying SoftReference
063     */
064    public synchronized SoftReference<T> getReference() {
065        return reference;
066    }
067
068    /**
069     * Sets the wrapped reference.
070     *
071     * <p>This method exists to allow a new, non-registered reference to be
072     * held by the pool to track objects that have been checked out of the pool.
073     * The actual parameter <strong>should</strong> be a reference to the same
074     * object that {@link #getObject()} returns before calling this method.</p>
075     *
076     * @param reference new reference
077     */
078    public synchronized void setReference(final SoftReference<T> reference) {
079        this.reference = reference;
080    }
081
082    /**
083     * {@inheritDoc}
084     */
085    @Override
086    public String toString() {
087        final StringBuilder result = new StringBuilder();
088        result.append("Referenced Object: ");
089        result.append(getObject().toString());
090        result.append(", State: ");
091        synchronized (this) {
092            result.append(getState().toString());
093        }
094        return result.toString();
095        // TODO add other attributes
096        // TODO encapsulate state and other attribute display in parent
097    }
098}