KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > functions > CollectionFunctions


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
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 package com.blandware.atleap.webapp.taglib.core.functions;
17
18 import com.blandware.atleap.common.util.ConvertUtil;
19
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.List JavaDoc;
22
23 /**
24  * <p>Contains set of functions to ease the work with collection on the JSP</p>
25  * <p><a HREF="CollectionFunctions.java.htm"><i>View Source</i></a></p>
26  *
27  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
28  * @version $Revision: 1.4 $ $Date: 2006/03/25 15:12:50 $
29  */

30 public class CollectionFunctions {
31
32     /**
33      * Calculates size of argument. Supported types are ones, supported by <code>ConvertUtil.convertCollectionToList(java.lang.Object)</code>.
34      * In addition, you may provide <code>getSize()</code> or <code>getLength()</code> method in object's class which return type
35      * must be either <code>int</code> or </code>java.lang.Integer</code>.
36      * If none of conditions are met, <code>java.lang.IllegalArgumentException</code> will be thrown
37      *
38      * @param o Object to calculate the size of
39      * @return Size of object according to rules, specified above
40      * @throws IllegalArgumentException if argument does not met rules, specified in
41      * <code>ConvertUtil.convertCollectionToList(java.lang.Object)</code>
42      * and does not provide no-argument <code>getSize()</code>
43      * or <code>getLength()</code> method or return type of this method
44      * is not <code>int</code> nor </code>java.lang.Integer</code>
45      * @see com.blandware.atleap.common.util.ConvertUtil#convertCollectionToList(java.lang.Object)
46      */

47     public static Integer JavaDoc sizeOf(Object JavaDoc o) {
48         int size = 0;
49         if ( o != null ) {
50             boolean sizeAcquired = false;
51             // convert specified collection to list
52
try {
53                 List JavaDoc list = ConvertUtil.convertCollectionToList(o);
54                 size = list.size();
55                 sizeAcquired = true;
56             } catch ( IllegalArgumentException JavaDoc e ) {
57                 // object is not convertable to list
58
}
59             if ( !sizeAcquired ) {
60                 // look for getSize() or getLength() method
61
Method JavaDoc sizeMethod = null;
62                 try {
63                     sizeMethod = o.getClass().getMethod("getSize", new Class JavaDoc[0]);
64                 } catch ( NoSuchMethodException JavaDoc e ) {
65                     // ignore
66
}
67
68                 if ( sizeMethod == null ) {
69                     try {
70                         sizeMethod = o.getClass().getMethod("getLength", new Class JavaDoc[0]);
71                     } catch ( NoSuchMethodException JavaDoc e ) {
72                         // ignore again
73
}
74                 }
75
76                 if ( sizeMethod != null ) {
77                     // if method has been found, invoke it on specified object
78
try {
79                         Integer JavaDoc oSize = (Integer JavaDoc) sizeMethod.invoke(o, (Object JavaDoc[]) null);
80                         size = oSize.intValue();
81                     } catch ( Exception JavaDoc e ) {
82                         throw new IllegalArgumentException JavaDoc(e.getClass().getName() + ": " + e.getMessage());
83                     }
84                 } else {
85
86                 }
87             }
88         }
89         return new Integer JavaDoc(size);
90     }
91
92
93     /**
94      * Constructor is private, because this class contains static methods only
95      */

96     private CollectionFunctions() {
97     }
98
99
100 }
101
Popular Tags