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

net.sf.jagg.msd.ArrayDiscriminator 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.List;

/**
 * An ArrayDiscriminator discriminates Lists of
 * arrays.
 *
 * @author Randy Gettman
 * @since 0.5.0
 */
public class ArrayDiscriminator extends ChainedDiscriminator
{
   /**
    * Returns an appropriate ChainedExtractor.
    * @param elements The List of elements.
    * @param extractor A ChainedExtractor that returns appropriate
    *    labels.
    * @return An appropriate ChainedExtractor.
    */
   protected  ChainedExtractor getChainedExtractor(List elements, Extractor extractor)
   {
      return new ArrayChainedExtractor(extractor);
   }

   /**
    * An ArrayChainedExtractor extracts members of an array as
    * labels.
    * @param  The type of element.
    * @param  The base type of the array.
    */
   protected class ArrayChainedExtractor extends ChainedExtractor
   {
      /**
       * Create an ArrayChainednExtractor that uses the given
       * Extractor to retrieve the array.
       * @param extractor An Extractor whose label is an array.
       */
      public ArrayChainedExtractor(Extractor extractor)
      {
         super(extractor);
      }

      /**
       * The label is the specific member of the array.
       * @param element The element.
       * @return A specific member of the array.
       */
      public B getLabel(E element)
      {
         return myExtractor.getLabel(element)[myIndex];
      }

      /**
       * The discrimination is complete when the process runs off the end of
       * the array.
       * @param element The element.
       * @return true if off the end of the array,
       *    false otherwise.
       */
      public boolean isComplete(E element)
      {
         return myExtractor.isComplete(element) || myIndex >= myExtractor.getLabel(element).length;
      }
   }

   /**
    * Returns the Discriminator that discriminates on the array'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 array'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))
         {
            T member = (T) extractor.getLabel(element);
            if (member != null)
            {
               return (Discriminator) Discriminators.getDiscriminator(member.getClass());
            }
         }
      }
      return new NullDiscriminator(null);
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy