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

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

The newest version!
package com.github.phantomthief.pool.impl;

import static com.github.phantomthief.pool.KeyAffinityExecutorUtils.RANDOM_THRESHOLD;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import java.util.function.BooleanSupplier;
import java.util.function.IntPredicate;
import java.util.function.IntSupplier;
import java.util.function.Supplier;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.NotThreadSafe;

import com.github.phantomthief.util.ThrowableConsumer;
import com.google.common.annotations.VisibleForTesting;

/**
 * @author w.vela
 * Created on 2018-02-09.
 */
@NotThreadSafe
class KeyAffinityBuilder {

    private Supplier factory;
    private IntSupplier count;
    private ThrowableConsumer depose;
    private IntPredicate usingRandom;
    private BooleanSupplier counterChecker;

    public  LazyKeyAffinity build() {
        ensure();
        return new LazyKeyAffinity<>(this::buildInner);
    }

     KeyAffinityImpl buildInner() {
        return new KeyAffinityImpl<>(factory, count, depose, usingRandom, counterChecker);
    }

    void ensure() {
        if (count == null || count.getAsInt() <= 0) {
            throw new IllegalArgumentException("no count found.");
        }
        if (counterChecker == null) {
            counterChecker = () -> true;
        }
        if (depose == null) {
            depose = it -> { };
        }
        if (usingRandom == null) {
            usingRandom = it -> it > RANDOM_THRESHOLD;
        }
    }

    @SuppressWarnings("unchecked")
    @CheckReturnValue
    @Nonnull
    @VisibleForTesting
    > T counterChecker(@Nonnull BooleanSupplier value) {
        this.counterChecker = checkNotNull(value);
        return (T) this;
    }

    @SuppressWarnings("unchecked")
    @CheckReturnValue
    @Nonnull
    public > T factory(@Nonnull Supplier value) {
        this.factory = checkNotNull(value);
        return (T) this;
    }

    /**
     * whether to use random strategy or less concurrency strategy
     * @param value {@code true} is random strategy and {@code false} is less concurrency strategy
     * default value is {@code true} is {@link #count} larger than {@link com.github.phantomthief.pool.KeyAffinityExecutorUtils#RANDOM_THRESHOLD}
     */
    @SuppressWarnings("unchecked")
    @CheckReturnValue
    @Nonnull
    public > T usingRandom(boolean value) {
        return usingRandom(it -> value);
    }

    /**
     * whether to use random strategy or less concurrency strategy
     * @param value {@code true} is random strategy and {@code false} is less concurrency strategy
     * default value is {@code true} is {@link #count} larger than {@link com.github.phantomthief.pool.KeyAffinityExecutorUtils#RANDOM_THRESHOLD}
     */
    @SuppressWarnings("unchecked")
    @CheckReturnValue
    @Nonnull
    public > T usingRandom(@Nonnull IntPredicate value) {
        this.usingRandom = checkNotNull(value);
        return (T) this;
    }

    @SuppressWarnings("unchecked")
    @CheckReturnValue
    @Nonnull
    public > T count(@Nonnegative int value) {
        checkArgument(value > 0);
        this.count = () -> value;
        return (T) this;
    }

    @SuppressWarnings("unchecked")
    @CheckReturnValue
    @Nonnull
    public > T count(@Nonnull IntSupplier value) {
        this.count = checkNotNull(value);
        return (T) this;
    }

    @SuppressWarnings("unchecked")
    @CheckReturnValue
    @Nonnull
    public > T depose(@Nonnegative ThrowableConsumer value) {
        this.depose = checkNotNull(value);
        return (T) this;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy