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

com.github.phantomthief.pool.impl.LazyPool Maven / Gradle / Ivy

There is a newer version: 0.1.19
Show newest version
package com.github.phantomthief.pool.impl;

import static com.github.phantomthief.util.MoreSuppliers.lazy;

import java.util.function.Supplier;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.github.phantomthief.pool.Pool;
import com.github.phantomthief.pool.Pooled;
import com.github.phantomthief.pool.StatsKey;
import com.github.phantomthief.util.MoreSuppliers.CloseableSupplier;

/**
 * @author w.vela
 * Created on 09/09/2016.
 */
class LazyPool implements Pool {

    private final CloseableSupplier> factory;

    LazyPool(Supplier> factory) {
        this.factory = lazy(factory, false);
    }

    @Nonnull
    @Override
    public Pooled borrow() {
        return factory.get().borrow();
    }

    @Nullable
    @Override
    public  V getStats(@Nonnull StatsKey key) {
        return factory.map(pool -> pool.getStats(key)).orElse(null);
    }

    @Override
    public void returnObject(@Nonnull Pooled pooled) {
        factory.get().returnObject(pooled);
    }

    @Override
    public void close() {
        factory.tryClose(Pool::close);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy