home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Extras / OSpace / jgl.exe / jgl_2_0 / src / COM / objectspace / jgl / Printing.java < prev    next >
Encoding:
Java Source  |  1997-03-14  |  3.1 KB  |  101 lines

  1. // Copyright(c) 1996,1997 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package COM.objectspace.jgl;
  5.  
  6. /**
  7.  * Printing is a class that contains generic printing algorithms.
  8.  * <p>
  9.  * @see COM.objectspace.jgl.examples.PrintingExamples
  10.  * @version 2.0.2
  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.   public 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 container
  72.    * to the standard output stream, System.out.
  73.    * @param container The container to describe.
  74.    */
  75.   public static void print( Container container )
  76.     {
  77.     System.out.print( toString( container.start(), container.finish() ) );
  78.     }
  79.  
  80.   /**
  81.    * Print the contents of the data structure associated with a particular iterator
  82.    * to the standard output stream, System.out, followed by a newline.
  83.    * @param first An iterator positioned at the first element to print.
  84.    * @param last An iterator positioned immediately after the last element to print.
  85.    */
  86.   public static void println( InputIterator first, InputIterator last )
  87.     {
  88.     System.out.println( toString( first, last ) );
  89.     }
  90.  
  91.   /**
  92.    * Print the contents of the container
  93.    * to the standard output stream, System.out, followed by a newline.
  94.    * @param container The container to describe.
  95.    */
  96.   public static void println( Container container )
  97.     {
  98.     System.out.println( toString( container.start(), container.finish() ) );
  99.     }
  100.   }
  101.