/*
 * Copyright 2021, TeamDev. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.tools.mc.java.protoc;

import io.spine.tools.protoc.FilePattern;
import io.spine.tools.protoc.ForEntities;
import io.spine.tools.protoc.ProtoOption;
import io.spine.type.MessageType;

import java.util.List;
import java.util.function.Predicate;

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

/**
 * A predicate which checks if a {@link MessageType} represents an entity state type.
 *
 * <p>An entity state type may be marked by a Protobuf option or be matched by a file pattern.
 */
public final class EntityMatcher implements Predicate<MessageType> {

    private final ForEntities entities;

    public EntityMatcher(ForEntities entities) {
        this.entities = checkNotNull(entities);
    }

    @Override
    public boolean test(MessageType type) {
        List<ProtoOption> options = entities.getOptionList();
        boolean match = options.stream()
                               .map(OptionMatcher::new)
                               .anyMatch(matcher -> matcher.test(type));
        if (match) {
            return true;
        }
        List<FilePattern> patterns = entities.getPatternList();
        return patterns.stream()
                       .map(FilePatternMatcher::new)
                       .anyMatch(matcher -> matcher.test(type));
    }
}

@@ -1310,102 +1310,18 @@
 oc.F
-ilePattern;%0Aimport io.spine.tools.protoc.ForEntities;%0Aimport io.spine.tools.protoc.ProtoOption
+orEntities
 ;%0Aim
@@ -1376,16 +1376,52 @@
 l.List;%0A
+import java.util.function.Function;%0A
 import j
@@ -1796,36 +1796,46 @@
 e final 
-ForEntities entities
+Predicate%3CMessageType%3E matcher
 ;%0A%0A    p
@@ -1889,24 +1889,8 @@
     
- this.entities =
  che
@@ -1918,213 +1918,141 @@
     
-%7D%0A%0A    @Override%0A    public boolean test(MessageType type) %7B%0A        List%3CProtoOption%3E options = entities.getOptionList();%0A        boolean match = options.stream()%0A                               .map(Optio
+    matcher = matchBy(entities.getOptionList(), OptionMatcher::new)%0A                .or(matchBy(entities.getPatternList(), FilePatter
 nMat
@@ -2065,60 +2065,90 @@
 new)
+);
 %0A    
+%7D%0A%0A
     
-                       .anyMatch(matcher -%3E
+@Override%0A    public boolean test(MessageType type) %7B%0A        return
  mat
@@ -2166,130 +2166,142 @@
 ype)
-)
 ;%0A    
+%7D%0A%0A
     
-if (match) %7B%0A            return true;%0A        %7D%0A        List%3CFilePattern%3E patterns = entities.getPatternList();
+private static %3CT%3E Predicate%3CMessageType%3E%0A    matchBy(List%3CT%3E criteria, Function%3CT, Predicate%3CMessageType%3E%3E newMatcher) %7B
 %0A   
@@ -2316,16 +2316,24 @@
 urn 
-patterns
+type -%3E criteria
 .str
@@ -2365,38 +2365,41 @@
     
-.map(FilePatternMatcher::new)%0A
+        .map(newMatcher)%0A        
     

Implement `EntityMatcher` in a more functional style