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

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

/**
 * A CharSequenceDiscriminator discriminates Lists of
 * CharSequences, such as Strings.
 *
 * @author Randy Gettman
 * @since 0.5.0
 */
public class CharSequenceDiscriminator extends PortionDiscriminator
{
   /**
    * Returns an appropriate PortionExtractor.
    * @param extractor A PortionExtractor that returns appropriate
    *    labels.
    * @return An appropriate PortionExtractor.
    */
   protected  PortionExtractor getPortionExtractor(Extractor extractor)
   {
      return new CharPortionExtractor(extractor);
   }

   /**
    * A CharPortionExtractor is a PortionExtractor
    * that extracts portions of CharSequences for their labels.
    */
   protected class CharPortionExtractor extends PortionExtractor
   {
      /**
       * Create a CharPortionExtractor that first uses the given
       * Extractor to get the value.
       * @param extractor Another Extractor.
       */
      public CharPortionExtractor(Extractor extractor)
      {
         super(extractor);
      }

      /**
       * Returns the label, which is the specific character at the index,
       * converted to an integer.
       * @param element The element.
       * @return The the specific character at the index, converted to an
       *    integer.
       */
      public int getLabel(E element)
      {
         return (int) myExtractor.getLabel(element).charAt(myIndex);
      }

      /**
       * Discrimination is complete if the index has reached the length of the
       * CharSequence.
       * @param element An element.
       * @return true if off the end of the
       *    CharSequence, false otherwise.
       */
      public boolean isComplete(E element)
      {
         if (!myExtractor.isComplete(element))
         {
            int length = myExtractor.getLabel(element).length();
            return myIndex >= length;
         }
         return true;
      }
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy