/*
 * Copyright 2017, TeamDev Ltd. 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.aggregate;

import io.spine.annotation.SPI;
import io.spine.server.storage.ReadRequest;

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

/**
 * A read request for {@link AggregateStorage}.
 *
 * @param <I> the type of the target ID
 * @author dmytro.grankin
 */
@SPI
public final class AggregateReadRequest<I> implements ReadRequest<I> {

    private final I id;
    private final int snapshotTrigger;

    public AggregateReadRequest(I id, int snapshotTrigger) {
        this.id = checkNotNull(id);
        this.snapshotTrigger = snapshotTrigger;
    }

    @Override
    public I getId() {
        return id;
    }

    /**
     * Obtains the {@linkplain AggregateRepository#snapshotTrigger snapshot trigger}.
     *
     * <p>Use this value for the {@linkplain AggregateStorage#historyBackward(AggregateReadRequest)
     * history} reading optimization.
     *
     * <p>To load an aggregate, required only the last {@link Snapshot} and events occurred after
     * creation of this snapshot. So instead of reading all {@linkplain AggregateEventRecord
     * aggregate event records}, it is reasonable to read them by batches, size of which is equal
     * to the snapshot trigger value.
     *
     * @return the snapshot trigger value
     */
    @SuppressWarnings("unused")
    public int getSnapshotTrigger() {
        return snapshotTrigger;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) { return true; }
        if (o == null || getClass() != o.getClass()) { return false; }

        AggregateReadRequest<?> that = (AggregateReadRequest<?>) o;

        return snapshotTrigger == that.snapshotTrigger && id.equals(that.id);
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }
}

@@ -1073,19 +1073,24 @@
 otation.
-SPI
+Internal
 ;%0Aimport
@@ -1249,16 +1249,308 @@
 ge%7D.%0A *%0A
+ * %3Cp%3ETo organize efficient reading of an aggregate history, %7B@link AggregateStorage%7D should%0A * read %7B@linkplain AggregateEventRecord aggregate event records%7D by batches.%0A *%0A * %3Cp%3EThe request specifies a size of the batches as a%0A * %7B@linkplain #getSnapshotTrigger snapshot trigger%7D value.%0A *%0A
  * @para
@@ -1575,14 +1575,14 @@
 the 
-target
+record
  ID%0A
@@ -1596,16 +1596,16 @@
 hor 
-d
+D
 mytro
-.g
+ G
 rank
@@ -1616,11 +1616,16 @@
 */%0A@
-SPI
+Internal
 %0Apub
@@ -2548,24 +2548,299 @@
 lue.%0A     *%0A
+     * %3Cp%3ENOTE: A snapshot trigger can be changed for %7B@link AggregateRepository%7D%0A     * after the instantiation, so the amount of events between snapshots can be different%0A     * in an aggregate history and may look like this: %7B@code e1, s1, e2, e3, e4, s2, e5, s3%7D.%0A     *%0A
      * @retu

Mark `AggregateReadRequest` internal instead of `SPI` and improve Javadocs