View Javadoc

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  import org.springframework.util.Assert;
23  
24  /**
25   * An {@link AbstractCollectionCondition} implementation that checks whether the given collection or array
26   * is longer then a specific minimum size.
27   *
28   * @author Uri Boness
29   */
30  public class MinSizeCollectionCondition extends AbstractCollectionCondition {
31  
32      private int minSize;
33  
34      /**
35       * Constructs a new MinSizeCollectionCondition with a given minimum size.
36       *
37       * @param minSize The minimum size.
38       */
39      public MinSizeCollectionCondition(int minSize) {
40          Assert.isTrue(minSize >= 0, "Min size cannot be negative");
41          this.minSize = minSize;
42      }
43  
44      /**
45       * Checks whether the length of the given array is greater than or equals the minimum size associated
46       * with this condition.
47       *
48       * @param array The array to be checked.
49       * @return <code>true</code> if the length of the given array is greater than or equals the minimum size
50       *         associated with this condition, <code>false</code> otherwise.
51       */
52      protected boolean checkArray(Object array) {
53          return Array.getLength(array) >= minSize;
54      }
55  
56      /**
57       * Checks whether the size of the given collection is greater than or equals the minimum size associated
58       * with this condition.
59       *
60       * @param collection The collection to be checked.
61       * @return <code>true</code> if the size of the given collection is greater than or equals the minimum size
62       *         associated with this condition, <code>false</code> otherwise.
63       */
64      protected boolean checkCollection(Collection collection) {
65          return collection.size() >= minSize;
66      }
67  
68      //============================================= Setter/Getter ===================================================
69  
70      /**
71       * Returns the minimum size associated with this condition.
72       *
73       * @return The minimum size associated with this condition.
74       */
75      public int getMinSize() {
76          return minSize;
77      }
78  
79  }