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

net.sf.jagg.msd.CharacterDiscriminator 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 CharacterDiscriminator discriminates Lists of
 * Characters.
 *
 * @author Randy Gettman
 * @since 0.5.0
 */
public class CharacterDiscriminator extends PortionDiscriminator
{
   /**
    * Partitions the given List of values into another
    * List, in which all of the values from the given list exist
    * in the new list, and all values 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 values
    *    that compare equal to each other.
    */
   public  List> discriminate(List elements, Extractor extractor, MsdWorkspace workspace)
   {
      CharExtractor ce = getPortionExtractor(extractor);
      return discriminatePortion(elements, ce, workspace);
   }

   /**
    * Returns a portion extractor appropriate for Characters.
    * @param e An Extractor that returns appropriate labels.
    * @return An appropriate PortionExtractor.
    */
   protected  CharExtractor getPortionExtractor(Extractor e)
   {
      return new CharExtractor(e);
   }

   /**
    * A CharExtractor extracts integer labels by converting the
    * character into an integer.  It does not use the index.
    */
   protected class CharExtractor extends PortionExtractor
   {
      /**
       * Create a CharExtractor that first uses the given
       * Extractor to get the value.
       * @param extractor Another Extractor.
       */
      public CharExtractor(Extractor extractor)
      {
         super(extractor);
      }

      /**
       * Create an integer label out of the extractor's label.
       * @param element The element.
       * @return An integer label.
       */
      public int getLabel(E element)
      {
         return (int) myExtractor.getLabel(element).charValue();
      }

      /**
       * Determines whether discrimination is complete for the given element, at
       * the given index.
       * @param element The element.
       * @return true if discrimination is complete or cannot
       *    continue, usually because the discrimination has run off the end of
       *    the label, false otherwise.
       */
      public boolean isComplete(E element)
      {
         // The index is not an issue here.
         return myExtractor.isComplete(element);
      }
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy