/*
 * 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.ImmutableMap;
import com.google.protobuf.FieldMask;
import com.google.protobuf.Message;
import io.spine.core.Event;
import io.spine.core.EventClass;
import io.spine.type.TypeUrl;

import java.util.Map;
import java.util.Optional;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableMap.copyOf;
import static com.google.common.collect.Maps.newHashMap;
import static io.spine.core.Events.getMessage;
import static io.spine.protobuf.AnyPacker.pack;
import static io.spine.server.entity.FieldMasks.applyMask;

/**
 * @author Dmytro Dashenkov
 */
public final class EventFieldFilter implements EventFilter {

    private final ImmutableMap<EventClass, FieldMask> fieldMasks;

    private EventFieldFilter(Builder builder) {
        this.fieldMasks = copyOf(builder.masks);
    }

    @Override
    public Optional<Event> filter(Event event) {
        EventClass eventClass = EventClass.of(event);
        FieldMask mask = fieldMasks.getOrDefault(eventClass, FieldMask.getDefaultInstance());
        Event result = mask(event, eventClass, mask);
        return Optional.of(result);
    }

    private static Event mask(Event event, EventClass eventClass, FieldMask mask) {
        Message eventMessage = getMessage(event);
        TypeUrl typeUrl = eventClass.getTypeName().toUrl();
        Message masked = applyMask(mask, eventMessage, typeUrl);
        Event result = event.toBuilder()
                            .setMessage(pack(masked))
                            .build();
        return result;
    }

    /**
     * Creates a new instance of {@code Builder} for {@code EventFieldFilter} instances.
     *
     * @return new instance of {@code Builder}
     */
    public static Builder newBuilder() {
        return new Builder();
    }

    /**
     * A builder for the {@code EventFieldFilter} instances.
     */
    public static final class Builder {

        private final Map<EventClass, FieldMask> masks = newHashMap();

        /**
         * Prevents direct instantiation.
         */
        private Builder() {
        }

        public Builder putMask(Class<? extends Message> eventClass, FieldMask mask) {
            checkNotNull(eventClass);
            checkNotNull(mask);
            EventClass eventType = EventClass.from(eventClass);
            masks.put(eventType, mask);
            return this;
        }

        public Builder putMask(EventClass eventClass, FieldMask mask) {
            checkNotNull(eventClass);
            checkNotNull(mask);
            masks.put(eventClass, mask);
            return this;
        }

        /**
         * Creates a new instance of {@code EventFieldFilter}.
         *
         * @return new instance of {@code EventFieldFilter}
         */
        public EventFieldFilter build() {
            return new EventFieldFilter(this);
        }
    }
}

@@ -1638,16 +1638,68 @@
 plyMask;
+%0Aimport static io.spine.validate.Validate.isDefault;
 %0A%0A/**%0A *
@@ -2121,17 +2121,8 @@
 .get
-OrDefault
 (eve
@@ -2132,43 +2132,118 @@
 lass
-, FieldMask.getDefaultInstance());%0A
+);%0A        if (mask == null %7C%7C isDefault(mask)) %7B%0A            return Optional.of(event);%0A        %7D else %7B%0A    
     
@@ -2292,32 +2292,36 @@
  mask);%0A        
+    
 return Optional.
@@ -2328,24 +2328,34 @@
 of(result);%0A
+        %7D%0A
     %7D%0A%0A    p
@@ -3603,227 +3603,8 @@
  %7D%0A%0A
-        public Builder putMask(EventClass eventClass, FieldMask mask) %7B%0A            checkNotNull(eventClass);%0A            checkNotNull(mask);%0A            masks.put(eventClass, mask);%0A            return this;%0A        %7D%0A%0A
     

Optimize EventFieldFilter.