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

com.github.phantomthief.pool.Pool Maven / Gradle / Ivy

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

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

import com.github.phantomthief.util.ThrowableConsumer;
import com.github.phantomthief.util.ThrowableFunction;

/**
 * @author w.vela
 * Created on 06/09/2016.
 */
public interface Pool extends AutoCloseable {

    default  V supply(ThrowableFunction function) throws X {
        Pooled pooled = borrow();
        try {
            return function.apply(pooled.get());
        } finally {
            returnObject(pooled);
        }
    }

    default  void run(ThrowableConsumer consumer) throws X {
        supply(obj -> {
            consumer.accept(obj);
            return null;
        });
    }

    /**
     * better use {@link #supply} or {@link #run}
     *
     * @throws IllegalStateException if pool was already closed.
     */
    @Nonnull
    Pooled borrow();

    @Nullable
     V getStats(@Nonnull StatsKey key);

    /**
     * must call exactly once after {@link #borrow} in pair
     */
    void returnObject(@Nonnull Pooled pooled);

    @Override
    void close();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy