001 /* 002 * Copyright 2002-2004 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy 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, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.springframework.rules.constraint; 017 018 import java.util.Arrays; 019 import java.util.HashSet; 020 import java.util.Iterator; 021 import java.util.Set; 022 023 import org.springframework.rules.constraint.Constraint; 024 025 /** 026 * A constraint that tests if an argument is one of a group. Similiar to a database's 'in' operator, and more convenient 027 * than using a bunch of ORs. 028 * 029 * @author Keith Donald 030 */ 031 public class InGroup implements Constraint { 032 private Set group; 033 034 public InGroup(Set group) { 035 this.group = new HashSet(group); 036 } 037 038 public InGroup(Object[] group) { 039 this.group = new HashSet(Arrays.asList(group)); 040 } 041 042 /** 043 * returns an iterator of the group elements 044 * 045 * @return iterator containing the elements of the group. 046 */ 047 public Iterator iterator() { 048 return group.iterator(); 049 } 050 051 /** 052 * Tests the variable argument value is in this group. 053 * 054 * @see Constraint#test(java.lang.Object) 055 */ 056 public boolean test(Object value) { 057 return group.contains(value); 058 } 059 060 public String toString() { 061 return "inGroup [" + group + "]"; 062 } 063 }