/*
 * 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.server.model;

import com.google.common.collect.ImmutableList;
import com.google.common.reflect.Invokable;
import com.google.common.reflect.TypeToken;

import javax.annotation.concurrent.Immutable;
import java.lang.reflect.Method;
import java.util.Objects;

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

/**
 * Specifies types of values a handler methods may return.
 */
@SuppressWarnings("UnstableApiUsage") // `TypeToken` is marked as "beta".
@Immutable
public final class ReturnTypes {

    /** A return type of a {@code void} handler method. */
    private static final ReturnTypes VOID = new ReturnTypes(void.class);

    private final ImmutableList<TypeToken<?>> whitelist;
    private final ImmutableList<TypeToken<?>> blacklist;

    private ReturnTypes(ImmutableList<TypeToken<?>> whitelist,
                        ImmutableList<TypeToken<?>> blacklist) {
        this.whitelist = whitelist;
        this.blacklist = blacklist;
    }

    /**
     * Creates an instance for the passed type.
     */
    private ReturnTypes(Class<?> cls) {
        this(ImmutableList.of(TypeToken.of(cls)), ImmutableList.of());
    }

    /**
     * Creates an instance containing the passed types.
     */
    public ReturnTypes(TypeToken<?>... whitelist) {
        this(ImmutableList.copyOf(whitelist), ImmutableList.of());
    }

    /**
     * Obtains the instance representing the {@code void} return type.
     */
    public static ReturnTypes onlyVoid() {
        return VOID;
    }

    /**
     * Creates a new {@code ReturnTypes} but with the given black list of types.
     *
     * @param types types not to allow as the return types
     * @return new {@code ReturnTypes} with a new black list
     */
    public ReturnTypes butNot(TypeToken<?>... types) {
        checkNotNull(types);
        return new ReturnTypes(whitelist, ImmutableList.copyOf(types));
    }

    /**
     * Verifies if the return type of the passed method matches this instance.
     *
     * @param method
     *         the method to check
     * @param mayReturnIgnored
     *         is {@code true} if the method can return {@link Nothing}
     */
    boolean matches(Method method, boolean mayReturnIgnored) {
        TypeToken<?> actualReturnType = Invokable.from(method).getReturnType();
        if (unrightfullyIgnored(actualReturnType, mayReturnIgnored)) {
            return false;
        }
        boolean conformsWhitelist = whitelist.stream()
                                             .anyMatch(t -> conforms(t, actualReturnType));
        if (!conformsWhitelist) {
            return false;
        }
        boolean conformsBlacklist = blacklist.stream()
                                             .noneMatch(t -> conforms(t, actualReturnType));
        return conformsBlacklist;
    }

    @SuppressWarnings("UnstableApiUsage") // Using Guava's `TypeToken`.
    private static boolean conforms(TypeToken<?> expectedType,
                                    TypeToken<?> actualType) {
        return TypeMatcher.matches(expectedType, actualType);
    }

    private static boolean unrightfullyIgnored(TypeToken<?> actualReturnType,
                                               boolean mayReturnIgnored) {
        return mayReturnIgnored || TypeMatcher.messagesFitting(actualReturnType)
                                              .stream()
                                              .noneMatch(MethodResult::isIgnored);
    }

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

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

@@ -3559,29 +3559,38 @@
     if (
-unrightfullyI
+!mayReturnIgnored && i
 gnored(a
@@ -3608,26 +3608,8 @@
 Type
-, mayReturnIgnored
 )) %7B
@@ -4055,80 +4055,8 @@
  %7D%0A%0A
-    @SuppressWarnings(%22UnstableApiUsage%22) // Using Guava's %60TypeToken%60.%0A
     
@@ -4277,21 +4277,9 @@
 ean 
-unrightfullyI
+i
 gnor
@@ -4314,119 +4314,26 @@
 Type
-,%0A                                               boolean mayReturnIgnored) %7B%0A        return mayReturnIgnored %7C%7C
+) %7B%0A        return
  Typ
@@ -4401,36 +4401,16 @@
         
-        
-            
 .stream(
@@ -4441,33 +4441,12 @@
     
-       
-             .none
+.any
 Matc

Clear up ignored type logic