All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.commons.pool2.impl.PooledSoftReference Maven / Gradle / Ivy

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

import java.lang.ref.SoftReference;

/**
 * Extension of {@link DefaultPooledObject} to wrap pooled soft references.
 *
 * 

This class is intended to be thread-safe.

* * @param the type of the underlying object that the wrapped SoftReference * refers to. * * @version $Revision: $ * * @since 2.0 */ public class PooledSoftReference extends DefaultPooledObject { /** SoftReference wrapped by this object */ private volatile SoftReference reference; /** * Creates a new PooledSoftReference wrapping the provided reference. * * @param reference SoftReference to be managed by the pool */ public PooledSoftReference(SoftReference reference) { super(null); // Null the hard reference in the parent this.reference = reference; } /** * Returns the object that the wrapped SoftReference refers to. *

* Note that if the reference has been cleared, this method will return * null. * * @return Object referred to by the SoftReference */ @Override public T getObject() { return reference.get(); } /** * {@inheritDoc} */ @Override public String toString() { StringBuilder result = new StringBuilder(); result.append("Referenced Object: "); result.append(getObject().toString()); result.append(", State: "); synchronized (this) { result.append(getState().toString()); } return result.toString(); // TODO add other attributes // TODO encapsulate state and other attribute display in parent } /** * Returns the SoftReference wrapped by this object. * * @return underlying SoftReference */ public synchronized SoftReference getReference() { return reference; } /** * Sets the wrapped reference. * *

This method exists to allow a new, non-registered reference to be * held by the pool to track objects that have been checked out of the pool. * The actual parameter should be a reference to the same * object that {@link #getObject()} returns before calling this method.

* * @param reference new reference */ public synchronized void setReference(SoftReference reference) { this.reference = reference; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy