org.jmythapi.protocol.utils
Class PropertyAwareUtils

java.lang.Object
  extended by org.jmythapi.protocol.utils.PropertyAwareUtils

public class PropertyAwareUtils
extends Object


Constructor Summary
PropertyAwareUtils()
           
 
Method Summary
static
<P extends Enum<P>,E extends IPropertyAware<P>>
EnumMap<P,Object[]>
compare(IPropertyAware<P> source, IPropertyAware<P> target)
          Compares two property-aware objects.
static
<P extends Enum<P>,E extends IPropertyAware<P>>
Map<Object,List<E>>
groupListByProperty(Iterable<E> list, P prop)
           Usage example:

    // query recorded programs
    IProgramInfoList allRecordings = backend.queryRecordings();
    
    // Group programs by category
    Map<Object,List<IProgramInfo>> recordingsByCategory = PropertyAwareUtils.groupListByProperty(allRecordings,IProgramInfo.Props.CATEGORY);
    for(Entry<Object,List<IProgramInfo>> entry : recordingsByCategory.entrySet()) {
       System.out.println(String.format(
          "\r\nCATEGORY '%s': %02d recordings",
          entry.getKey(),
          entry.getValue().size()
       ));
       
       for(IProgramInfo program : entry.getValue()) {
          System.out.println(String.format(
             "- %s",
             program.getFullTitle()
          ));
       }
    }
 

static
<P extends Enum<P>,E extends IPropertyAware<P>>
Map<Object,List<E>>
groupListByProperty(Iterable<E> list, P prop, IFilter<P,E> filter)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PropertyAwareUtils

public PropertyAwareUtils()
Method Detail

groupListByProperty

public static <P extends Enum<P>,E extends IPropertyAware<P>> Map<Object,List<E>> groupListByProperty(Iterable<E> list,
                                                                                                      P prop)

Usage example:


    // query recorded programs
    IProgramInfoList allRecordings = backend.queryRecordings();
    
    // Group programs by category
    Map<Object,List<IProgramInfo>> recordingsByCategory = PropertyAwareUtils.groupListByProperty(allRecordings,IProgramInfo.Props.CATEGORY);
    for(Entry<Object,List<IProgramInfo>> entry : recordingsByCategory.entrySet()) {
       System.out.println(String.format(
          "\r\nCATEGORY '%s': %02d recordings",
          entry.getKey(),
          entry.getValue().size()
       ));
       
       for(IProgramInfo program : entry.getValue()) {
          System.out.println(String.format(
             "- %s",
             program.getFullTitle()
          ));
       }
    }
 

Type Parameters:
P - The class of the enumeration property used for grouping
E - The class of the property-aware object
Parameters:
list - A list of property-aware objects
prop - The property to use for grouping
Returns:
the grouped lists

groupListByProperty

public static <P extends Enum<P>,E extends IPropertyAware<P>> Map<Object,List<E>> groupListByProperty(Iterable<E> list,
                                                                                                      P prop,
                                                                                                      IFilter<P,E> filter)

compare

public static <P extends Enum<P>,E extends IPropertyAware<P>> EnumMap<P,Object[]> compare(IPropertyAware<P> source,
                                                                                          IPropertyAware<P> target)
Compares two property-aware objects.

This function compares two property-aware objects and determines those properties, whose values are not equal in the compared objects. As a result a enumeration map is returned containing the constants and values of all different properties.

Usage Example:


    // two property aware objects to compare
    IProgramInfo recording1 = ...;
    IProgramInfo recording2 = ...;
 
    // compare the objects
    EnumMap<IProgramInfo.Props,Object[]> diffMap = PropertyAwareUtils.compare(recording1,recording2);
    
    // print differences
    for(Entry<IProgramInfo.Props,Object[]> entry : diffMap.entrySet()){
       IProgramInfo.Props property = entry.getKey();
       Object[] values = entry.getValue();
       System.out.println(String.format(
          "Property %s is different: %s != %s",
          property, values[0],values[1]
       ));
    }
  

Type Parameters:
P - the type of the property
E - the type of the objects
Parameters:
source - the source object
target - the target object
Returns:
a map containing all properties that are different


Copyright © 2008-2013. All Rights Reserved.