home *** CD-ROM | disk | FTP | other *** search
/ ActiveX Programming Unleashed CD / AXU.iso / jgl_1_1 / src / applying.java < prev    next >
Encoding:
Java Source  |  1996-09-12  |  3.7 KB  |  99 lines

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package jgl;
  5.  
  6. /**
  7.  * The Applying class contains generic applying algorithms.
  8.  * <p>
  9.  * @see jgl.examples.ApplyingExamples
  10.  * @version 1.1
  11.  * @author ObjectSpace, Inc.
  12.  */
  13.  
  14. public final class Applying
  15.   {
  16.   private Applying()
  17.     {
  18.     }
  19.  
  20.   /**
  21.    * Apply a unary function to every element in a specified range.
  22.    * The time complexity is linear and the space complexity is constant.
  23.    * @param first An iterator positioned at the first element in the range.
  24.    * @param last An iterator positioned immediately after the last element in the range.
  25.    * @param function The unary function to apply.
  26.    * @return The unary function.
  27.    */
  28.   public static UnaryFunction forEach( InputIterator first, InputIterator last, UnaryFunction function )
  29.     {
  30.     InputIterator firstx = (InputIterator) first.clone();
  31.  
  32.     while( !firstx.equals( last ) )
  33.       {
  34.       function.execute( firstx.get() );
  35.       firstx.advance();
  36.       }
  37.  
  38.     return function;
  39.     }
  40.  
  41.   /**
  42.    * Apply a unary function to every element in a container.
  43.    * The time complexity is linear and the space complexity is constant.
  44.    * @param container The container.
  45.    * @param function The unary function to apply.
  46.    * @return The unary function.
  47.    */
  48.   public static UnaryFunction forEach( Container container, UnaryFunction function )
  49.     {
  50.     return forEach( container.start(), container.finish(), function );
  51.     }
  52.   
  53.   /**
  54.    * Inject a specified range with a binary function and an initial value.
  55.    * Calculate the initial result by calling the function with the initial value as the 
  56.    * first parameter and the first element as the second parameter. Then apply the function 
  57.    * to the remaining elements, using the previous result as the first parameter and the
  58.    * next element as the second parameter. Return the last result.
  59.    * @param first An iterator positioned at the first element in the range.
  60.    * @param last An iterator positioned immediately after the last element in the range.
  61.    * @param object An object to use as a starting result to the binary function.
  62.    * @param function A binary function.
  63.    * @return The result of the last binary function call.
  64.    * @exception IllegalArgumentException If the iterator container types are different.
  65.    */
  66.   public static Object inject( ForwardIterator first, ForwardIterator last, Object object, BinaryFunction function )
  67.     {
  68.     Class firstClass = first.getContainer().getClass();
  69.     Class lastClass = last.getContainer().getClass();
  70.  
  71.     if( firstClass != lastClass )
  72.       throw new IllegalArgumentException( "iterator containers must be the same" );
  73.  
  74.     ForwardIterator firstx = (ForwardIterator) first.clone();
  75.  
  76.     while( !firstx.equals( last ) )
  77.       object = function.execute( object, firstx.nextElement() );
  78.  
  79.     return object;
  80.     }
  81.   
  82.   /**
  83.    * Inject a container with a binary function and an initial value.
  84.    * Calculate the initial result by calling the function with the initial value as the 
  85.    * first parameter and the first element as the second parameter. Then apply the function 
  86.    * to the remaining elements, using the previous result as the first parameter and the
  87.    * next element as the second parameter. Return the last result.
  88.    * @param container A container.
  89.    * @param object An object to use as a starting result to the binary function.
  90.    * @param function A binary function.
  91.    * @return The result of the last binary function call.
  92.    */
  93.   public static Object inject( Container container, Object object, BinaryFunction function )
  94.     {
  95.     return inject( container.start(), container.finish(), object, function );
  96.     }
  97.   }
  98.  
  99.