/*
 * 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.collect.ImmutableList;
import io.spine.core.Event;

import java.util.Optional;

/**
 * @author Dmytro Dashenkov
 */
public class CompositeEventFilter implements EventFilter {

    private final ImmutableList<EventFilter> filters;

    private CompositeEventFilter(ImmutableList<EventFilter> filters) {
        this.filters = filters;
    }

    @Override
    public Optional<Event> filter(Event event) {
        Optional<Event> result = Optional.of(event);
        for (EventFilter filter : filters) {
            result = filter.filter(result.get());
            if (!result.isPresent()) {
                break;
            }
        }
        return result;
    }
}

@@ -1144,16 +1144,159 @@
 ional;%0A%0A
+import static com.google.common.base.Preconditions.checkNotNull;%0Aimport static java.util.Optional.empty;%0Aimport static java.util.Optional.of;%0A%0A
 /**%0A * @
@@ -1330,16 +1330,22 @@
 %0Apublic 
+final 
 class Co
@@ -1481,42 +1481,23 @@
 ter(
-ImmutableList%3CEventFilter%3E filt
+Builder build
 er
-s
 ) %7B%0A
@@ -1519,23 +1519,39 @@
 lters = 
-filters
+builder.filters.build()
 ;%0A    %7D%0A
@@ -1647,25 +1647,16 @@
 esult = 
-Optional.
 of(event
@@ -1812,19 +1812,780 @@
     
-break;%0A    
+return empty();%0A            %7D%0A        %7D%0A        return result;%0A    %7D%0A%0A    /**%0A     * Creates a new instance of %7B@code Builder%7D for %7B@code CompositeEventFilter%7D instances.%0A     *%0A     * @return new instance of %7B@code Builder%7D%0A     */%0A    public static Builder newBuilder() %7B%0A        return new Builder();%0A    %7D%0A%0A    /**%0A     * A builder for the %7B@code CompositeEventFilter%7D instances.%0A     */%0A    public static final class Builder %7B%0A%0A        private final ImmutableList.Builder%3CEventFilter%3E filters = ImmutableList.builder();%0A%0A        /**%0A         * Prevents direct instantiation.%0A         */%0A        private Builder() %7B%0A        %7D%0A%0A        public Builder add(EventFilter filter) %7B%0A            checkNotNull(filter);%0A            filters.add(filter);%0A            return this;%0A
     
@@ -2586,32 +2586,33 @@
 ;%0A        %7D%0A
+%0A
         
 %7D%0A        re
@@ -2603,32 +2603,272 @@
     
-%7D%0A        return result;
+/**%0A         * Creates a new instance of %7B@code CompositeEventFilter%7D.%0A         *%0A         * @return new instance of %7B@code CompositeEventFilter%7D%0A         */%0A        public CompositeEventFilter build() %7B%0A            return new CompositeEventFilter(this);%0A        %7D
 %0A   

Add builder for the `CompositeEventFilter`.