
import java.net.*;
import java.io.*;
import java.util.*;

/**
 * This class represents a set of Translations in various
 * languages.
 *
 * tested in jvm1.1 and jview
 *
 * @see "Translation"
 * @author http://bumble.sf.net
 *
 */ 

public class TranslationSet extends Object
{
  //--------------------------------------------
  private Vector set;
  //--------------------------------------------
  public static String NEWLINE = System.getProperty("line.separator");


  //--------------------------------------------
  /** construct a new empty set to hold translations which
   *  will be added later, possibly by a Record object */ 
  public TranslationSet()
  {
    this.set = new Vector();
  } //-- constr: ()


  //--------------------------------------------
  /** how many translations */
  public int count()
  {
    return this.set.size();
  }

  //--------------------------------------------
  /** is the set empty */
  public boolean isEmpty()
  {
    if (this.set.size() == 0)
     { return true; }
    else 
     { return false; }
  }

  //--------------------------------------------
  /** return a particular translation based on its language code.
   *  These language codes will be in accordance with whatever
   *  is the applicable RFC and will probably be supplied by the 
   *  java 'locale' class which contains all of these codes 
   *  built in. */
  public String getTranslationByCode(String sLanguageCode)
  {
    String sReturn = "";
    Translation tCurrentTranslation;
    Enumeration ii = this.set.elements();
    while (ii.hasMoreElements()) 
    {
      tCurrentTranslation = (Translation)ii.nextElement();
      if (tCurrentTranslation.getLanguageCode() == sLanguageCode)
      {
	sReturn = tCurrentTranslation.getTranslation();	      
      }	
    } //-- while
      
    return sReturn;
  } //-- getTranslationByCode

  //--------------------------------------------
  /** add a Translation object to the set. Translation objects
   *  may become available for adding as the Record object
   *  parses through the data string. Normally the Record object
   *  will call this method */
  public void addTranslation(Translation tTranslation)
  { 
    this.set.addElement(tTranslation);
  }

  //--------------------------------------------
  public String print()
  { 
    StringBuffer sbReturn = new StringBuffer("");	  
    Translation tCurrentTranslation;
    Enumeration ii = this.set.elements();
    while (ii.hasMoreElements()) 
    {
      tCurrentTranslation = (Translation)ii.nextElement();
      sbReturn.append(tCurrentTranslation.print());
      sbReturn.append(NEWLINE);
    }
    return sbReturn.toString();
  } //-- print
  
  //--------------------------------------------
  public String toString()
  {
    StringBuffer sbReturn = new StringBuffer("");	  
    Translation tCurrentTranslation;
    Enumeration ii = this.set.elements();
    while (ii.hasMoreElements()) 
    {
      tCurrentTranslation = (Translation)ii.nextElement();
      sbReturn.append(tCurrentTranslation.toString());
      sbReturn.append(NEWLINE);
  
    }
    return sbReturn.toString();
  }
  
  //--------------------------------------------
  /** return a string which displays the data contained
   *  by the object at a particular time, in an
   *  unambiguous format. */
  public String debug()
  { 
    StringBuffer sbReturn = new StringBuffer("");	  
    Translation tCurrentTranslation;
    Enumeration ii = this.set.elements();
    while (ii.hasMoreElements()) 
    {
      tCurrentTranslation = (Translation)ii.nextElement();
      sbReturn.append(tCurrentTranslation.debug());
    }
    return sbReturn.toString();
  } //-- method: debug
  
  //--------------------------------------------
  /** A main method for console testing */
  public static void main(String[] args) throws Exception
  {

    StringBuffer sbUsageMessage = new StringBuffer("");
    sbUsageMessage.append("usage: java TranslationSet .");
    sbUsageMessage.append(NEWLINE); 

    if (args.length == 0)
    {	    
      System.out.println(sbUsageMessage);
      System.exit(-1);
    }


    TranslationSet ts = new TranslationSet();
    ts.addTranslation(new Translation("tr = [en]how far is it"));
    ts.addTranslation(new Translation("tr  = [es]es muy lejos o no"));
    ts.addTranslation(new Translation("tr  = [fr]cest tres loin"));
    System.out.println(ts.toString());

  } //-- main()

} //-- TranslationSet class
