All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.jagg.msd.DiscriminableDiscriminator Maven / Gradle / Ivy

Go to download

jAgg is a Java 5.0 API that supports “group by” operations on Lists of Java objects: aggregate operations such as count, sum, max, min, avg, and many more. It also allows custom aggregate operations.

The newest version!
package net.sf.jagg.msd;

import java.util.ArrayList;
import java.util.List;

/**
 * A DiscriminableDiscriminator assumes that the type of elements
 * is Discriminable.  It extracts the properties, then delegates
 * to a PropertiesDiscriminator.
 *
 * @author Randy Gettman
 * @since 0.5.0
 * @see PropertiesDiscriminator
 */
public class DiscriminableDiscriminator extends AbstractDiscriminator
{
   /**
    * Partitions the given List of elements into another
    * List, in which all of the elements from the given list exist
    * in the new list, and all elements that compare equal are adjacent to each
    * other, according to the given Extractor.
    * @param elements A List of elements.
    * @param extractor An Extractor that gives labels for
    *    each element.
    * @param workspace The MsdWorkspace used in the discrimination
    *    process.
    * @return A List of Lists containing all
    *    equivalence classes.  Each equivalence class list contains all elements
    *    that compare equal to each other.
    * @throws ClassCastException If an encountered object was not
    *    Discriminable.
    * @see Discriminable
    */
   public  List> discriminate(List elements, Extractor extractor, MsdWorkspace workspace)
   {
      int size = elements.size();
      if (size == 0)
         return new ArrayList>(0);

      T label = null;
      for (int i = 0; i < size; i++)
      {
         E element = elements.get(i);
         label = extractor.getLabel(element);
         if (label != null)
            break;
      }

      Discriminator pd;
      if (label == null)
      {
         // All nulls
         pd = new NullDiscriminator(null);
      }
      else
      {
         Discriminable d = (Discriminable) label;
         List properties = d.getDiscriminableProperties();
         pd = new PropertiesDiscriminator(properties);
      }
      return pd.discriminate(elements, extractor, workspace);
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy