001 package stringtransformers; 002 003 import java.util.Vector; 004 import java.util.Enumeration; 005 import cs101.io.connection.InputConnection; 006 import cs101.io.connection.OutputConnection; 007 import cs101.io.connection.ConnectionRejectedException; 008 import cs101.io.Console; 009 010 public class AlternatingOutputter extends StringTransformer 011 { 012 private Vector outVector = new Vector(); 013 private int whoseTurn = 0; 014 015 public AlternatingOutputter () 016 { 017 super(); 018 } 019 020 // public synchronized void addOutputConnection( OutputConnection out ) 021 022 // note the above was because AlternatingOutputter's Vector might be changed 023 // while the act method is stepping through the enumerations, but 024 // caused a deadlock. Is this a problem? 025 026 public void addOutputConnection( OutputConnection out ) 027 throws ConnectionRejectedException 028 { 029 030 Console.print ( this + " adding "); 031 if ( this.outVector.isEmpty() ) 032 { 033 this.outVector.addElement( this.visualizer.interceptOutput( out ) ); 034 Console.print ( "first " ); 035 } 036 else 037 { 038 this.outVector.addElement( out ); 039 Console.print ( "subsequent " ); 040 } 041 Console.println( "output connection"); 042 } 043 044 // public synchronized void act () 045 public void act () 046 { 047 048 if ( ( this.in != null ) && ( ! this.outVector.isEmpty() ) ) 049 { 050 String string = this.transform( this.in.readInput() ); 051 052 if ( this.whoseTurn >= this.outVector.size() ) 053 { 054 this.whoseTurn = 0; 055 } 056 057 ( (OutputConnection) outVector.elementAt( this.whoseTurn ) ).writeOutput( string ); 058 059 this.whoseTurn = this.whoseTurn + 1; 060 061 } 062 } 063 064 public String transform( String input ) 065 { 066 return input; 067 } 068 069 } 070