home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / Algorithms1.java < prev    next >
Encoding:
Java Source  |  1997-07-30  |  1010 b   |  39 lines

  1. // Copyright(c) 1996,1997 ObjectSpace, Inc.
  2. import COM.objectspace.jgl.*;
  3.  
  4. /**
  5.  * Applying provides algorithms for affecting every element in a container.
  6.  *
  7.  * @see COM.objectspace.jgl.Applying
  8.  * @version 2.0.2
  9.  * @author ObjectSpace, Inc.
  10.  */
  11.  
  12. public class Algorithms1
  13.   {
  14.   public static void main( String[] args )
  15.     {
  16.     Array array1 = new Array();
  17.     array1.add( "cat" );
  18.     array1.add( "monkey" );
  19.     array1.add( "goat" );
  20.     Applying.forEach( array1, new PrintFunction() );
  21.  
  22.     SList list = new SList();
  23.     list.add( new Integer( 3 ) );
  24.     list.add( new Integer( 7 ) );
  25.     list.add( new Integer( 4 ) );
  26.     Integer total = (Integer) Applying.inject( list, new Integer( 0 ), new PlusNumber() );
  27.     System.out.println( "list = " + list + ", total = " + total );
  28.     }
  29.   }
  30.  
  31. class PrintFunction implements UnaryFunction
  32.   {
  33.   public Object execute( Object object )
  34.     {
  35.     System.out.println( "PRINT " + object );
  36.     return null; // Not used.
  37.     }
  38.   }
  39.