    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 *      https://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    @Override
    084    public String toString() {
    085        final StringBuilder result = new StringBuilder();
    086        result.append("Referenced Object: ");
    087        result.append(getObject().toString());
    088        result.append(", State: ");
    089        synchronized (this) {
    090            result.append(getState().toString());
    091        }
    092        return result.toString();
    093        // TODO add other attributes
    094        // TODO encapsulate state and other attribute display in parent
    095    }
    096}