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