001 /* 002 * Copyright 2002-2004 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 of 006 * 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 under 014 * 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 "and" compound constraint (aka conjunction). 024 * 025 * @author Keith Donald 026 */ 027 public class And extends CompoundConstraint { 028 029 /** 030 * Creates a empty And conjunction. 031 */ 032 public And() { 033 super(); 034 } 035 036 /** 037 * "Ands" two constraints. 038 * 039 * @param constraint1 040 * The first constraint. 041 * @param constraint2 042 * The second constraint. 043 */ 044 public And(Constraint constraint1, Constraint constraint2) { 045 super(constraint1, constraint2); 046 } 047 048 /** 049 * "Ands" the specified constraints. 050 * 051 * @param constraints 052 * The constraints 053 */ 054 public And(Constraint[] constraints) { 055 super(constraints); 056 } 057 058 /** 059 * Tests if all of the constraints aggregated by this compound constraint 060 * return <code>true</code> when evaulating this argument. 061 * 062 * @see Constraint#test(java.lang.Object) 063 */ 064 public boolean test(Object argument) { 065 for (Iterator i = iterator(); i.hasNext();) { 066 if (!((Constraint)i.next()).test(argument)) { 067 return false; 068 } 069 } 070 return true; 071 } 072 }