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

com.github.phantomthief.zookeeper.GenericZkBasedNodeBuilder Maven / Gradle / Ivy

The newest version!
package com.github.phantomthief.zookeeper;

import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

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

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.zookeeper.data.Stat;

import com.github.phantomthief.util.ThrowableBiConsumer;
import com.github.phantomthief.util.ThrowableBiFunction;
import com.github.phantomthief.util.ThrowableConsumer;
import com.github.phantomthief.util.ThrowableFunction;
import com.github.phantomthief.zookeeper.ZkBasedNodeResource.Builder;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;

/**
 * @author w.vela
 * Created on 16/5/13.
 */
public class GenericZkBasedNodeBuilder {

    private final Builder builder;

    GenericZkBasedNodeBuilder(Builder builder) {
        this.builder = builder;
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder
            addFactoryFailedListener(@Nonnull ThrowableConsumer listener) {
        builder.addFactoryFailedListener(listener);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder addFactoryFailedListener(
            @Nonnull ThrowableBiConsumer listener) {
        builder.addFactoryFailedListener(listener);
        return this;
    }

    /**
     * use {@link #withFactoryEx}
     */
    @Deprecated
    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder withFactory(BiFunction factory) {
        return withFactoryEx(factory::apply);
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder
            withFactoryEx(ThrowableBiFunction factory) {
        builder.withFactoryEx(factory);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder asyncRefresh(@Nonnull ListeningExecutorService executor) {
        builder.asyncRefresh(executor);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder
            onResourceChange(BiConsumer callback) {
        builder.onResourceChange(callback);
        return this;
    }

    /**
     * use {@link #withFactoryEx}
     */
    @Deprecated
    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder withFactory(Function factory) {
        return withFactoryEx(factory::apply);
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder
            withFactoryEx(ThrowableFunction factory) {
        return withFactoryEx((b, s) -> factory.apply(b));
    }

    /**
     * use {@link #withStringFactoryEx}
     */
    @Deprecated
    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder
            withStringFactory(BiFunction factory) {
        return withStringFactoryEx(factory::apply);
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder
            withStringFactoryEx(ThrowableBiFunction factory) {
        return withFactoryEx((b, s) -> factory.apply(b == null ? null : new String(b), s));
    }

    /**
     * use {@link #withStringFactoryEx}
     */
    @Deprecated
    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder withStringFactory(Function factory) {
        return withStringFactoryEx(factory::apply);
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder
            withStringFactoryEx(ThrowableFunction factory) {
        return withStringFactoryEx((b, s) -> factory.apply(b));
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder withCacheFactory(Supplier cacheFactory) {
        builder.withCacheFactory(cacheFactory);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder withCacheFactory(String path, CuratorFramework curator) {
        builder.withCacheFactory(path, curator);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder withCacheFactory(String path,
            Supplier curatorFactory) {
        builder.withCacheFactory(path, curatorFactory);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder
            withCleanupConsumer(ThrowableConsumer cleanup) {
        builder.withCleanupConsumer(cleanup);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder withCleanupPredicate(Predicate cleanup) {
        builder.withCleanupPredicate(cleanup);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder withWaitStopPeriod(long waitStopPeriod) {
        builder.withWaitStopPeriod(waitStopPeriod);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder withEmptyObject(T emptyObject) {
        builder.withEmptyObject(emptyObject);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder withRefreshFactory(
            @Nonnull ThrowableBiFunction factory) {
        builder.withRefreshFactory(factory);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder withRefreshFactory(
            @Nullable ListeningExecutorService executor,
            @Nonnull ThrowableBiFunction factory) {
        builder.withRefreshFactory(executor, factory);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder withAsyncRefreshFactory(
            @Nonnull ThrowableBiFunction, Exception> factory) {
        builder.withAsyncRefreshFactory(factory);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder withRefreshFactory(
            @Nonnull ThrowableFunction factory) {
        builder.withRefreshFactory(factory);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder withRefreshFactory(
            @Nullable ListeningExecutorService executor,
            @Nonnull ThrowableFunction factory) {
        builder.withRefreshFactory(executor, factory);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder withAsyncRefreshFactory(
            ThrowableFunction, Exception> factory) {
        builder.withAsyncRefreshFactory(factory);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder withRefreshStringFactory(
            @Nonnull ThrowableBiFunction factory) {
        builder.withRefreshStringFactory(factory);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder withRefreshStringFactory(
            @Nullable ListeningExecutorService executor,
            @Nonnull ThrowableBiFunction factory) {
        builder.withRefreshStringFactory(executor, factory);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder withAsyncRefreshStringFactory(
            @Nonnull ThrowableBiFunction, Exception> factory) {
        builder.withAsyncRefreshStringFactory(factory);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder withRefreshStringFactory(
            @Nonnull ThrowableFunction factory) {
        builder.withRefreshStringFactory(factory);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder withRefreshStringFactory(
            @Nullable ListeningExecutorService executor,
            @Nonnull ThrowableFunction factory) {
        builder.withRefreshStringFactory(executor, factory);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public GenericZkBasedNodeBuilder withAsyncRefreshStringFactory(
            ThrowableFunction, Exception> factory) {
        builder.withAsyncRefreshStringFactory(factory);
        return this;
    }

    @Nonnull
    public ZkBasedNodeResource build() {
        return builder.build();
    }
}