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

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

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

import static com.google.common.base.Preconditions.checkNotNull;

import javax.annotation.Nonnull;

import com.github.phantomthief.pool.impl.KeyAffinityBuilder;
import com.github.phantomthief.util.ThrowableConsumer;
import com.github.phantomthief.util.ThrowableFunction;

/**
 * @author w.vela
 * Created on 2018-02-03.
 */
public interface KeyAffinity extends AutoCloseable, Iterable {

    @Nonnull
    static  KeyAffinityBuilder newBuilder() {
        return new KeyAffinityBuilder<>();
    }

    @Nonnull
    V select(K key);

    void finishCall(K key);

    default  T supply(K key, @Nonnull ThrowableFunction func)
            throws X {
        checkNotNull(func);
        V one = select(key);
        try {
            return func.apply(one);
        } finally {
            finishCall(key);
        }
    }

    default  void run(K key, @Nonnull ThrowableConsumer func) throws X {
        supply(key, it -> {
            func.accept(it);
            return null;
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy