XorPolicy.java 1.39 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
11

Matt Tucker's avatar
Matt Tucker committed
12 13 14 15 16 17 18
package org.jivesoftware.net.policies;

import org.jivesoftware.net.AcceptPolicy;
import org.jivesoftware.net.Connection;
import org.jivesoftware.net.AcceptPolicy;

/**
19
 * Performs a bitwise logical XOR (exclusive OR) evaluation on
Matt Tucker's avatar
Matt Tucker committed
20
 * child policies (e.g. if both policies evaluate to true or false
21
 * the XOR result is false).<p>
Matt Tucker's avatar
Matt Tucker committed
22
 *
23
 * This policy is useful for combining simpler policies to create
Matt Tucker's avatar
Matt Tucker committed
24
 * complex policy decisions. The comparison is done using the bitwise
25
 * XOR operation so both policies will always be evaluated.
Matt Tucker's avatar
Matt Tucker committed
26 27 28 29 30 31 32 33 34
 *
 * @author Iain Shigeoka
 */
public class XorPolicy implements AcceptPolicy {

    private AcceptPolicy policy1;
    private AcceptPolicy policy2;

    /**
35
     * Create an AND policy with the given two child policies.
Matt Tucker's avatar
Matt Tucker committed
36 37 38 39 40 41 42 43 44 45 46 47
     *
     * @param firstPolicy The first policy that will be evaluated
     * @param secondPolicy The first policy that will be evaluated
     */
    public XorPolicy(AcceptPolicy firstPolicy, AcceptPolicy secondPolicy){
        policy1 = firstPolicy;
        policy2 = secondPolicy;
    }

    public boolean evaluate(Connection connection) {
        return policy1.evaluate(connection) ^ policy2.evaluate(connection);
    }
48
}