BeanUtils to extract a property out of an Array or Collection of Beans i.e. 
static <T> T[] getProperty(Object[] beansArray, String propertyName, Class<T> 
targetType)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------

                 Key: BEANUTILS-322
                 URL: https://issues.apache.org/jira/browse/BEANUTILS-322
             Project: Commons BeanUtils
          Issue Type: New Feature
          Components: Bean / Property Utils
    Affects Versions: LATER THAN 1.8.0
            Reporter: Giovanni Azua
             Fix For: LATER THAN 1.8.0


While developing math related applications several times I have had the 
following use case:

1) I have a collection or array of a given Bean type.
2) I need to calculate some statistic e.g. min, max, stddev or avg over a 
numeric property of the Bean type above.

The solutions around this are ugly since the application would need writing 
each time code to extract the property of interest and put it into an e.g. 
double[] Another tempting antipattern solution for other users is to instead of 
keeping as state a collection of Beans of a well defined type, and to avoid the 
continuous conversion they decide to maintain and pass around a bunch of arrays 
or collections.

I have developed this facility that could be added to e.g. BeanUtils

**************************************************************************************************************************************************

        @SuppressWarnings("unchecked")
        public static <T> T[] getProperty(Object[] beansArray, String name, 
Class<T> target) {
                
                assert beansArray != null : "'beansArray' must not be null";
                assert beansArray.length > 0 : "'beansArray' must not be empty";
                assert name != null : "'name' must not be null";
                assert target != null : "'target' must not be null";
                
                T[] result = null;              
                try
                {
                        ConvertUtilsBean converter = new ConvertUtilsBean();
                        
                        final int length = beansArray.length;
                        
                        result = (T[]) Array.newInstance(target, length);
                        
                        // we have zero element already ...
                        for (int i = 0; i < length; i++) {                      
        
                                result[i] = (T) 
converter.convert(BeanUtils.getProperty(beansArray[i], name), target);
                        }
                }
                // covert to RuntimeException and re-throw
                catch (Throwable exception) {
                        throw new RuntimeException(exception);
                }
                
                return result;
       }
               
       public static <T> List<T> getProperty(List<Object> beansCollection, 
String name, Class<T> target)                
                
                assert beansCollection!= null : "'beansCollection' must not be 
null";
                assert beansCollection.size() > 0 : "beansCollection' must not 
be empty";
                assert name != null : "'name' must not be null";
                assert target != null : "'target' must not be null";
                 
                return Arrays.asList(getProperty(beansCollection.toArray(), 
name, target));
       }

**************************************************************************************************************************************************




-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to