1 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 ; 21 import java.util.List ; 22 23 30 public class CollectionFunctions { 31 32 47 public static Integer sizeOf(Object o) { 48 int size = 0; 49 if ( o != null ) { 50 boolean sizeAcquired = false; 51 try { 53 List list = ConvertUtil.convertCollectionToList(o); 54 size = list.size(); 55 sizeAcquired = true; 56 } catch ( IllegalArgumentException e ) { 57 } 59 if ( !sizeAcquired ) { 60 Method sizeMethod = null; 62 try { 63 sizeMethod = o.getClass().getMethod("getSize", new Class [0]); 64 } catch ( NoSuchMethodException e ) { 65 } 67 68 if ( sizeMethod == null ) { 69 try { 70 sizeMethod = o.getClass().getMethod("getLength", new Class [0]); 71 } catch ( NoSuchMethodException e ) { 72 } 74 } 75 76 if ( sizeMethod != null ) { 77 try { 79 Integer oSize = (Integer ) sizeMethod.invoke(o, (Object []) null); 80 size = oSize.intValue(); 81 } catch ( Exception e ) { 82 throw new IllegalArgumentException (e.getClass().getName() + ": " + e.getMessage()); 83 } 84 } else { 85 86 } 87 } 88 } 89 return new Integer (size); 90 } 91 92 93 96 private CollectionFunctions() { 97 } 98 99 100 } 101 | Popular Tags |