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

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package jgl;
  5.  
  6. /**
  7.  * The Transforming class contains generic transforming algorithms.
  8.  * <p>
  9.  * @see jgl.examples.TransformingExamples
  10.  * @version 1.1
  11.  * @author ObjectSpace, Inc.
  12.  */
  13.  
  14. public final class Transforming
  15.   {
  16.   private Transforming()
  17.     {
  18.     }
  19.  
  20.   /**
  21.    * Traverse a sequence and store the results of invoking a UnaryFunction on each
  22.    * element into another sequence of the same size. The time complexity is linear and
  23.    * the space complexity is constant.
  24.    * @param first An iterator positioned at the first element of the input sequence.
  25.    * @param last An iterator positioned immediately after the last element of the input sequence.
  26.    * @param result An iterator positioned at the first element of the output sequence.
  27.    * @param function A uanry function.
  28.    * @return An iterator positioned immediately after the last element of the output sequence.
  29.    */
  30.   public static OutputIterator transform( InputIterator first, InputIterator last, OutputIterator result, UnaryFunction function )
  31.     {
  32.     InputIterator firstx = (InputIterator) first.clone();
  33.     OutputIterator resultx = (OutputIterator) result.clone();
  34.  
  35.     while( !firstx.equals( last ) )
  36.       {
  37.       resultx.put( function.execute( firstx.nextElement() ) );
  38.       resultx.advance();
  39.       }
  40.  
  41.     return resultx;
  42.     }
  43.  
  44.   /**
  45.    * Traverse a container and store the results of invoking a UnaryFunction on each
  46.    * element into a sequence. The time complexity is linear and the space complexity is 
  47.    * constant.
  48.    * @param input The input container.
  49.    * @param result An iterator positioned at the first element of the output sequence.
  50.    * @param function A unary function.
  51.    * @return An iterator positioned immediately after the last element of the output sequence.
  52.    */
  53.   public static OutputIterator transform( Container input, OutputIterator result, UnaryFunction function )
  54.     {
  55.     return transform( input.start(), input.finish(), result, function );
  56.     }
  57.  
  58.   /**
  59.    * Traverse a container and add the results of invoking a UnaryFunction on each
  60.    * element into another container. The time complexity is linear and the space complexity is 
  61.    * constant.
  62.    * @param source The source container.
  63.    * @param desination The destination container.
  64.    * @param function A unary function.
  65.    * @return An iterator positioned immediately after the last element of the output sequence.
  66.    */
  67.   public static void transform( Container source, Container destination, UnaryFunction function )
  68.     {
  69.     transform( source.start(), source.finish(), new InsertIterator( destination ), function );
  70.     }
  71.  
  72.   /**
  73.    * Traverse two sequences and store the results of invoking a BinaryFunction on 
  74.    * corresponding elements into another sequence of the same size. Stop when the
  75.    * end of the first sequence is reached. The time complexity is linear and
  76.    * the space complexity is constant.
  77.    * @param first1 An iterator positioned at the first element of the first input sequence.
  78.    * @param last1 An iterator positioned immediately after the last element of the first input sequence.
  79.    * @param first2 An iterator positioned at the first element of the second input sequence.
  80.    * @param result An iterator positioned at the first element of the output sequence.
  81.    * @param function A binary function.
  82.    * @return An iterator positioned immediately after the last element of the output sequence.
  83.    */
  84.   static public OutputIterator transform( InputIterator first1, InputIterator last1, InputIterator first2, OutputIterator result, BinaryFunction function )
  85.     {
  86.     InputIterator first1x = (InputIterator) first1.clone();
  87.     InputIterator first2x = (InputIterator) first2.clone();
  88.     OutputIterator resultx = (OutputIterator) result.clone();
  89.  
  90.     while( !first1x.equals( last1 ) )
  91.       {
  92.       resultx.put( function.execute( first1x.nextElement(), first2x.nextElement() ) );
  93.       resultx.advance();
  94.       }
  95.  
  96.     return resultx;
  97.     }
  98.  
  99.   /**
  100.    * Traverse two containers and store the results of invoking a BinaryFunction on 
  101.    * corresponding elements into a sequence. Stop when the end of the first container is 
  102.    * reached. The time complexity is linear and the space complexity is constant.
  103.    * @param input1 The first input container.
  104.    * @param input2 The second input container.
  105.    * @param result An iterator positioned at the first element of the output sequence.
  106.    * @param function A binary function.
  107.    * @return An iterator positioned immediately after the last element of the output container.
  108.    */
  109.   static public OutputIterator transform( Container input1, Container input2, OutputIterator result, BinaryFunction function )
  110.     {
  111.     return transform( input1.start(), input1.finish(), input2.start(), result, function );
  112.     }
  113.  
  114.   /**
  115.    * Traverse two containers and add the results of invoking a BinaryFunction on 
  116.    * corresponding elements to another container. Stop when the end of the first 
  117.    * container is reached. The time complexity is linear and the space complexity 
  118.    * is constant.
  119.    * @param input1 The first input container.
  120.    * @param input2 The second input container.
  121.    * @param output The output container.
  122.    * @param function A binary function.
  123.    * @return An iterator positioned immediately after the last element of the output container.
  124.    */
  125.   static public void transform( Container input1, Container input2, Container output, BinaryFunction function )
  126.     {
  127.     transform( input1.start(), input1.finish(), input2.start(), new InsertIterator( output ), function );
  128.     }
  129.  
  130.   /**
  131.    * Return a container that is the same class as the original and contains
  132.    * the result of applying the given unary function to each element in the range.
  133.    * The original container is not modified.
  134.    * @param first An iterator positioned at the first element in the range.
  135.    * @param last An iterator positioned immediately after the last element in the range.
  136.    * @param function A unary function.
  137.    * @return A new container that contains the result of applying the unary function.
  138.    * @exception IllegalArgumentException If the iterator container types are different.
  139.    */
  140.   public static Container collect( ForwardIterator first, ForwardIterator last, UnaryFunction function )
  141.     {
  142.     Class firstClass = first.getContainer().getClass();
  143.     Class lastClass = last.getContainer().getClass();
  144.  
  145.     if( firstClass != lastClass )
  146.       throw new IllegalArgumentException( "iterator containers must be the same" );
  147.  
  148.     Container container = null;
  149.     
  150.     try
  151.       {
  152.       container = (Container)(firstClass.newInstance());
  153.       }
  154.     catch( InstantiationException exception )
  155.       {
  156.       return null;
  157.       }
  158.     catch( IllegalAccessException exception )
  159.       {
  160.       return null;
  161.       }
  162.  
  163.     ForwardIterator firstx = (ForwardIterator) first.clone();
  164.     
  165.     while( !firstx.equals( last ) )
  166.       container.add( function.execute( firstx.nextElement() ) );
  167.  
  168.     return container;
  169.     }
  170.   
  171.   /**
  172.    * Return a container that is the same class as the original and contains
  173.    * the result of applying the given unary function to each element in the original.
  174.    * The original container is not modified.
  175.    * @param container A container.
  176.    * @param function A unary function.
  177.    * @return A new container containing the result of applying the unary function.
  178.    */
  179.   public static Container collect( Container container, UnaryFunction function )
  180.     {
  181.     return collect( container.start(), container.finish(), function );
  182.     }
  183.   }
  184.  
  185.