home *** CD-ROM | disk | FTP | other *** search
/ ActiveX Programming Unleashed CD / AXU.iso / jgl_1_1 / jgl_1_1.exe / src / Replacing.java < prev    next >
Encoding:
Java Source  |  1996-09-10  |  8.3 KB  |  210 lines

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package jgl;
  5.  
  6. /**
  7.  * The Replacing class contains generic replacing algorithms.
  8.  * <p>
  9.  * @see jgl.examples.ReplacingExamples
  10.  * @version 1.1
  11.  * @author ObjectSpace, Inc.
  12.  */
  13.  
  14. public final class Replacing
  15.   {
  16.   private Replacing()
  17.     {
  18.     }
  19.  
  20.   /**
  21.    * Traverse a sequence and replace every occurrence of a particular object with another.
  22.    * The time complexity is linear and the space complexity is constant.
  23.    * @param first An iterator positioned at the first element of the sequence.
  24.    * @param last An iterator positioned immediately after the last element of the sequence.
  25.    * @param oldValue The object to be replaced.
  26.    * @param newValue The replacement object.
  27.    * @return The number of objects that were replaced.
  28.    */
  29.   public static int replace( ForwardIterator first, ForwardIterator last, Object oldValue, Object newValue )
  30.     {
  31.     ForwardIterator firstx = (ForwardIterator) first.clone();
  32.     int n = 0;
  33.  
  34.     while( !firstx.equals( last ) )
  35.       {
  36.       if( firstx.get().equals( oldValue ) )
  37.         {
  38.         firstx.put( newValue );
  39.         ++n;
  40.         }
  41.  
  42.       firstx.advance();
  43.       }
  44.  
  45.     return n;
  46.     }
  47.  
  48.   /**
  49.    * Traverse a container and replace every occurrence of a particular object with another.
  50.    * The time complexity is linear and the space complexity is constant.
  51.    * @param container The container.
  52.    * @param last An iterator positioned immediately after the last element of the sequence.
  53.    * @param oldValue The object to be replaced.
  54.    * @param newValue The replacement object.
  55.    * @return The number of objects that were replaced.
  56.    */
  57.   public static int replace( Container container, Object oldValue, Object newValue )
  58.     {
  59.     return replace( container.start(), container.finish(), oldValue, newValue );
  60.     }
  61.  
  62.   /**
  63.    * Traverse a sequence and replace every object that satisfies a predicate with another. 
  64.    * The time complexity is linear and the space complexity is constant.
  65.    * @param first An iterator positioned at the first element of the sequence.
  66.    * @param last An iterator positioned immediately after the last element of the sequence.
  67.    * @param predicate A unary predicate.
  68.    * @param newValue The replacement object.
  69.    * @return The number of objects that were replaced.
  70.    */
  71.   public static int replaceIf( ForwardIterator first, ForwardIterator last, UnaryPredicate predicate, Object newValue )
  72.     {
  73.     ForwardIterator firstx = (ForwardIterator) first.clone();
  74.     int n = 0;
  75.  
  76.     while( !firstx.equals( last ) )
  77.       {
  78.       if( predicate.execute( firstx.get() ) )
  79.         {
  80.         firstx.put( newValue );
  81.         ++n;
  82.         }
  83.  
  84.       firstx.advance();
  85.       }
  86.  
  87.     return n;
  88.     }
  89.  
  90.   /**
  91.    * Traverse a container and replace every object that satisfies a predicate with another. 
  92.    * The time complexity is linear and the space complexity is constant.
  93.    * @param container The container.
  94.    * @param predicate A unary predicate.
  95.    * @param newValue The replacement object.
  96.    * @return The number of objects that were replaced.
  97.    */
  98.   public static int replaceIf( Container container, UnaryPredicate predicate, Object newValue )
  99.     {
  100.     return replaceIf( container.start(), container.finish(), predicate, newValue );
  101.     }
  102.  
  103.   /**
  104.    * Copy one sequence to another of the same size, replacing all occurrences of a
  105.    * particular object with another. The time complexity is linear and the space 
  106.    * complexity is constant.
  107.    * @param first An iterator positioned at the first element of the input sequence.
  108.    * @param last An iterator positioned immediately after the last element of the input sequence.
  109.    * @param result An iterator positioned at the first element of the output sequence.
  110.    * @param oldValue The object to be replaced.
  111.    * @param newValue The replacement object.
  112.    * @return An iterator positioned immediately after the last element of the output sequence.
  113.    */
  114.   public static OutputIterator replaceCopy( InputIterator first, InputIterator last, OutputIterator result, Object oldValue, Object newValue )
  115.     {
  116.     InputIterator firstx = (InputIterator) first.clone();
  117.     OutputIterator resultx = (OutputIterator) result.clone();
  118.  
  119.     while( !firstx.equals( last ) )
  120.       {
  121.       resultx.put( firstx.get().equals( oldValue ) ? newValue : firstx.get() );
  122.       resultx.advance();
  123.       firstx.advance();
  124.       }
  125.  
  126.     return resultx;
  127.     }
  128.  
  129.   /**
  130.    * Copy a container to a sequence, replacing all occurrences of a particular object 
  131.    * with another. The time complexity is linear and the space complexity is constant.
  132.    * @param input The input container.
  133.    * @param result An iterator positioned at the first element of the output sequence.
  134.    * @param oldValue The object to be replaced.
  135.    * @param newValue The replacement object.
  136.    * @return An iterator positioned immediately after the last element of the output sequence.
  137.    */
  138.   public static OutputIterator replaceCopy( Container input, OutputIterator result, Object oldValue, Object newValue )
  139.     {
  140.     return replaceCopy( input.start(), input.finish(), result, oldValue, newValue );
  141.     }
  142.  
  143.   /**
  144.    * Copy a container to another container, replacing all occurrences of a particular object 
  145.    * with another. The time complexity is linear and the space complexity is constant.
  146.    * @param source The source container.
  147.    * @param destination The destination container.
  148.    * @param oldValue The object to be replaced.
  149.    * @param newValue The replacement object.
  150.    */
  151.   public static void replaceCopy( Container source, Container destination, Object oldValue, Object newValue )
  152.     {
  153.     replaceCopy( source.start(), source.finish(), new InsertIterator( destination ), oldValue, newValue );
  154.     }
  155.  
  156.   /**
  157.    * Copy one sequence to another of the same size, replacing all objects that satisfy 
  158.    * a predicate with another. The time complexity is linear and the space complexity 
  159.    * is constant.
  160.    * @param first An iterator positioned at the first element of the input sequence.
  161.    * @param last An iterator positioned immediately after the last element of the input sequence.
  162.    * @param result An iterator positioned at the first element of the output sequence.
  163.    * @param predicate A unary predicate.
  164.    * @param newValue The replacement object.
  165.    * @return An iterator positioned immediately after the last element of the output sequence.
  166.    */
  167.   public static OutputIterator replaceCopyIf( InputIterator first, InputIterator last, OutputIterator result, UnaryPredicate predicate, Object newValue )
  168.     {
  169.     InputIterator firstx = (InputIterator) first.clone();
  170.     OutputIterator resultx = (OutputIterator) result.clone();
  171.  
  172.     while( !firstx.equals( last ) )
  173.       {
  174.       resultx.put( predicate.execute( firstx.get() ) ? newValue : firstx.get() );
  175.       resultx.advance();
  176.       firstx.advance();
  177.       }
  178.  
  179.     return resultx;
  180.     }
  181.  
  182.   /**
  183.    * Copy a container to a sequence, replacing all objects that satisfy a predicate 
  184.    * with another. The time complexity is linear and the space complexity is constant.
  185.    * @param input The input container.
  186.    * @param result An iterator positioned at the first element of the output sequence.
  187.    * @param predicate A unary predicate.
  188.    * @param newValue The replacement object.
  189.    * @return An iterator positioned immediately after the last element of the output sequence.
  190.    */
  191.   public static OutputIterator replaceCopyIf( Container input, OutputIterator result, UnaryPredicate predicate, Object newValue )
  192.     {
  193.     return replaceCopyIf( input.start(), input.finish(), result, predicate, newValue );
  194.     }
  195.  
  196.   /**
  197.    * Copy a container to another container, replacing all objects that satisfy a predicate 
  198.    * with another. The time complexity is linear and the space complexity is constant.
  199.    * @param source The source container.
  200.    * @param destination The destination container.
  201.    * @param predicate A unary predicate.
  202.    * @param newValue The replacement object.
  203.    */
  204.   public static void replaceCopyIf( Container source, Container destination, UnaryPredicate predicate, Object newValue )
  205.     {
  206.     replaceCopyIf( source.start(), source.finish(), new InsertIterator( destination ), predicate, newValue );
  207.     }
  208.   }
  209.  
  210.