PooledSoftReference.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.pool2.impl;

  18. import java.lang.ref.SoftReference;

  19. /**
  20.  * Extension of {@link DefaultPooledObject} to wrap pooled soft references.
  21.  *
  22.  * <p>This class is intended to be thread-safe.</p>
  23.  *
  24.  * @param <T> the type of the underlying object that the wrapped SoftReference
  25.  * refers to.
  26.  *
  27.  * @since 2.0
  28.  */
  29. public class PooledSoftReference<T> extends DefaultPooledObject<T> {

  30.     /** SoftReference wrapped by this object */
  31.     private volatile SoftReference<T> reference;

  32.     /**
  33.      * Creates a new PooledSoftReference wrapping the provided reference.
  34.      *
  35.      * @param reference SoftReference to be managed by the pool
  36.      */
  37.     public PooledSoftReference(final SoftReference<T> reference) {
  38.         super(null);  // Null the hard reference in the parent
  39.         this.reference = reference;
  40.     }

  41.     /**
  42.      * Gets the object that the wrapped SoftReference refers to.
  43.      * <p>
  44.      * Note that if the reference has been cleared, this method will return
  45.      * null.
  46.      * </p>
  47.      *
  48.      * @return Object referred to by the SoftReference
  49.      */
  50.     @Override
  51.     public T getObject() {
  52.         return reference.get();
  53.     }

  54.     /**
  55.      * Gets the SoftReference wrapped by this object.
  56.      *
  57.      * @return underlying SoftReference
  58.      */
  59.     public synchronized SoftReference<T> getReference() {
  60.         return reference;
  61.     }

  62.     /**
  63.      * Sets the wrapped reference.
  64.      *
  65.      * <p>This method exists to allow a new, non-registered reference to be
  66.      * held by the pool to track objects that have been checked out of the pool.
  67.      * The actual parameter <strong>should</strong> be a reference to the same
  68.      * object that {@link #getObject()} returns before calling this method.</p>
  69.      *
  70.      * @param reference new reference
  71.      */
  72.     public synchronized void setReference(final SoftReference<T> reference) {
  73.         this.reference = reference;
  74.     }

  75.     @Override
  76.     public String toString() {
  77.         final StringBuilder result = new StringBuilder();
  78.         result.append("Referenced Object: ");
  79.         result.append(getObject().toString());
  80.         result.append(", State: ");
  81.         synchronized (this) {
  82.             result.append(getState().toString());
  83.         }
  84.         return result.toString();
  85.         // TODO add other attributes
  86.         // TODO encapsulate state and other attribute display in parent
  87.     }
  88. }