1 /* 2 * Copyright 2004-2009 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springmodules.validation.util.condition.collection; 18 19 import java.lang.reflect.Array; 20 import java.util.Collection; 21 22 /** 23 * An {@link AbstractCollectionCondition} that checks that the size/length of the the checked collection/array is 24 * with in a specific bounds. 25 * 26 * @author Uri Boness 27 */ 28 public class SizeRangeCollectionCondition extends AbstractCollectionCondition { 29 30 private int minSize; 31 32 private int maxSize; 33 34 /** 35 * Constructs a new SizeRangeCollectionCondition with a given minimum and maximum sizes. 36 * 37 * @param minSize The given minimum size. 38 * @param maxSize The given maximum size. 39 */ 40 public SizeRangeCollectionCondition(int minSize, int maxSize) { 41 this.minSize = minSize; 42 this.maxSize = maxSize; 43 } 44 45 /** 46 * Checks whether the given array is in the boundries of the range defined by this condition. 47 * 48 * @see AbstractCollectionCondition#checkArray(Object) 49 */ 50 protected boolean checkArray(Object array) { 51 int length = Array.getLength(array); 52 return length >= minSize && length <= maxSize; 53 } 54 55 /** 56 * Checks whether the given collection is in the boundries of the range defined by this condition. 57 * 58 * @see AbstractCollectionCondition#checkCollection(java.util.Collection) 59 */ 60 protected boolean checkCollection(Collection collection) { 61 int size = collection.size(); 62 return size >= minSize && size <= maxSize; 63 } 64 65 //=============================================== Setter/Getter ==================================================== 66 67 /** 68 * Returns the minimum size of this range condition 69 * 70 * @return The minimum size of this range condition 71 */ 72 public int getMinSize() { 73 return minSize; 74 } 75 76 /** 77 * Returns the maximum size of this range condition 78 * 79 * @return The maximum size of this range condition 80 */ 81 public int getMaxSize() { 82 return maxSize; 83 } 84 85 }