/*
 * Copyright 2020, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.server.delivery;

import com.google.protobuf.Duration;
import com.google.protobuf.util.Durations;
import io.spine.server.delivery.CatchUpProcess.DispatchCatchingUp;
import io.spine.server.projection.ProjectionRepository;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;

import java.util.Optional;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.spine.util.Preconditions2.checkPositive;

/**
 * A builder for {@link CatchUpProcess}.
 */
public final class CatchUpProcessBuilder<I> {

    private static final Duration DEFAULT_TURBULENCE_PERIOD = Durations.fromMillis(500);

    private final ProjectionRepository<I, ?, ?> repository;
    private @MonotonicNonNull CatchUpStorage storage;
    private @MonotonicNonNull DispatchCatchingUp<I> dispatchOp;
    private @MonotonicNonNull Duration turbulencePeriod;
    private int pageSize;

    CatchUpProcessBuilder(ProjectionRepository<I, ?, ?> repository) {
        this.repository = repository;
    }

    public ProjectionRepository<I, ?, ?> repository() {
        return repository;
    }

    Optional<CatchUpStorage> getStorage() {
        return Optional.ofNullable(storage);
    }

    CatchUpStorage storage() {
        return checkNotNull(storage);
    }

    CatchUpProcessBuilder<I> withStorage(CatchUpStorage storage) {
        this.storage = checkNotNull(storage);
        return this;
    }

    CatchUpProcessBuilder<I> withPageSize(int pageSize) {
        checkPositive(pageSize);
        this.pageSize = pageSize;
        return this;
    }

    public int pageSize() {
        return pageSize;
    }

    CatchUpProcessBuilder<I> withTurbulencePeriod(Duration period) {
        this.turbulencePeriod = checkNotNull(period);
        return this;
    }

    public Duration turbulencePeriod() {
        return checkNotNull(turbulencePeriod);
    }

    public CatchUpProcessBuilder<I> withDispatchOp(DispatchCatchingUp<I> operation) {
        this.dispatchOp = checkNotNull(operation);
        return this;
    }

    public Optional<DispatchCatchingUp<I>> getDispatchOp() {
        return Optional.ofNullable(dispatchOp);
    }

    public DispatchCatchingUp<I> dispatchOp() {
        return checkNotNull(dispatchOp);
    }

    public CatchUpProcess<I> build() {
        checkNotNull(storage);
        checkNotNull(dispatchOp);
        checkPositive(pageSize);

        if(turbulencePeriod == null) {
            turbulencePeriod = DEFAULT_TURBULENCE_PERIOD;
        }
        return new CatchUpProcess<>(this);
    }
}

@@ -1317,36 +1317,8 @@
 l;%0A%0A
-import java.util.Optional;%0A%0A
 impo
@@ -1378,16 +1378,16 @@
 otNull;%0A
+
 import s
@@ -2094,104 +2094,8 @@
  %7D%0A%0A
-    Optional%3CCatchUpStorage%3E getStorage() %7B%0A        return Optional.ofNullable(storage);%0A    %7D%0A%0A
     
@@ -2464,23 +2464,16 @@
  %7D%0A%0A    
-public 
 int page
@@ -2521,166 +2521,8 @@
     
-CatchUpProcessBuilder%3CI%3E withTurbulencePeriod(Duration period) %7B%0A        this.turbulencePeriod = checkNotNull(period);%0A        return this;%0A    %7D%0A%0A    public 
 Dura
@@ -2770,135 +2770,12 @@
  %7D%0A%0A
+
     
-public Optional%3CDispatchCatchingUp%3CI%3E%3E getDispatchOp() %7B%0A        return Optional.ofNullable(dispatchOp);%0A    %7D%0A%0A    public 
 Disp

Remove the unused API methods.