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