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

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

/**
 * A ClassDiscriminator discriminates Lists of
 * Objects by their class names.
 *
 * @author Randy Gettman
 * @since 0.5.0
 */
public class ClassDiscriminator 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 ClassChainedExtractor(extractor);
   }

   /**
    * A ClassChainedExtractor extracts the class name.
    * @param  The type of element.
    * @param  The type of object.
    */
   protected class ClassChainedExtractor extends ChainedExtractor
   {
      /**
       * Create a ClassChainedExtractor that uses the given
       * Extractor to retrieve the Class name.
       * @param extractor An Extractor whose label is a class
       *    name.
       */
      public ClassChainedExtractor(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 String getLabel(E element)
      {
         return myExtractor.getLabel(element).getClass().getName();
      }

      /**
       * The discrimination is complete when the chained Extractor
       * is complete.
       * @param element The element.
       * @return true if complete, false otherwise.
       */
      public boolean isComplete(E element)
      {
         // Only check the class names once!
         return myExtractor.isComplete(element) || myIndex >= 1;
      }
   }

   /**
    * Returns the Discriminator that discriminates on the class
    * name of the objects.
    * @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 class name
    *    of the objects.
    */
   protected  Discriminator getDiscriminator(List elements, ChainedExtractor extractor,
      int index)
   {
      return new CharSequenceDiscriminator();
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy