001    //  edu.isi.gamebots.clients.MessageBlock
002    //  Copyright 2000, University of Southern California,
003    //                  Information Science Institute
004    //
005    //  Personal and Educational use is hereby granted.
006    //  Permission required for commercial use and redistribution.
007    
008    
009    package edu.isi.gamebots.client;
010    
011    import java.lang.*;
012    import java.util.*;
013    
014    import edu.isi.gamebots.client.*;
015    
016    
017    /**
018     *  @author <a href="mailto:amarshal#gamebots@isi.edu">Andrew n marshall</a>
019     */
020    public class MessageBlock extends Message {
021      protected List messages; // of Messages
022      
023      MessageBlock( GamebotsClient client,
024      String type,
025      Properties props,
026      List messages,
027      boolean complete ) {
028        super( client, type, props, complete );
029        
030        this.messages = messages;
031      }
032      
033      public Iterator getMessages() {
034        return messages.iterator();
035      }
036      
037      // added by Ryan Rozich
038      // returns a new message block filtered to only contain the messages of
039      // node-type object senses
040      public MessageBlock onlyNodeMessages(){
041        MessageBlock newBlock = new MessageBlock((GamebotsClient)source, type, props, messages, complete);
042        // filter out all sync messages that are not visible nodes or objects
043        for(int i=0; i<newBlock.messages.size();){
044          Message msg = (Message)newBlock.messages.get(i);
045          if(msg.getType().equalsIgnoreCase(GamebotsConstants.BEG) || msg.getType().equalsIgnoreCase(GamebotsConstants.END) || msg.getType().equalsIgnoreCase(GamebotsConstants.SELF) || msg.getType().equalsIgnoreCase(GamebotsConstants.GAMESTATE)) newBlock.messages.remove(i);
046          else i++;
047        }
048        return newBlock;
049      }
050      
051      public String getMessageString( StringBuffer sb, int indent ) {
052        return super.toString( sb, indent ).toString();
053      }
054      
055      // added by Ryan Rozich
056      public Message getMessageAt(int index){
057        try{
058          return (Message)messages.get(index);
059        }
060        catch(IndexOutOfBoundsException e){
061          return null;
062        }
063      }
064      
065      // added by Ryan Rozich
066      public int getNumMessages(){
067        return messages.size();
068      }
069      
070      public StringBuffer toString( StringBuffer sb, int indent ) {
071        super.toString( sb, indent );
072        indent += 2;
073        
074        Iterator i = getMessages();
075        while( i.hasNext() ) {
076          sb.append( '\n' );
077          ((Message) i.next()).toString( sb, indent );
078        }
079        
080        return sb;
081      }
082    }