Performs a logical exclusive OR on the array of Boolean values and returns true if and only if the Boolean array is not null and 1 and only 1 Boolean wrapper object evaluates to true, as determined by the BooleanUtils.valueof method. - Java java.lang

Java examples for java.lang:boolean

Description

Performs a logical exclusive OR on the array of Boolean values and returns true if and only if the Boolean array is not null and 1 and only 1 Boolean wrapper object evaluates to true, as determined by the BooleanUtils.valueof method.

Demo Code

/*//from w w  w  .  j a v a  2 s .  c  o m
 * Copyright (c) 2011-Present. Codeprimate, LLC and authors.  All Rights Reserved.
 * 
 * This software is licensed under the Codeprimate End User License Agreement (EULA).
 * This software is proprietary and confidential in addition to an intellectual asset
 * of the aforementioned authors.
 * 
 * By using the software, the end-user implicitly consents to and agrees to be in compliance
 * with all terms and conditions of the EULA.  Failure to comply with the EULA will result in
 * the maximum penalties permissible by law.
 * 
 * In short, this software may not be reverse engineered, reproduced, copied, modified
 * or distributed without prior authorization of the aforementioned authors, permissible
 * and expressed only in writing.  The authors grant the end-user non-exclusive, non-negotiable
 * and non-transferable use of the software "as is" without expressed or implied WARRANTIES,
 * EXTENSIONS or CONDITIONS of any kind.
 * 
 * For further information on the software license, the end user is encouraged to read
 * the EULA @ ...
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        boolean values = true;
        System.out.println(xor(values));
    }

    /**
     * Performs a logical exclusive OR on the array of Boolean values and returns true if and only if the Boolean array
     * is not null and 1 and only 1 Boolean wrapper object evaluates to true, as determined by the BooleanUtils.valueof
     * method.
     * 
     * @param values the array of Boolean values evaluated with the logical exclusive OR operator.
     * @return a boolean value of true if and only if the Boolean array is not null and 1 and only 1 of the Boolean
     * wrapper objects evaluates to true.
     * @see #valueOf(Boolean)
     */
    public static boolean xor(final Boolean... values) {
        boolean result = false; // guilty until proven innocent.

        if (values != null) {
            for (Boolean value : values) {
                boolean primitiveValue = valueOf(value);

                if (result && primitiveValue) {
                    return false; // short-circuit if we find more than 1 true value
                }

                result |= primitiveValue;
            }
        }

        return result;
    }

    /**
     * Determines the primitive boolean value of the specified Boolean wrapper object.  The Boolean value is true
     * if and only if it is equal to Boolean.TRUE.  This method handles null values.
     * 
     * @param value the Boolean wrapper value to convert to a primitive boolean value.
     * @return a primitive boolean value equivalent to the value of the Boolean wrapper object.
     * @see java.lang.Boolean
     */
    public static boolean valueOf(final Boolean value) {
        return Boolean.TRUE.equals(value);
    }
}

Related Tutorials