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

com.fireflysource.common.pool.PooledObject Maven / Gradle / Ivy

There is a newer version: 5.0.2
Show newest version
package com.fireflysource.common.pool;

import com.fireflysource.common.io.AsyncCloseable;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

/**
 * @author Pengtao Qiu
 */
public class PooledObject implements AsyncCloseable {

    protected final Pool pool;
    protected final T object;
    protected final Consumer> leakCallback;
    final AtomicBoolean released = new AtomicBoolean(false);

    public PooledObject(T object, Pool pool, Consumer> leakCallback) {
        this.object = object;
        this.pool = pool;
        this.leakCallback = leakCallback;
    }

    /**
     * Get the cached object.
     *
     * @return T the cached object.
     */
    public T getObject() {
        return object;
    }

    public boolean isReleased() {
        return released.get();
    }

    @Override
    public CompletableFuture closeAsync() {
        return pool.release(this);
    }

    @Override
    public void close() {
        closeAsync();
    }

    @Override
    public String toString() {
        return "PooledObject{" +
                "object=" + object +
                ", released=" + released +
                '}';
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy