/**
 * Copyright (c) 2009-2013, rultor.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met: 1) Redistributions of source code must retain the above
 * copyright notice, this list of conditions and the following
 * disclaimer. 2) Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided
 * with the distribution. 3) Neither the name of the rultor.com nor
 * the names of its contributors may be used to endorse or promote
 * products derived from this software without specific prior written
 * permission.
 *
 * 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 HOLDER 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 com.rultor.guard.github;

import com.jcabi.aspects.Immutable;
import com.jcabi.aspects.Loggable;
import java.io.IOException;
import javax.validation.constraints.NotNull;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.eclipse.egit.github.core.PullRequest;

/**
 * Approval for a pull request.
 *
 * @author Yegor Bugayenko (yegor@tpc2.com)
 * @version $Id$
 * @since 1.0
 */
@Immutable
public interface Approval {

    /**
     * This pull request has an approval?
     * @param request Pull request
     * @param client Client
     * @param repo Repository
     * @return TRUE if approved
     * @throws IOException If fails
     */
    boolean has(PullRequest request, Github client, Github.Repo repo)
        throws IOException;

    /**
     * Always yes.
     */
    @Immutable
    @ToString
    @EqualsAndHashCode
    @Loggable(Loggable.INFO)
    final class Always implements Approval {
        @Override
        public boolean has(final PullRequest request, final Github client,
            final Github.Repo repo) throws IOException {
            return true;
        }
    }

    /**
     * Always no.
     */
    @Immutable
    @ToString
    @EqualsAndHashCode
    @Loggable(Loggable.INFO)
    final class Never implements Approval {
        @Override
        public boolean has(final PullRequest request, final Github client,
            final Github.Repo repo) throws IOException {
            return false;
        }
    }

    /**
     * Logical OR.
     */
    @Immutable
    @ToString
    @EqualsAndHashCode
    @Loggable(Loggable.INFO)
    final class Or implements Approval {
        /**
         * First approval to ask.
         */
        private final transient Approval first;
        /**
         * Second approval to ask.
         */
        private final transient Approval second;
        /**
         * Public ctor.
         * @param left Left
         * @param right Right
         */
        public Or(final Approval left, final Approval right) {
            this.first = left;
            this.second = right;
        }
        @Override
        public boolean has(final PullRequest request, final Github client,
            final Github.Repo repo) throws IOException {
            return this.first.has(request, client, repo)
                || this.second.has(request, client, repo);
        }
    }

    /**
     * Logical AND.
     */
    @Immutable
    @ToString
    @EqualsAndHashCode
    @Loggable(Loggable.INFO)
    final class And implements Approval {
        /**
         * First approval to ask.
         */
        private final transient Approval first;
        /**
         * Second approval to ask.
         */
        private final transient Approval second;
        /**
         * Public ctor.
         * @param left Left
         * @param right Right
         */
        public And(final Approval left, final Approval right) {
            this.first = left;
            this.second = right;
        }
        @Override
        public boolean has(final PullRequest request, final Github client,
            final Github.Repo repo) throws IOException {
            return this.first.has(request, client, repo)
                && this.second.has(request, client, repo);
        }
    }

    /**
     * Logical NOT.
     */
    @Immutable
    @ToString
    @EqualsAndHashCode
    @Loggable(Loggable.INFO)
    final class Not implements Approval {
        /**
         * The approval to reverse.
         */
        private final transient Approval approval;
        /**
         * Public ctor.
         * @param app Approval to negate
         */
        public Not(final Approval app) {
            this.approval = app;
        }
        @Override
        public boolean has(final PullRequest request, final Github client,
            final Github.Repo repo) throws IOException {
            return !this.approval.has(request, client, repo);
        }
    }

    /**
     * Assigned to.
     */
    @Immutable
    @ToString
    @EqualsAndHashCode(of = "user")
    @Loggable(Loggable.INFO)
    final class AssignedTo implements Approval {
        /**
         * First approval to ask.
         */
        private final transient String user;
        /**
         * Public ctor.
         * @param assignee Assignee
         */
        public AssignedTo(
            @NotNull(message = "Assignee can't be NULL")
            final String assignee) {
            this.user = assignee;
        }

        @Override
        public boolean has(final PullRequest request, final Github client,
            final Github.Repo repo) throws IOException {
            return this.user.equals(request.getAssignee().getLogin());
        }
    }
}

@@ -1694,53 +1694,8 @@
 on;%0A
-import javax.validation.constraints.NotNull;%0A
 impo
@@ -5451,775 +5451,6 @@
  %7D%0A%0A
-    /**%0A     * Assigned to.%0A     */%0A    @Immutable%0A    @ToString%0A    @EqualsAndHashCode(of = %22user%22)%0A    @Loggable(Loggable.INFO)%0A    final class AssignedTo implements Approval %7B%0A        /**%0A         * First approval to ask.%0A         */%0A        private final transient String user;%0A        /**%0A         * Public ctor.%0A         * @param assignee Assignee%0A         */%0A        public AssignedTo(%0A            @NotNull(message = %22Assignee can't be NULL%22)%0A            final String assignee) %7B%0A            this.user = assignee;%0A        %7D%0A%0A        @Override%0A        public boolean has(final PullRequest request, final Github client,%0A            final Github.Repo repo) throws IOException %7B%0A            return this.user.equals(request.getAssignee().getLogin());%0A        %7D%0A    %7D%0A
 %7D%0A

Revert "#257 AssignedTo class"