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

net.sf.jagg.msd.CollectionDiscriminator 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.Collection;
import java.util.List;

/**
 * A CollectionDiscriminator discriminates
 * Lists of Collections.  The Collections
 * are the items being discriminated.
 *
 * @author Randy Gettman
 * @since 0.5.0
 */
public class CollectionDiscriminator extends ChainedDiscriminator>
{
   /**
    * 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.
    */
   public  List> discriminate(List elements, Extractor> extractor, MsdWorkspace workspace)
   {
      RandomAccessListDiscriminator listDiscr = new RandomAccessListDiscriminator();
      ChainedExtractor, Collection> ce = getChainedExtractor(elements, extractor);
      return listDiscr.discriminate(elements, ce, workspace);
   }

   /**
    * Returns an appropriate ChainedExtractor.
    * @param elements The List of elements.
    * @param extractor A ChainedExtractor that returns appropriate
    *    labels.
    * @return An appropriate ChainedExtractor.
    */
   protected  ChainedExtractor, Collection> getChainedExtractor(List elements, Extractor> extractor)
   {
      return new CollectionChainedExtractor(extractor);
   }

   /**
    * A CollectionChainedExtractor turns Collections
    * into RandomAccess Lists.
    * @param  The type of element.
    * @param  The base type of the collection.
    */
   protected class CollectionChainedExtractor extends ChainedExtractor, Collection>
   {
      /**
       * Create a CollectionChainedExtractor that uses the given
       * Extractor to retrieve the collection.
       * @param extractor An Extractor whose label is a collection.
       */
      public CollectionChainedExtractor(Extractor> extractor)
      {
         super(extractor);
      }

      /**
       * The label is a random access list containing all members of the
       * collection.
       * @param element The element.
       * @return A random access list containing all members of the
       *    collection.
       */
      public List getLabel(E element)
      {
         Collection collection = myExtractor.getLabel(element);
         List list = new ArrayList();
         list.addAll(collection);
         return list;
      }

      /**
       * Completeness doesn't matter for this extractor.
       * @param element The element.
       * @return false.
       */
      public boolean isComplete(E element)
      {
         return myExtractor.isComplete(element);
      }
   }

   /**
    * Returns the Discriminator that discriminates on the
    * collection's base type.
    * @param elements The list of elements.
    * @param extractor The ChainedExtractor that was obtained from
    *    getChainedExtractor.
    * @param index The index of the loop.
    * @return A Discriminator that discriminates on the
    *    collection's base type.
    */
   @SuppressWarnings({"unchecked", "ForLoopReplaceableByForEach"})
   protected  Discriminator getDiscriminator(List elements, ChainedExtractor> extractor,
      int index)
   {
      for (int i = 0; i < elements.size(); i++)
      {
         E element = elements.get(i);
         if (!extractor.isComplete(element))
         {
            List member = (List) extractor.getLabel(element);
            if (member != null)
            {
               return (Discriminator) Discriminators.getDiscriminator(member.getClass());
            }
         }
      }
      return new NullDiscriminator(null);
   }
}