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

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package jgl;
  5.  
  6. /**
  7.  * Printing is a class that contains generic printing algorithms.
  8.  * <p>
  9.  * @see jgl.examples.PrintingExamples
  10.  * @version 1.1
  11.  * @author ObjectSpace, Inc.
  12.  */
  13.  
  14. public final class Printing
  15.   {
  16.   private Printing()
  17.     {
  18.     }
  19.  
  20.   /**
  21.    * Return a string that describes a container.
  22.    * @param container The container to describe.
  23.    * @param name The type of the container.
  24.    * @return A string that describes the container.
  25.    */
  26.   static String toString( Container container, String name )
  27.     {
  28.     return name + toString( container.start(), container.finish() );
  29.     }
  30.  
  31.   /**
  32.    * Return a string that describes the contents of the sequence
  33.    * associated with an iterator.
  34.    * @param first An iterator positioned at the first element to describe.
  35.    * @param last An iterator positioned immediately after the last element to describe.
  36.    * @return A string that describes the container's contents.
  37.    */
  38.   public static String toString( InputIterator first, InputIterator last )
  39.     {
  40.     if( first.atEnd() )
  41.       return "()";
  42.  
  43.     InputIterator firstx = (InputIterator) first.clone();
  44.     StringBuffer buffer = new StringBuffer();
  45.     buffer.append( "( " );
  46.  
  47.     while( !firstx.equals( last ) )
  48.       {
  49.       buffer.append( firstx.nextElement() );
  50.  
  51.       if( !firstx.equals( last ) )
  52.         buffer.append( ", " );
  53.       }
  54.  
  55.     buffer.append( " )");
  56.     return buffer.toString();
  57.     }
  58.  
  59.   /**
  60.    * Print the contents of the data structure associated with a particular iterator
  61.    * to the standard output stream, System.out.
  62.    * @param first An iterator positioned at the first element to print.
  63.    * @param last An iterator positioned immediately after the last element to print.
  64.    */
  65.   public static void print( InputIterator first, InputIterator last )
  66.     {
  67.     System.out.print( toString( first, last ) );
  68.     }
  69.  
  70.   /**
  71.    * Print the contents of the data structure associated with a particular iterator
  72.    * to the standard output stream, System.out, followed by a newline.
  73.    * @param first An iterator positioned at the first element to print.
  74.    * @param last An iterator positioned immediately after the last element to print.
  75.    */
  76.   public static void println( InputIterator first, InputIterator last )
  77.     {
  78.     System.out.println( toString( first, last ) );
  79.     }
  80.   }
  81.  
  82.