/*
 * Copyright 2018, 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.entity;

import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import io.spine.core.Event;

import java.util.Deque;
import java.util.Iterator;
import java.util.Objects;
import java.util.stream.Stream;

import static com.google.common.collect.Queues.newArrayDeque;

/**
 * A copy of recent history of an {@linkplain TransactionalEntity
 * event-sourced entity}.
 *
 * <p>Any modifications to this object will not affect the real history of the entity.
 *
 * @author Mykhailo Drachuk
 * @author Alexander Yevsyukov
 */
public final class RecentHistory {

    /**
     * Holds the history of all events which happened to the aggregate since the last snapshot.
     *
     * <p>Most recent event come first.
     *
     * @see #iterator()
     */
    private final Deque<Event> history = newArrayDeque();

    /**
     * Creates new instance.
     */
    RecentHistory() {
        super();
    }

    /**
     * Returns {@code true} if there are no events in the recent history, {@code false} otherwise.
     */
    public boolean isEmpty() {
        return history.isEmpty();
    }

    /**
     * Removes all events from the recent history.
     */
    void clear() {
        history.clear();
    }

    /**
     * Creates a new iterator over the recent history items.
     *
     * <p>The iterator returns events in the reverse chronological order. That is, most recent
     * event would be returned first.
     *
     * @return an events iterator
     */
    public Iterator<Event> iterator() {
        ImmutableList<Event> events = ImmutableList.copyOf(history);
        return events.iterator();
    }

    /**
     * Create a new {@link Stream} of the recent history items.
     *
     * <p>The produced stream is sequential and emits items in the reverse chronological order.
     * That is, most recent event would be returned first.
     *
     * @return a stream of the recent events
     */
    public Stream<Event> stream() {
        ImmutableList<Event> events = ImmutableList.copyOf(history);
        return events.stream();
    }

    /**
     * Adds events to the history.
     *
     * @param events events in the chronological order
     */
    void addAll(Iterable<Event> events) {
        for (Event event : events) {
            history.addFirst(event);
        }
    }

    @Override
    public int hashCode() {
        return Objects.hash(history);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        RecentHistory other = (RecentHistory) obj;
        return Objects.equals(this.history, other.history);
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                          .add("size", history.size())
                          .toString();
    }
}

@@ -2694,16 +2694,17 @@
 * Create
+s
  a new %7B

Fix a typo.