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

import com.google.common.base.Joiner;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.function.Predicate;

import static java.lang.reflect.Modifier.isPrivate;
import static java.lang.reflect.Modifier.isProtected;
import static java.lang.reflect.Modifier.isPublic;

/**
 * @author Alex Tymchenko
 */
public class AccessModifier implements Predicate<Method> {

    public static final AccessModifier PUBLIC =
            new AccessModifier(Modifier::isPublic, "public");

    public static final AccessModifier PROTECTED =
            new AccessModifier(Modifier::isProtected, "protected");


    public static final AccessModifier PACKAGE_PRIVATE =
            new AccessModifier(
                    methodModifier -> !(isPublic(methodModifier)
                            || isProtected(methodModifier)
                            || isPrivate(methodModifier)), "package-private");

    public static final AccessModifier PRIVATE =
            new AccessModifier(Modifier::isProtected, "private");


    private final Predicate<Integer> checkingMethod;

    private final String name;

    public AccessModifier(Predicate<Integer> checkingMethod, String name) {
        this.checkingMethod = checkingMethod;
        this.name = name;
    }

    static Object asString(Iterable<AccessModifier> modifiers) {
        return Joiner.on(", ")
                     .join(modifiers);
    }

    @Override
    public boolean test(Method method) {
        return checkingMethod.test(method.getModifiers());
    }

    @Override
    public String toString() {
        return name;
    }
}

@@ -1353,16 +1353,111 @@
 c;%0A%0A/**%0A
+ * The predicate for %7B@linkplain Modifier access modifiers%7D of %7B@linkplain Method methods%7D.%0A *%0A
  * @auth
@@ -1765,25 +1765,24 @@
 otected%22);%0A%0A
-%0A
     public s
@@ -2182,62 +2182,312 @@
 );%0A%0A
-%0A    private final Predicate%3CInteger%3E checkingMethod;%0A
+    /**%0A     * The predicate which works with the %7B@linkplain Method#getModifiers() raw representation %7D%0A     * of method's access modifiers.%0A     */%0A    private final Predicate%3CInteger%3E checkingMethod;%0A%0A    /**%0A     * The name of the access modifier.%0A     *%0A     * %3Cp%3EServes for pretty printing.%0A     */
 %0A   
@@ -2520,21 +2520,22 @@
 ;%0A%0A    p
-ublic
+rivate
  AccessM
@@ -2679,145 +2679,664 @@
     
-static Object asString(Iterable%3CAccessModifier%3E modifiers) %7B%0A        return Joiner.on(%22, %22)%0A                     .join(modifiers);%0A    %7D%0A
+/**%0A     * Composes a string representation of several access modifiers.%0A     *%0A     * @param modifiers%0A     *         the modifiers to compose into a %7B@code String%7D%0A     * @return the string with modifier-as-strings%0A     */%0A    static Object asString(Iterable%3CAccessModifier%3E modifiers) %7B%0A        return Joiner.on(%22, %22)%0A                     .join(modifiers);%0A    %7D%0A%0A    /**%0A     * Checks whether the method is of the modifier determined by %7B@code this%7D instance.%0A     *%0A     * @param method%0A     *         the method to check%0A     * @return %7B@code true%7D if the method is declared with the expected modifier,%0A     *         %7B@code false%7D otherwise.%0A     */
 %0A   

Document `AccessModifier`.