View Javadoc
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  
19  import java.lang.ref.SoftReference;
20  
21  /**
22   * Extension of {@link DefaultPooledObject} to wrap pooled soft references.
23   *
24   * <p>This class is intended to be thread-safe.</p>
25   *
26   * @param <T> the type of the underlying object that the wrapped SoftReference
27   * refers to.
28   *
29   * @since 2.0
30   */
31  public class PooledSoftReference<T> extends DefaultPooledObject<T> {
32  
33      /** SoftReference wrapped by this object */
34      private volatile SoftReference<T> reference;
35  
36      /**
37       * Creates a new PooledSoftReference wrapping the provided reference.
38       *
39       * @param reference SoftReference to be managed by the pool
40       */
41      public PooledSoftReference(final SoftReference<T> reference) {
42          super(null);  // Null the hard reference in the parent
43          this.reference = reference;
44      }
45  
46      /**
47       * Gets the object that the wrapped SoftReference refers to.
48       * <p>
49       * Note that if the reference has been cleared, this method will return
50       * null.
51       * </p>
52       *
53       * @return Object referred to by the SoftReference
54       */
55      @Override
56      public T getObject() {
57          return reference.get();
58      }
59  
60      /**
61       * Gets the SoftReference wrapped by this object.
62       *
63       * @return underlying SoftReference
64       */
65      public synchronized SoftReference<T> getReference() {
66          return reference;
67      }
68  
69      /**
70       * Sets the wrapped reference.
71       *
72       * <p>This method exists to allow a new, non-registered reference to be
73       * held by the pool to track objects that have been checked out of the pool.
74       * The actual parameter <strong>should</strong> be a reference to the same
75       * object that {@link #getObject()} returns before calling this method.</p>
76       *
77       * @param reference new reference
78       */
79      public synchronized void setReference(final SoftReference<T> reference) {
80          this.reference = reference;
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public String toString() {
88          final StringBuilder result = new StringBuilder();
89          result.append("Referenced Object: ");
90          result.append(getObject().toString());
91          result.append(", State: ");
92          synchronized (this) {
93              result.append(getState().toString());
94          }
95          return result.toString();
96          // TODO add other attributes
97          // TODO encapsulate state and other attribute display in parent
98      }
99  }