001 /*
002 * Copyright 2002-2006 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy
006 * of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations
014 * under the License.
015 */
016 package org.springframework.rules.constraint;
017
018 import java.util.Iterator;
019
020 import org.springframework.rules.constraint.Constraint;
021
022 /**
023 * A "xor" compound constraint (aka exclusive disjunction).
024 *
025 * @author Mathias Broekelmann
026 *
027 */
028 public class XOr extends CompoundConstraint {
029
030 /**
031 * Creates a empty UnaryOr disjunction.
032 */
033 public XOr() {
034 super();
035 }
036
037 /**
038 * "Ors" two constraints.
039 *
040 * @param constraint1
041 * The first constraint.
042 * @param constraint2
043 * The second constraint.
044 */
045 public XOr(Constraint constraint1, Constraint constraint2) {
046 super(constraint1, constraint2);
047 }
048
049 /**
050 * "Ors" the specified constraints.
051 *
052 * @param constraints
053 * The constraints
054 */
055 public XOr(Constraint[] constraints) {
056 super(constraints);
057 }
058
059 /**
060 * Tests if any of the constraints aggregated by this compound constraint test
061 * <code>true</code>.
062 *
063 * @see Constraint#test(java.lang.Object)
064 */
065 public boolean test(Object value) {
066 boolean found = false;
067 for (Iterator i = iterator(); i.hasNext();) {
068 if (((Constraint) i.next()).test(value)) {
069 if (found) {
070 return false;
071 }
072 found = true;
073 }
074 }
075 return found;
076 }
077
078 }